Game development basis (2)
Chapter II Windows Programming Basic First Introduction In order to keep up with the trend, we abandoned the DOS operating system that has been eliminated, all explanations and routines are based on Microsoft's Windows operating system. Considering a lot of users don't have a Windows programming basis, we set this special story, discuss the terms of Windows, and the part of the concept, so that this part of the users can understand and master our programming, discussion. Programming method. This part mainly tells something very basic in Windows, so users should learn according to their own situation. it is good! Let us now enter the way of hard and exciting game programming! The second section of Windows describes how Windows applications can adopt process-oriented implementation methods. Object-oriented structures can also be used. All implementations are integrated with clicking control and pop-up menus, which can run applications written in Windows. Windows is a multitasking operating system based on a graphical interface. Programs developed for this environment (those designed for Windows) have the same look and command structure. For users, this makes learning to use Windows applications easy. To help develop Windows applications, Windows provides a large number of built-in functions to easily use pop-up menus, scroll bars, dialogs, icons, and other friendly user interfaces that should have features. Windows runs the application with hardware-independent way to process video display, keyboard, mouse, printer, serial port, and system clock. The most worthless Windows feature is its standardized graphical user interface. The unified interface uses pictures or icons to represent the commands and movements of the disk drive, file, subdirectory, and other operating systems. The unified user interface has also brought advantage of programmers. For example, you can easily use the built-in function of the common menu and dialog box. All menus have the same style keyboard and mouse interface because it is Windows instead of the programmer to implement it. Windows multi-tasking environments allow users to run multiple applications or multiple instances of the same application at the same time. An application may be active. Activated application refers to it is receiving the user's input. Because each instant is only one program can be processed, only one application can be activated at the same time. However, you can have any number of parallel running tasks. Section III Windows Basic Concept Windows Messages and Object-Oriented Programming Windows implements an imitation OOP (object-oriented programming) environment. The message system under Windows is responsible for breaking information in a multi-task environment. From an application perspective, the message is a notification of an event that occurs. Users can generate these events by pressing or moving the mouse, or by changing the window size or selecting a menu item. These events can also be generated by the application itself. Windows itself can also generate messages. For example, "Close Windows" message, Windows notifies all applications through this message, and Windows will be turned off. Memory Management System Memory in Windows System is one of the most important shared resources. When there are multiple applications at the same time, in order to do not consume system resources, each application must cooperate to share memory. At the same time, the memory will be fragmented when the new program and the old program are turned off. Windows can connect the memory empty space by moving the code and data block in the memory. It is also possible to use memory in Windows. For example, the application can be larger than the memory capacity. Windows is able to discard the currently unused code, and read it from the application when needed in the future. Windows applications can share routines in executable. The file containing a shared routine is called a dynamic link library (DLL). Windows includes the mechanism of chain the DLL routine linkage. Hardware-independent Windows also provides hardware or device-independence, so that you have to consider the possible use of displays, printers or input devices when you generate programs. Below Windows, the driver for each hardware device is only written once. Hardware irrelevant makes programming easier for application developers. The application is dealing with Windows instead of various devices. The dynamic keying library dynamic key connector provides more Windows feature.
They enhance the basic operating system through a powerful and flexible graphical user interface. Dynamic Key Library includes some predefined functions that can be used (dynamically) when an application is transferred, rather than when the application is created (static). Dynamic Key Library uses the DLL suffix. The library liberates each programmer from a repeatedly developed a common routine such as reading characters or formatted outputs. Programmers can easily construct their own library to contain more features, such as changing fonts or testing text. Change the function to a general purpose redundancy design, which is a key feature of OOP. The library of Windows is dynamically bore. Alternatively, the key is not copied to the program's executable file. Conversely, when the program runs, it produces a call to the library function. Nature, this saves memory. Regardless of how many applications are running, there is always a book in RAM, and this library can be shared. Windows's executable Format Windows has a new format of the new executable, called the New Excutable format. It includes a new file header that saves information about the DLL function. Section IV Windows Windows Windows window window looks like a rectangular area in the display device, its appearance is independent of a specific application, but for an application, the window is on the screen to directly control Rectangular area. Applications can create and control everything in the main window, such as size and shape. When the user starts a program, a window is created. The user responds each time you click on the window. Turning off a window will end the application. Multi-window brings multi-tasking capabilities for users Windows. By dividing the screen into a different window, the user can select a parallel running application using a keyboard or mouse to enter a specific program in a multi-task environment, and Windows intercepts the user's input and assigns the necessary resources (for example microprocessor). Windows's layout All Windows applications have common features such as borders, control menu, and About dialogs. These features make each Windows application very similar. Border Windows window is surrounded by the border. The border is composed of a line around the window. For newcomers, the border looks only to open the screen viewport of an application with other distinction. However, for the skiller, the border has a different role. For example, if the mouse pointer is placed on the border and press the left button of the mouse, the user can change the size of the window. The name of the title system is displayed in the headings in the top of the window. The title bar is always at the center of the top of the related window. The title bar is very useful, it can help you remember which application is running. The title bar of the active application is displayed in a color different from the non-active application. Control Icon Control Icon is a small picture above each window, and each application uses it. Click the mouse button on the control icon to display the system menu. The system menu opens the control menu when using the mouse to control the icon. It provides standard operations such as Restore, Move, Size, Minimize, Maximize, and Close. Minimize icons Each Windows 95 or Windows NT application is displayed in the upper right corner of the window. The leftmost icon is a short scribe, which is the minimum icon. It can be minimized using programs. Maxi icon Maxi icon is the one in the middle of three icons, looks like two small windows. Using the maximum icon can use the application to account for full screen. If this icon is selected, other application windows will be covered. Vertical scroll bar If necessary, the application can display a vertical scroll bar. The vertical flow bar is displayed on the right side of the application window, and there are two directions opposite to both ends. It also has a colored rod and a transparent window block. The latter is used to show the relationship between the current display content and the entire document (colored rod). You can use the scroll bar to choose which page. Generally, click on any arrow to move the display.
Click the window block under the up arrow and drag it to quickly update the screen output to any location of the application screen output. Horizontal scroll bar can also display a horizontal scroll bar. The horizontal scroll bar is displayed at the bottom of the window with a function similar to the vertical scroll bar. You use it to choose which columns want to display. Generally click on one of the arrows to move the display. Click the window block on the left arrow and drag it to quickly update the screen output to any location of the application screen output. A selectable menu bar can be displayed below the title bar. Select the menu and submenu through the menu bar. This option can be implemented by using a mouse click or a hotkey combination. The hotkey combination is often a combination of Alt and the order in the order, such as "f" in the file command. The user area usually user area occupies the largest part of the window. This is the basic output area of the application. The application should be complex by the application. In addition, the application can output to the user area. The basic components of the fifth section of Windows window helps to illustrate the appearance of the application. Sometimes the application needs to create two windows similar to both appearances and performance. Windows's Paint is an example. By running two instances (or copies) of the Paint, Paint allows the user to clip or copy a portion of the picture. The information can then be copied from one instance to another instance. The appearance and performance of each run instance of Paint are the same as others. This requires every instance to create its own look and functionally similar windows. The appearance and function of the created in this case are called the same window class. However, the window you created can have different features. They can have different sizes, different locations, different colors or different titles, or different cursors. Each created window is based on a window class. In applications developed with C language, some windows are registered for training initialized in Windows applications. Your app can register your own window class. In order to enable several windows to create on the same window class, Windows defines some window features, such as the parameters of CREATEWINDOWS (), and the structure of other defined window classes. When you register a window class, this class can be used by any program running under Windows. For applications that use MFC, most registration work has been completed by a predefined object. A window with similar appearance and performance can be combined into a class to reduce information that requires maintenance. Because each window class has its own shared class structure, it is not necessary to copy unnecessary window class parameters. At the same time, the two windows of the same type use the same function and related routines. This saves time and space because there is no code replication. Section 6 Object-Oriented Programming in Windows Absorbs Some Object-Oriented Programming under Windows. Objects are an abstract data type including a data structure and a function that operates on these data structures. Moreover, object receptions will cause a message to different actions. For example, a Windows graphic object is a collection of some data that can be manipulated as an entity, which is part of the visual interface for the user. Special, an object means this data and data function. Menu, title bank, control block, and scroll bars are examples of graphics objects. The next section describes some new graphics objects that affect the appearance of the application. The icon icon is used to use small graphics objects that remember specific operations, ideas, or products. For example, an spreadsheet program is minimized to display a small histogram to remind the user that this program is still running. Double-click the mouse on the histogram to activate this application. The icon is very powerful tool. It is very suitable for the user's attention, such as providing an error warning or providing a selection to the user. The cursor cursor is a graphic symbol of the sport of the pointing device to track the point of view. This graphic symbol can change shape to indicate a particular Windows operation. For example, when the standard arrow is changed to the hourglass cursor, the Windows is executing a command and needs to be paused. Edit the Cursor Application Displays the editing cursor in the window to tell the user where to enter. The editing cursor is obviously different from other screen symbols because it is blinking. Most time, the mouse input is connected to the cursor, and the keyboard input is connected to the editing cursor.
However, you can use the mouse to change the input point of the editing cursor. The message box message box is another Class of Windows graphics objects. The message box is a pop-up window that contains headers, icons, and messages. Figure (?) Is a standard message box that occurs when closing the Windows NotePad program. -------------------------------------------------- --------------------------------------------------------------------------------------------------------------- --------------------------------------------- Windows dialogue The box dialog and the message box are similar to it is also a pop-up window. However, dialogs are mainly used to accept user inputs, not just display some output. The dialog allows the application to accept input, each time a domain or a box content, not each character. Figure (?) Shows a typical Windows dialog. The graphic design of the counterfeit frame is automatically completed by Windows. The layout of the dialog is usually done with the resource editor in the compiler. -------------------------------------------------- --------------------- | --------------------------- ------------------------------------------- font font is a graphic Objects or resources define the words of the full character set. These characters have a specific size and style that allows text to have different appearances. The word is a basic attribute of the character, which defines the liner of the character and the stroke width. Bitchart diagram is a display picture (according to pixel organization), stored in memory. A bitmap can be used when the application needs to quickly display pictures. Because the bitmap is transmitted directly from the memory, it is much better than the picture re-draws a picture. The bitmap has two basic purposes. First, it can display pictures on the screen. The second bitmap is also used to create a brush. The brush allows you to draw and fill the object on the screen. There are two shortcomings using the bitmap. First, it is related to its size, and the bitmap will occupy a large amount of memory that is difficult to estimate. Each displayed pixel is to occupy the corresponding space in memory. Displaying a pixel on a color display will occupy more memory than in a monochrome display. On a monochrome display, only one (bit) can represent the state of the pixel. However, on the color display of 16 colors, four are required to represent a pixel feature. Similarly, as the resolution of the display device is increased, the demand for the memory has also increased. Another disadvantage of a bitmap is that it only includes a static picture. For example, if you use the bitmap to represent a car, there is no way to access different parts of the picture, such as route, top cover, window, etc. However, if the car is generated by a series of basic drawing routines, the application can change the data transmitted to these routines to change the different parts of the image. For example, the application can modify a top strand and turn a car into a cabriolet. Brush When Windows displays a graphic on the screen, it uses the current brush information. Brush is used to draw lines or rockeeles. The brush has three basic characteristics: line width, line type (dashed line, short line, solid line) and color. Windows always keeps a brush for painting white lines and black lines, and any app can use it. You can also create your own brush. The brush Windows uses a brush to draw colors and fill a region in a predefined style. The brush has at least 8 × 8 pixels. There are three basic characteristics: style and color. Since they have at least 8 × 8 sizes, the brush is called a pattern rather than a brush, called a line style. The style can be pure color, or a shadow line, a slash or other user-defined combination Section 7 Windows message Windows message, in Windows, the application does not write on the screen, handle hardware interrupts or direct printers Output. In contrast, the application uses a suitable Windows function or waits for an appropriate message. The Windows Message System is responsible for assigning messages in multi-tasking environments.
From an application's point of view, the message can be seen as a notification of events, some need to make a specific reaction, and some do not need it. These events may be generated by the user, such as pressing the mouse or moving the mouse, changing the size of the window, or selecting a menu. At the same time, these events may also be generated by the application itself. This process allows your application to handle messages. When a message is received, the application must activate and determine the correct action, and return to the wait state after completing this action. By checking the format and source of the message, the next part will be more carefully discussed. The format message of the message notifies an event that an application has taken place. From a technical context, the message is not only related to the application, but also related to a particular window of the application. Therefore, all messages are sent to the window. There is only one message system under Windows - the system message queue. However, each application running under Windows has its own message queue. Each message in the system message queue is eventually to be transferred by the USER module to the application queue of the application. All messages of all the windows of the program are stored in the application's message queue. Regardless of the type of message, they have four parameters: a window handle, a message type, two additional 32-bit parameters. The first parameter defined in the window message is the window handle associated with the message. Use the handle frequently when writing a Windows application. The handle is a unique number that is used to identify many types of objects such as menus, icons, brushes, and brushes, memory allocation, output devices, even window instances. In Windows 95 and Windows NT, each running copy of the program is called an instance. Because Windows 95 and Windows NT allow you to run multiple instances of a program at the same time, the operating system must keep track of these instances. This is achieved by imparting a unique instance handle of each running instance. The example handle is typically used as an index of an internal maintained table. By referring to the elements in the table instead of the actual memory address, Windows 95 and Windows NT can dynamically adjust all resources, and only insert a new address in the table location corresponding to this resource. The memory resources are saved by Windows 95 and Windows NT, based on a plurality of instances of an application. Examples of applications have a very important role. The instance of the application defines all objects required for the program's function. This includes controls, menus, dialogs, and more new Windows classes. The second parameter in the message is the type of message. This is the identifier defined in some header files unique to Windows. These headers can be used by Windows.h. Under Windows, each message starts with two characters, followed by underscore, and finally a descriptor. The last two parameters provide additional information required to explain the message. Therefore, the content of the last two parameters depends on the type of message. Generating Message Message Transfer Concepts make Windows implement multiple tasks. There are four basic sources of messages. Applications can accept messages from users, or the Windows itself, the application itself or other applications. User messages include buttons, mouse movement, mouse points, or click, menu selection, and scroll bar positioning, etc. Applications must spend a lot of time to handle user messages. Messages generated by users indicate that people running programs want to change the application's performance. Whenever, if the state changes, there will be a message to be sent to the application. An example is that the user clicks the app's icon, indicating that they want to turn this application into an active application. In this case, Windows tells the application that its main window is opened, and its size and position are changed, and the message generated by Windows can be processed, or it can be ignored, which is related to the current state of the application. The corresponding message is in the traditional process-oriented C language Windows application, and each message encountered, it has a corresponding process to handle this message. Different windows give different responses to the same message.
Windows sends each message to different windows of the application, and different windows have different interpretations for the same message. Do not make applications require different processes to handle each message, each window should also have different processes to handle different messages. The window process collects all message processing procedures for the application. A basic composition of all Windows applications is a message processing loop. Each C application performs this operation within it. The C application contains the process of creating and initializing the window, followed by a message processing loop, and finally some of the code required to end the application. The message loop is responsible for handling the message sent to the main program. Here, the program knows with a message and requires Windows to send messages to the appropriate window procedure for processing. When the message is accepted, the window process performs the hope of the hope. Section 8 Windows' function Windows provides hundreds of functions to application developers. Examples of these functions include Dispatchmes-Sage (), PostMessage (), RegisterWindowMessage (), and setActiveWindow (). Many functions are automatically run for C programmers using the underlying class library. The function declaration under the 16-bit Windows 3.x includes a PASCAL modifier, which is more valid under the DOS More valid Windows 95 and Windows NT. The 32-bit application under Windows NT will no longer use this modifier. As you know, all the parameters of all Windows functions are passed through the system. The parameter of the function starts from the rightmost parameter to the left, which is the standard C method. Before returning from the function, the calling process must be pressed into the stack of bytes to adjust the stack pointer. Article 9 Windows Application Framework Windows Header file: Windows.hWindows.h header file (and other related files) is the inner part of all programs. Traditionally, Windows.h is a part of the Windows applications written in all C language. WINDOWIN.H is included in the AFXWIN.H header when using the underlying class library in C . The consisting of Windows applications has some important steps in the development of a Windows application: * Write the WinMain () function and related window functions with the C language, or use basic classes in C , such as CWINAPP, etc. * Create a menu, dialog, and other resources and put them in a resource description file. * (Optional) Create a dialog using the Enterprise Editor in the Vinsual C compiler. * (Optional) Create a dialog using the Enterprise Editor in the Vinsual C compiler. * Compile and links all C / C source programs and resource files for components in Windows applications 1. WinMain () function Windows 95 and Windows NT require a Winmain () function. This is where the application starts execution and end. Pass four parameters from Windows to WinMain (). The following code segment demonstrates the use of these parameters: int WinAPI WinMain (Hinstance Hinst, Hinstance Hpreinst, LPSTR 1PSZCMDLINE, INT NCMDSHOW) The first parameter HINST contains an instance handle of the application. This number uniquely identifies the application when the application runs in Windows. The second parameter hPreinST will always be a null value, indicating that other instances without this application are running because each application is running in its own address space under Windows 95 and Windows NT. The third parameter 1pszcmdline is a long pointer to a string ending at '/ 0', which represents the command line parameters of the application. The fourth parameter of WinMain is NCMDSHOW. The integer stored in NCMDSHOW represents one of the many constants of Windows predefined, which determines the way the window is displayed.
2. Wndclasswinmain () is responsible for the main window class of the application. Each window class is based on some of the style, font, title, icon, size, location, etc. selected by some users. Window classes are actually a template that defines these properties. Basically, all Windows class definitions use the same standard C / C structure. The following example is a description typedef statement WNDCLASSW structure, WNDCLASS is inherited from here: typedef struct tagWNDCLASSWUINT style; WNDPROC 1pfnWndProc; int cbClsExtra; int cbWndExtra; HANDLE hInstance; HICON hIcon; HCURSOR hCursor; HBR8USH hbrBackground; LPCWSTR 1pszMenuName; LPCWSTR 1pszClassName WNDCLASSW, * PWNDCLASSW, NEAR * NPWNDCLASSW, FAR * LPWNDCLASSW; the following part discusses different domains in the WNDCLASS structure. Some of these domains can be given null, telling Windows to use the default predefined value. Style: The Style domain indicates a class style. 1pfnWndProc: Accept a pointer to a window function, which will perform all window tasks. Cbclsextra: Specifies the number of bytes that must be allocated behind the window structure, which can be NULL. CbWndextra: Specifies the number of bytes that must be assigned later after the window instance, it can be NULL. Hinstance: Defines the application instance of the registered window class. It must be an instance handle, not NULL. Hiconhicon: The icon that is displayed when the window is minimized. It can be NULL. HCURSORHCURSOR: Defines the cursor used by the application. This handle can be NULL. HBRBACKGROUND: The identifier of the background brush is provided. 1pszMenuname: It is a pointer to a string ending with an empty character. This string is the resource name of the menu. This can be NULL. 1pszclassname: It is a pointer to a string ending with an empty character. This string is the name of the window class. 3.WndClassexWindows provides an extended WndClass definition named WNDCLASSEX, which allows the application to use a small icon. Here is the definition WNDCLASSEX structure: typedef struct WNDCLASSEXUINT style; WNDPROC 1pfnWndProc; int cbClsExtra; int cbWndExtra; HANDLE hInstance; HICON hIcon; HCURSOR hbrBackground; LPCTSTR 1pszMenuName; LPCTSTR 1pszClassName; HICON hIconSm; WNDCLASSEX; you can see these two structures are Similarly, in addition to WNDCLASSEX includes HiconSM members, this is a small icon with a window class. 4. Define window applications can define their own window classes, as long as a suitable type of structure is defined, then fill the structure of the structure with the information of the window class. The following code demonstrates how to define and initialize a WNDCLASS structure.
char szProgName [] = "ProgName"; ... WNDCLASS wcApp; ... wcApp.1pszClassName = szProgName; wcApp.hInstance = hInst; wcApp.1pfnWndProc = WndProc; wcApp.hCursor = LoadCursor (NULL, IDC-ARROW); wcApp .hIcon = NULL; wcApp.1pszMenuName = szAppIName; wcApp.hbrBackground = GetStockObject (WHITE-BRUSH); wcApp.style = CS-HREDRAW | CS-VREDRAW; wcApp.cbClsExtra = 0; wcApp.cbWndExtra = 0; if (RegisterClass! (& WCAPP)) RETURN 0; the second domain in the WNDClass structure is Wcapp.hinstance, which is given the value returned after WinMain () is activated. This indicates the current instance of the application. 1pfnWndProc is given a pointer address of the window function of all window tasks. For most applications, this function is called WndProc (). Note: WndProc () is a user-defined and is not a predefined function name. Function prototypes must be given before assigning statements. Wcapp.hcursor domain is given the cursor handle of the instance. When Wcapp.1pszMenuname is given a null value, Windows thinks that this window class does not have a menu. If so, the menu must have a name, it must appear in the quotation mark. The getStockOject () function returns a brush handle for drawing the background color in the window user area created by this class. The Wcapp.Style window style is set to CS-HREDRAW or CS-VREDRAW. The last two domains, WeApp.cbclsextra and Wcapp.cbWndextra are often set to 0. These domains can be selected to indicate the number of additional bytes that should be retained behind the window structure and the window data structure. The following segment is used to register the window class: if (! Hpreinst) ... if (! Registerclass (& wcapp)) Return False; Windows 95 and Windows NT determine how many instances by checking HPREINST, and HPreinst is always null So register the window class .5. Creating a window window is created by calling the CREATEWINDOW () function. This process is the same for all versions of Windows. The window class defines the general feature of the window, allowing the same window class to be used in a plurality of different windows, and the parameters of the CreateWin-Dow () function indicate more detailed information about the window. The parameter information of the CREATEWINDOW () function includes the following: window classes, window titles, window styles, curtain positions, parent sorts of windows, menu handles, unique handles, and 32-bit additional information.
In most applications, this function looks like this: hWnd = CreateWindow (szProgName, "Simple Windows Program", WS-OVERLAPPEDWINDOW, CW-USEDEFAULT, CW-USEDEFAULT, CW-USEDEFAULT, CW-USEDEFAULT, (HWND) NULL, (HMENU) NULL, (HANDLE) HINST, (LPSTR) NULL); the first domain szprogname defines the class of the window, followed by the title used on the window title bar. The style of the window is the six parameters below the third parameter represent the size of the x, y coordinates, and X, Y directions, and then the parent window handle and window menu handle. Each domain is given a default. The HINST domain contains an instance handle of the program, which is an additional parameter (NULL). The display and update window Under Windows, the showWindow () function is used to actually display a window. The following code demonstrates this function: show window (hwnd, ncmdshow); the window handle generated when calling createWindow () is used as the HWND parameter. The second parameter of ShowWindow () is ncmdshow, which determines how the window is displayed. This display status is also referred to as a window visual state. The final step of the display window is to call the Windows Update Window () function. UpdateWindow (HWND); 6. Message loops When you call the Win-Main () function and display the window, the application requires a message processing loop. The most common implementation method is to use a standard WHILE loop: while (GetMessage (& lpmsg, null, 0, 0)) {TranslateMsSage (& lpmsg); DITMESSAGE (& lpmsg);} getMessage () function: The next next The message can be obtained by calling the Windows getMessage () function. NULL parameter indication function Retrieves any message of any window of this application. The last two parameters 0 and 0 tell GetMessage () do not use any message filter. Message filters can limit the received messages within a clear range, such as keyboard messages or mouse messages. General applications should confirm that all steps that are universally messaging have been executed correctly. This includes confirming that each window class has been registered, all have been created. Otherwise, once the message loop is entered, only one message can end this loop. Whenever the WM-Quit message is processed, the return value is false. This will initiate the main loop closure routine. The WM-Quit message is the only way to exit the message loop for the application. TranslateMessage () Function: Virtual message can be converted to a character message via the TranslateMessage () function. DispatchMessage () Function: Windows Sends the current message to the correct window process via the dispatchMessage () function. ******* All applications all applications must include a WinMain () function and a callback window function. Because a Win-DOWS application never accesss any window functions directly, each application must make requests to Windows to perform the specified operations. A callback function is registered in Windows, which is called when Windows is operated on a window. The actual code length of the callback function of each application will be greatly different. The window function itself can be very small, only one or two messages can also be very large and complicated.
The following code segment (incomplete application description statement) shows an example of the callback window function WndProc () in the application: LRESULT CALLBACK WNDPROC (HWND HWND, UNIT Messg, WParam WPARAM, LPARAM 1PARAM) HDC HDC; Paintstruct PS; Switch (Messg) Case WM-PAINT: HDC = BeginPaint (hwnd, & ps); Endpaint (HWND, & PS); Break; Case Wm-Destroy: PostquitMessage: PostquitMessage (0); BREAK Default: Return (DefWindowProc (WPARAM, 1PARAM); Return (0); Windows hopes that the name of WCAPP, 1pfnWndProc domain can match the name of the callback function in the window class structure definition. The callback function of all windows created later with this window should use the name of WndProc (). The following code segment discusses the location and assignment of the callback function name in a window class: ... wcapp.1pszclassname = szprogname; wcapp.hinstance = hinst; wcapp.1pfnWndproc = WndProc; ... Windows is available to 100 messages can be sent Give the window function. These messages identified with the "WM-" header. The first parameter of WndProc () is HWND. HWnd contains Windows sends a window handle. The second parameter of the function Messg Press the definition in Winuser.h indicates the actual message that is about to be processed. The last two parameters WPARAM and 1PARAM indicate additional information that handles each message. The WndProc () function continues to define two variables: HDC indicates a display device handle, and PS indicates a PAINTSTRUCT structure required to store user area information. The callback function is used to check the messages that will be processed and select Perform the appropriate action. This selection process is usually done in a standard C language Switch statement. The module definition file is just like you learned in front, Link provides alternatives to the module definition files required by Windows applications. Module Definition File Provides definition and description information to the linker so that the linker knows how to organize the executable of the Windows application. This information has become part of the file header of the new execution file format. Note: You are unlikely to create a module definition file below Windows 95 and Windows NT. This information is for integrity and backward compatibility. Section 10 VC Windows Programming Tools Visual C compiler contains several resource editors. A separate editor can run through the INSERT RESource menu in the compiler main menu. Graphics objects are resources, icons, cursors, message boxes, dialogs, fonts, bitmaps, brushes, brushes, etc. The resource represents the data contained in the executable of the application. Resource compiler RC.EXE is a compiler of a Windows resource. . The use of resources and additional compilers add the complexity of application development. But it is easy to use in project tools.
The project file project file provides a means of the overview resource and program code compilation process, and it can also keep the application's executable version to the latest. They track the date and time of the source file to implement these enhancements. The project file contains information about the compile chain process for a particular program. The project file is created in an integrated C or C editing environment. Project files also support enhanced compilation and links. Resources When you use the resource editor provided by the Visualc compiler, custom Windows applications with their own icons, pointers, and bitmaps. These editors provide you with a complete environment for developing graphics resources. These editors can also help you develop menu and dialog box - the basic means of data entries underwindows. These editors can help you manipulate separate bitmaps, accelerator keys, and strings. . Resource Editor Each editor is provided in a Visualc environment and is an integrated part of the compiler. In this way, each editor is a fully integrated resource development tool running under Windows. You can start each editor by selecting Insert Resource. Below we will demonstrate the use of the resource editor by tutorial. Click Back, then run the tutorial. ? The basic concept of the MFC's basic concept base base library provides you with easy to use objects. It is natural to link Windows with C , so that the advantages of object-oriented technology can be taken. The MFC development group implements a large number of Windows Application Programming Interfaces (APIs). This C library encapsulates the most important data structure and API function calls in some reusable classes. There are many advantages over the library of MFC compared to the C programmers discussed in the previous two chapters. Some of the advantages of the C class are listed below, such as: * Encapacity with data and code * Inheritance * Classification of conflicts of functions and variable names * Class is the natural expansion of language * Usually, the carefully designed library reduces code quantity Using the underlying class library, create a window required to only account for approximately one-third of the traditional application. This allows the programmer to deal with Windows with a small time with Windows, and focus more energy on the development of their own program code. 22.2 MFC Design Considering Basic Class Branch Design Group defines strict design rules, these rules must be followed when designing the MFC library. These rules and approaches are as follows: * Use the power of C , but cannot scare the programmer * Make the conversion from the standard API to the class library as simple as possible * Allow hybrids to use traditional functions calls and new class libraries * in design classes When the library is considering the function and efficiency * The class library must be conveniently ported in different platforms, such as Windows 95 and Windows NT Design Groups Feel the development of high quality code must start from the MFC library itself. The C basic library must be small and fast. Its simplicity makes it easy to use, while the speed is close to the huge C function library. These classes should make skilled Windows programmers do not have to re-learn the names of various functions. This can be achieved by careful naming and design. Microsoft believes this is a feature of the MFC from other class libraries. The MFC team also designs the underlying class library to be allowed to be programmed in a mix. That is to say, in the same source file, you can use the class or the traditional function call. Even when using the MFC, a function like setCursor () and getSystemMetrics () is still required. Microsoft also knows that the library must be easy to use. Some types of libraries provided by other vendors are too abstract. According to Microsoft's statement, these cumbersome companies have a large and slow application. The MFC library provides reasonable abstraction and ensuring that the code is small. The development team designs the original MFC library as dynamic rather than static. The dynamic structure is that these classes can accommodate the Windows 95 and Windows NT environments we use now.
22.3 The key features of the MFC library can also get a Windows class library from other compiler vendors, but Microsoft claims that their MFC class libraries have many real advantages: * Fully support all Windows functions, controls, messages, gdi (graphics device interface Drawing primitives, menus, and dialogs. * Use the same naming agreement as Windows API. Therefore, you can directly know the functions of the class from the name. * Eliminate an error source, a large number of Switch / CASE statements. All messages are mapped to the members of the class. This message-method mapping method is applied to all messages. * Ability to output objects to files, which provides better diagnostic support. It also provides the ability to verify a member variable. * Enhanced exception processing design makes the probability code failure. Can solve the "insufficient memory" and other problems. * You can determine the type of data object at runtime. This allows for dynamic manipulation of classifications. * Small and fast code. As mentioned earlier, the MFC library only adds a lot of code, which is actually just as fast as the traditional C language Windows application. * Support for Component Object Models (COM). Experienced Windows programmers immediately like two of these features: familiar naming conventions and messages - method mapping mechanisms. If you re-check the source code for the application developed in the 21st chapter, you will see a large number of Switch / Case statements for handling errors. It should also be noted that these applications call a large number of API functions. When you use the MFC library, these two phenomena disappear or decreased. Professional programmers will definitely enjoy a better diagnosis and small code implemented in the MFC library. The programmer can now use the benefits of the MFC library without having to worry about their application's code size. Finally, the MFC is the only truly useful class library. 22.4 Everything starts from the COBJECT class similar to MFC usually comes from a few base classes. The additional class can then be inherited from these base classes. COBJECT is a base class that uses a lot of use when developing a Windows application. The MFC library head file provided in the MFC / Include subdirectory includes many class definition information. Let's take a brief look, COBJECT, it is on the header file AFX.
H in the definition: ///// class CObject is the root of all compliant objectsclass CObjectpublic: // Object model (types, destruction, allocation) virtual CRuntimeClass * GetRuntimeClass () const; virtual ~ CObject (); // virtual destructors Are Necessary // Diagnostic Allocations Void * Pascal Operator New (Size-T); Void * Pascal Operator New (Size-T, Void * P); Void Pascal Operator Delete (VOID * P); # if defined (-debug) &&! defined (-af File Name / Line Number TRACKING Using Debug-New Void * Pascal Operator New (Size-T Nsize, LPCSTR 1PSZFILENAME, INT NLINE); // Disable The Copy Constructionor and assignment by default // so you will get compiler errors instead of unexpected // behavior if you pass objects by value or assign objects.protected: CObject (); private: CObject (const CObject & objectSrc); // no implementation void operator = (Const Cobject & ObjectSrc); // AttributeSpublic: BOOL ISSERIALIZABLE () Const; Bool Iskindof (const cruntimeclass * pclass) const; // over ridables virtual void Serialize (CArchive & ar); // Diagnostic Support virtual void AssertValid () const; virtual void Dump (CDumpContext & dc) const; // Implementationpublic: static const AFX-DATA CRuntimeClass classCObject; # ifdef-AFXDLL static CRuntimeClass * PASCAL- GetBaseClass (); # Endif; For the sake of clarity, some subtle changes have been made to this code. But the code you can find on the header file AFX.H is basically the same. Check the code of the COBJECT, pay attention to constitute the ingredients defined by this class. First, COBJECT is divided into three parts: public, protected and private. COBJECT also provides general and dynamic type inspections and serialization features. Memolive, dynamic type check allows you to determine the type of object at runtime.
With permanent concepts, the status of the object can be saved to the storage medium, such as a disk. The permanent object of the object can make the object member function can be permanent, allowing the object data to recover. Subclasses are inherited from the base class. For example, CGDIObject is a class that inherits from the COBJECT class. Here is AFXWIN. The CGDIObject class definition found in H. Similarly, for the sake of clarity, some changes were made. //// CGdiObjet abstract class for CDC SelectObjectclass CGdiObject: public CObject DECLARE-DYNCREATE (CGdiObject) public: // Attributes HGDIOBJ m-hObject; // must be first data member operator HGDIOBJ () const; static CGdiObject * PASCAL FromHandle (HGDIOBJ hObject); static void PASCAL Delete TempMap (); BOOL Attach (HGDIOBJ hObject); HGDIOBJ Detach (); // Constructors CGdiobject (); // must create a derived class object BOOL DeleteObject (); // Operations int GetObject (int nCount, LPVOID 1pObject) const; UINT GetObjectType () const; BOOL CreateStockObject (int nIndex); BOOL UnrealizeObject (); BOOL operator == (const CGdiObject & obj) const;! BOOL operator = (const CGdiObject & obj) const; // Implementationpublic : Virtual ~ cgdiobject (); # ifdef-debug virtual void dump (cdumpContext & dc) const; virtual void assertvalid () const; #ENDIF; CGDIObject and its member function Allows creation and use painting objects in a Windows application, such as self Define brushs, brushes, and fonts. Classes such as CPEN are further inherited from the CGDiObject class. Microsoft provides all source code for the MFC library to increase programming flexibility as much as possible. However, for beginners, there is no need to understand how different classes are defined. For example, in a conventional C language Windows application, the deleteObject () function calls the following syntax: deleteObject (hbrush); / * Hbrush is the brush handle * / in C , using the MFC library, you can use the syntax of the following The class member function is the same purpose: newbrush.deleteObject (); // new brush is current brush As you can see, call the steering class library object from the C language Windows function is simple.