You will become quickly familiar with two functions when programming in Windows. These two functions are WinMain and the window procedure. In this section, you learn about both these functions.
In the steps in the preceding section, you probably noticed that CodeWarrior generates a great deal of code for you. Here, in its entirety, is the code that CodeWarrior generates:
With the knowledge of C++ that you have gained so far, you probably recognize parts of this code. First of all, it contains one include statement:
This statement includes the standard Windows library needed to create a Windows program. The Windows API contains many other libraries, but this file is the main one. Just remember that windows.h must be included in every Windows program.
You probably also noticed the two global character array variables, szProgName and message:
Windows does not require these two variables. They exist only for convenience. As the comments explain, szProgName stores the name of the program, and message holds the text that is displayed. Notice that if you change the value of message, you can change what is displayed! Perhaps that’s not as exciting to you as it is to us, but at least now you know that you have a choice.
Another thing you can immediately gather from this code is that it has two functions, WinMain and WndProc:
You probably will not recognize some terms in these function declarations—for example, WINAPI, HISTANCE, LPSTR, LRESULT, CALLBACK, HWND, UINT, WPARAM, and LPARAM. WINAPI and CALLBACK are special terms that you can ignore for now (just pretend they aren’t there).
The rest of the terms are Windows data types, each with a different purpose. For example, WinMain is the Windows equivalent of the main function. It does exactly the same thing in Windows that main does in console applications—provides the starting function for the program. All the arguments passed to WinMain (by Windows to your application) are Windows equivalents to command-line arguments. They provide all the information your program needs to perform normal Windows functions.
The WndProc function handles special events, such as when you click a button. The arguments that WndProc receives contain information about the type of event and the circumstances.
In the Real World
In Windows programming, events take on a special meaning. Normally, when you open a Windows program, it will load and sit, waiting for your input. This is different from, say, a real-time strategy game, where the computer opponents will build up an army to destroy you whether you do anything or not. This programming style is called event-driven programming.
Events in Windows happen frequently and in specified circumstances—for example, when the clock changes to a certain time or the user presses Enter or clicks the mouse. Events are happening all the time when Windows is running.
When Windows programmers design programs, they design functions called event-handlers (see “Handling Events,” later in this chapter, for more on event-handlers). Windows calls these functions whenever a certain event takes place. For example, when the user clicks a program’s Exit button, the program immediately exits.
As we stated previously, WinMain is the Windows equivalent of the main function. Windows calls WinMain when a program starts. When the program exits WinMain, the program is done. In this section, you examine WinMain in detail.
Windows passes these four arguments to your application when the application begins: HINSTANCE hInst, HINSTANCE hPreInst, LPSTR lpszCmdLine, and int nCmdShow.
hInst is a handle to your application. A handle is a lot like a pointer or a reference and keeps track of various Windows elements, such as your application, your application’s program windows, and other things. Inst stands for instance. If two copies of your application are running at the same time, each one will be a different instance of the same application. hInst is a handle to this particular instance of the application. It can be useful for calling some of the Windows API functions.
hPreInst is a handle to the previous instance of your application. This is outdated and no longer has use, so just ignore it. The only reason that it is still here is for backward compatibility. Backward compatibility occurs when a new version of some software is still compatible with older versions. This way, people using older versions won’t have to buy the newer version.
lpszCmdLine is just a null-terminated string that stores the command-line arguments. If you go to the Start menu and select Run, you can type anything you want after the program name. For example, if your program is named Hello.exe, you can type Hello.exe Goodbye. Then you can design the program to use this command-line argument to display whatever message is supplied, in this case Goodbye. Notice that the name of the program, Hello.exe, is not part of lpszCmdLine.
nCmdShow is an integer specifying how the window will display. You can treat this as a suggestion from Windows as to how your window should look. Like most suggestions, this one can be ignored. This argument has too many possible values to list here, but Table lists the ones generally needed.
Possible Values for nCmdShow
Knowing all the possible values is nearly impossible (Table shows approximately 5 percent of them). However, you’ll probably use only the first three values in Table 12.1 in roughly 99 percent of your programming.
However, if you need to know the other possible values, you can find them in many places. One good place to find them is the Microsoft Developer Network (MSDN). For more detailed information about Windows programming, you might go to your library and look up Platform SDK.
WndProc is the function called by Windows whenever an event occurs that affects your program. Windows passes arguments to WndProc that explain what the event is. You, as the programmer, have an option of letting Windows handle the event in its own way or handling it yourself. It is not possible to handle all the events yourself—there are just too many of them.
WndProc supports these four arguments: HWND hWnd, UINT messg, WPARAM wParam, and LPARAM lParam.
hWnd is a handle to the window the event is relevant to. You will not need to use this argument often, but if you have multiple windows that use the same version of WndProc, hWnd enables you to tell which window should receive which message. You use the data type HWND to refer to a window in Windows programming. Handles are used in a similar way as classes are used. For example, to refer to a bitmap (a kind of image), you use the HBITMAP data type.
messg is the ID number of the message. There are hundreds of different messages. Table shows some of the possible messages.
Common Message IDs
In the section “Handling Events,” later, you learn how to use some of these message IDs; right now, you just need to know they exist and that there are hundreds of them.
wParam and lParam are extra information about an event. These arguments are different for every event and may or may not be filled with values, depending on the type of message.
The return type, LRESULT, is a standard return type for many Windows functions. It is a type used to store the result of a function.
C++ Related Interview Questions
|DBMS Interview Questions||C++ Interview Questions|
|C#. NET Interview Questions||Core Java Interview Questions|
|Data Structures Interview Questions||C & Data Structures Interview Questions|
|ADO.Net Interview Questions||Java Interview Questions|
|Go (programming language) Interview Questions||F Sharp (programming language) Interview Questions|
|C preprocessor Interview Questions||R Programming language Interview Questions|
|D Programming Language Interview Questions||Embedded C Interview Questions|
All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.