Programming Windows, Fifth Edtion study notes Mole This book shows you how to write programs that run under Microsoft Windows 98, Microsoft Windows NT 4.0, and Windows NT 5.0. These programs are written in the C programming language and use the native Windows application programming interfaces (APIs). As I'll discuss later in this chapter, this is not the only way to write programs that run under Windows. However, it is important to understand the Windows APIs regardless of what you eventually use to write your code.
You should know C. If you do not know C, Windows programming is probably not a good place to start. I recommend that you learn C in a character-mode environment such as that offered under the Windows 98 MS-DOS Command Prompt window . But for the most part, you should have a good working familiarity with the language, particularly with C structures and pointers. Some knowledge of the standard C run-time library is helpful but not required.
I'll Be Assuming That You're Using Microsoft Visual C 6.0, Which Can Be PURCHASED SEPARATELY OR AS A Part of The Visual Studio 6.0 Package.
That's it. I'm not going to assume. You have any experience at all programming for a graphical user interface such as windows.
Central to The Workings of Windows Is A Concept KNown As "Dynamic Linking."
Your First Windows ProgramNow it's time to do some coding. Let's begin by looking at a very short Windows program and, for comparison, a short character-mode program. These will help us get oriented in using the development environment and going through the mechanics of Creating and compling a program.
The following is a program running in a plain text method: main () {printf ("Hello, World / N");} Yes, the C language uses a function of C running time (if the printf above) can not be declared . But now is after the 1990s, we are used to telling our compilers how to find the runtime library. The following is a modified code: #include
Return 0;} We should add more parameters to main, but first remove the focus from here.
The "Hello, World" program in The Windows EquivalentWindows is still using the components of the character mode version. There is an incline statement, a program's entry point, function call, and a return statement, as follows: / * --------------------------- ---------------------------------- Hellomsg.c - Displays "Hello, Windows 98!" In A Message Box (C) Charles Petzold, 1998 ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --------------------- * /
#include
Int WinApi Winmain (Hinstance Hinstance, Pstr Szcmdline, ICMDSHOW) {MessageBox (Null, Text ("Hello, Windows 98!"), Text ("Hellomsg"), 0);
Return 0;}
Create an empty Win32 application in the VC, then things should be very simple! No matter what the way is built, the code is the above thing. In the structure, the C program is equivalent to the "Hello, World" program in the character mode. But you have to pay attention, the header file stdio.h has been replaced by Windows.h, and the entry point main is also replaced by WinMain, and the runtime library function Printf of C is certainly used by WINMAIN. However, there are still a lot of things, including individual strange uppercase symbols.
Start with the head! The header fileshellomsg.c indicates in a pre-procedure, including such a top preoperative indication in each Windows program written in C: #include
Winnt.h Type Definitions for Unicode Support.
WinBase.h kernel functions.
WinUser.h user interface functions.wingdi.h graphics device interface functions.
These header files define all Windows data types, function calls, data structures, and constant markers. They are an important part of the Windows documentation. Maybe you will find that the Find In Files option in the EDIT menu is used to search for these headers. You can open them in Developer Studio and test them directly.
Program entry pointwinmain as the entry point of the Windows program, can also be called execution points, often written like this:
Int WinApi Winmain (Hinstance Hinstance, Hinstance Hprevinstance, PSTR Szcmdline, ICMDSHOW)
This entry point is proved in the / Platform SDK / User Interface Service Services / Windowing / Windows / Window Reference / Window Functions. It declares in WinBase.h:
INTWINAPIWINMAIN (Hinstance Hinstance, Hinstance Hprevinstance, LPSTR LPCMDLINE, INT NSHOWCMD);
You must notice that there are several changes in Hellomsg.c. The third parameter is LPSTR in WINBASE.H, but we use PSTR. Both data types are defined in winnt.h as pointers of characters or strings. The representative long pointer of the LP prefix is a 16-bit Windows product.
I have repeatedly changed the definition of Winmain to two parameter names; many Windows programs use the system to call "Hungarian symbol" to name the variable. The WinMain function declares a integer return type. WinAPI logo defines this statement in WINDEF.H:
#define winapi __stdcall
This statement specifies the call conversion, which contains how the machine encodes places the parameters of the function call in the stack. Most of the Windows function calls are declared into WinAPI.
The first parameter to WinMain is something called an "instance handle." In Windows programming, a handle is simply a number that an application uses to identify something. In this case, the handle uniquely identifies the program. It is required as an argument to some other Windows function calls. In early versions of Windows, when you ran the same program concurrently more than once, you created multiple instances of that program. All instances of the same application shared code and read-only memory (usually resources such as menu and dialog box templates). A program could determine if other instances of itself were running by checking the hPrevInstance parameter. It could then skip certain chores and move some data from the previous instance into its own data area.In the 32-bit versions of Windows, this concept has been abandoned. The second parameter to Winmain is always null (defined as 0).
The third parameter to WinMain is the command line used to run the program. Some Windows applications use this to load a file into memory when the program is started. The fourth parameter to WinMain indicates how the program should be initially displayed-either normally or maximized To Fill The Window, or minimized to be displayed in the task list bar. We'll See How this parameter is buy in chapter 3.
The MessageBox function MessageBox function is designed to display a short message. The first parameter of the Messagebox function is a Window Handle, which can be seen in Chapter 3 on Window Handle. The second parameter is in the message box, the third parameter is the text in the header bar of the message box.
The fourth parameter can start with the prefix MB_ this definition in WINUSER.H, you can select the button you want to display: #define MB_OK 0x00000000L # define MB_OKCANCEL 0x00000001L # define MB_ABORTRETRYIGNORE 0x00000002L # define MB_YESNOCANCEL 0x00000003L # define MB_YESNO 0x00000004L # when you define MB_RETRYCANCEL 0x00000005L to the fourth parameter is set to 0, then only the OK button, you can use C or (|) to combine these buttons: #define MB_DEFBUTTON1 0x00000000L # define MB_DEFBUTTON2 0x00000100L # define MB_DEFBUTTON3 0x00000200L # define MB_DEFBUTTON4 0x00000300L
You can also use an instance of the icon in the message box: #define MB_ICONHAND 0x00000010L # Define MB_ICONQUESTION 0x00000020L # Define MB_ICONEXCLAMATION 0x00000030L # define MB_IConasterisk 0x00000040L
Some icons have variable names: #define MB_ICONWARNING MB_ICONEXCLAMATION # define MB_ICONEROR MB_ICONHAND # define MB_ICONITIONMATION MB_ICONASTERISK # Define MB_ICONSTOP MB_ICONHAND
There are a few other MB_ constants, but you can consult the header file yourself or the documentation in / Platform SDK / User Interface Services / Windowing / Dialog Boxes / Dialog Box Reference / Dialog Box Functions.
In this program, the MessageBox function returns the value 1, but it's more proper to say that it returns IDOK, which is defined in WINUSER.H as equaling 1. Depending on the other buttons present in the message box, the MessageBox function can also Return iDyes, IDNO, IDCANCEL, Idabort, IDRETRY, or iDIGNORE.
Is this little Windows program really the equivalent of the K & R "hello, world" program? Well, you might think not because the MessageBox function does not really have all the potential formatting power of the printf function in "hello, world." But We'll See in the next chapter how to write a version of messagebox whp, link, and run This chapter learning technology, but also involve some other problems, some places have not seen it in depth, and Understanding may have a fault, so please read the official to correct one or two.