Tricks of the Windows Game Programming Gurus:The Windows Programming Model

All ITKnowledge Source Code Search Tips Advanced Search |

| Tricks of the Windows Game Programming Gurus by Andre LaMothe Sams, Macmillan Computer Publishing ISBN: 0672313618 Pub Date: 09/27/99

Search this book:

Previous | Table of Contents | Next

Examining Parameters

  • `hinstance`—This parameter is the instance handle that Windows generates for your application. Instances are pointers or numbers used to track resources. In this case, `hinstance` is used to track your application, like a name or address. When your application is executed, Windows will supply this parameter.
  • `hprevinstance`—This parameter is no longer used, but in past versions of Windows, it tracked the previous instance of the application (in other words, the instance of the application that launched the current one). No wonder Microsoft got rid of it! It’s like time travel—it gives me a headache thinking about it.
  • `lpcmdline`—This is a null-terminated string, similar to the command-line parameters of the standard C/C++ `main(int argc, char **argv)` function, except that there isn’t a separate parameter analogous to argc indicating the number of command-line parameters. For example, if you create a Windows application called `TEST.EXE` and launch it with the following parameters:
    `TEST.EXE one`

  lpcmdline will contain the following data:

    `lpcmdline = “one two three”`

  Notice that the name of the .`EXE` itself is not part of the command line.

  • `ncmdshow`—This final parameter is simply an integer that is passed to the application during launch, indicating how the main application window is to be opened. Thus, the user has a little control over how the application starts up. Of course, as the programmer, you can disregard this if you want, but it’s there if you want to use it. (You pass it to `ShowWindow()`, but we’re getting ahead of ourselves.) Table 2.2 lists the most common values that ncmdshow can take on.

Next, let’s look at each other of the parameters in detail:

Table 2.2 Windows Codes for `ncmdshow`
Value | Function
--- | ---
`SW_SHOWNORMAL` | Activates and displays a window. If the window is minimized or maximized, Windows restores it to its original size and position. An application should specify this flag when displaying the window for the first time.
`SW_SHOW` | Activates the window and displays it in its current size and position.
`SW_HIDE` | Hides the window and activates another window.
`SW_MAXIMIZE` | Maximizes the specified window.
`SW_MINIMIZE` | Minimizes the specified window and activates the next top-level window in the Z order.
`SW_RESTORE` | Activates and displays the window. If the window is minimized or maximized, Windows restores it to its original size and position. An application should specify this flag when restoring a minimized window.
`SW_SHOWMAXIMIZED` | Activates the window and displays it as a maximized window.
`SW_SHOWMINIMIZED` | Activates the window and displays it as a minimized window.
`SW_SHOWMINNOACTIVE` | Displays the window as a minimized window. The active window remains active.
`SW_SHOWNA` | Displays the window in its current state. The active window remains active.
`SW_SHOWNOACTIVATE` | Displays a window in its most recent size and position. The active window remains active.

As you can see from Table 2.2, there are a lot of settings for `ncmdshow` (many of which make no sense at this point). In reality, the majority of these settings will never be sent in `ncmdshow`. You will use them with another function, `ShowWindow()`, which actually displays a window once it’s created. However, we will get to this a little later in the chapter.

The point I want to make is that Windows has a lot of options, flags, and so on that you will never use, but they’re still there. It’s like VCR programming options—more is always better, as long as you don’t need to use them if you don’t want to. Windows is designed this way. It has to make everybody happy, so that means including a lot of options. In fact, we will use `SW_SHOW`, `SW_SHOWNORMAL`, and `SW_HIDE` 99 percent of the time, but you need to know the other for that one percent!

#### Choosing a Message Box

Finally, let’s talk about the actual function call to `MessageBox()` within `WinMain()`. `MessageBox()` is a Win32 API function that does something useful for us, so we don’t have to do it. It is used to display messages with various icons, along with a button or two. You see, simply displaying messages is so common in Windows applications that a function was written just to save application programmers the half hour or so it would take to write one every time.

`MessageBox()` doesn’t do much, but it does enough to get a window up on the screen, ask a question, and wait for the user’s input. Here is the prototype for `MessageBox()`:

    int MessageBox( HWND    hwnd,     // handle of owner window

                    LPCTSTR lptext,   // address of text in message box

                    LPCTSTR lpcaption,// address of title of message box

                    UINT    utype);   // style of message box

The parameters are defined as follows:

  • `hwnd`—This is the handle of the window you want the message box to be attached to. At this point I haven’t covered window handles yet, so just think of it as the parent of the message box. In the case of `DEMO2_2.CPP`, we are setting it to `NULL`, so use the Windows desktop as the parent window.
  • `lptext`—This is a null-terminated string containing the text you want to display.
  • `lpcaption`—This is a null-terminated string containing the caption for the message dialog box.
  • `utype`—This is about the only exciting parameter of the bunch. It controls what kind of message box is displayed.

Previous | Table of Contents | Next

Copyright © Macmillan Computer Publishing, Inc.

Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.