A generic instance application

xiaoxiao2021-03-06  105

This article is translated from MSDN, because the English level is general, if there is any error, please actively pointed out that I must change it. Original link: http://msdn.microsoft.com/library/en-us/winprog/winprog/a_generic_sample_application.asp This article is suitable for beginners for Win32 API a Generic Sample Application A generic instance application

This section introduces the source code components of a generic Windows-based application, named Generic. This section assumes that you have used Windows-based applications and therefore are already familiar with windows, menus, and dialog boxes in Microsoft? Windows ?. this section This section describes a component of the source code of Windows, and our program is called generic. We assume that you have already used Windows-based applications, so you should be more familiar with the windows in Windows, menus and dialogs.

The Generic Application Described Here Consists of the Following Parts: The generic application described here consists of the following parts:

THE Entry-Point Function Entry Function The Menu Menu The Window Procedure Window Process The About Dialog Box About dialog

The complete code is listed in the "Source Code" section of the Complete Code.

The Entry-Point Function Entrance Function

Every application must have an port function. Usually the name of the entry point is WinMain.

AS IN MOST Windows-based Applications, The Winmain Function for the Generic Application Completes The Following Steps: Like most Windows-based applications, the Winmain functions in the generic application complete the following steps:

Registering the window class registration window CREANG THE MIN WINDOW Creating the main window Entering the message loop Enters the message loop

Registering the window class registration window classes

Every window must have a window class. A window class defines the attributes of a window, such as its style, its icon, its cursor, the name of the menu, and the name of the window procedure. Each window must have a Window class. This window class defines an attribute of a window, such as its style, icon, cursor pointer, menu name and window process.

The first step is to fill in a WNDCLASS structure with class information Next, you pass the structures to the RegisterClass function The Generic application registers the GenericAppClass window class as follows:.. The first step is to use the information to fill a class WNDCLASS structure. Next, pass this structure to the RegisterClass function. The generic application registers the GenericAppClass window class in accordance with the following code: Hinstance Hinstance; WNDCLASS WC;

wc.lpszClassName = "GenericAppClass"; wc.lpfnWndProc = MainWndProc; wc.style = CS_OWNDC | CS_VREDRAW | CS_HREDRAW; wc.hInstance = hInstance; wc.hIcon = LoadIcon (NULL, IDI_APPLICATION); wc.hCursor = LoadCursor (NULL, IDC_ARROW ); wc.hbrbackground = (hbrush) (color_window 1); wc.lpszMenuname = "genericappmenu"; wc.cbclsextra = 0; wc.cbwndextra = 0;

RegisterClass (& WC);

For more information on the menu. For more information on the menu side. For more information on the window process, please refer to "The Window Procedure".

Creating the main window Creating a main window

You can create the window by calling the createwindow function. The generic application creates: You can create a window by calling the CREATEWINDOW function. The Generic application creates a window in accordance with the following code:

HWND = CREATEWINDOW ("GenericAppClass", "Generic Application", WS_OVERLAPPEDWINDOW | WS_HSCROLL | WS_VSCROLL, 0, 0, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, HINSTANCE, NULL;

The first parameter is the name of the class that we registered. The remaining parameters specify other window attributes. This call creates the window, but the system does not display a window until the application calls the ShowWindow function. The Generic application displays the window as FOLLOWS: The first parameter is the name of our registered window class. The remaining parameters are used to specify other window properties. This function call creates a window, but the system does not display it unless you call the showWindow this function. The generic application displays the window on the screen in accordance with the following code: ShowWindow (HWND, NCMDSHOW);

Entering the message loop enters the message loop

Once the main window is created and displayed, the WinMain function can begin its primary task, which is to read messages from the application queue and dispatch them to the appropriate window. Once the main window has been created and displayed, WinMain function can start it The main task, that is, read the message from the application's message queue and sends it to the appropriate window.

The system does not send input directly to an application. Instead, it places all mouse and keyboard input from the user into a message queue, along with messages posted by the system and other applications. The application must read the message queue, retrieve the messages And Dispatch Them So That The Window Procedure CAN Process The WINDOW Procedure CAN Process The system does not send the input directly to an application. Instead, it puts the mouse from the user to the keyboard into a message queue, together with the message sent by the system and other applications. The application must read messages from the message queue to retrieve and send them to the window process, which is to make the window process can handle them.

The generic application uses The Following Message Loop: Generic Application Use the following message loop:

BOOL BRET;

While ((Bret = GetMessage (& MSG, NULL, 0, 0))! = 0) {if (Bret == -1) {// Handle The Error and Possibly EXIT} else {TranslateMessage (& MSG); DispatchMessage (& MSG) }}

The GetMessage function retrieves a message from the queue. The DispatchMessage function sends each message to the appropriate window procedure. The TranslateMessage function translates virtual-key message into character messages. In Generic, this is necessary to implement menu access keys.GetMessage function from the message Get a message in the queue. The DispatchMessage function sends each message to the appropriate window procedure. The TranslateMessage function converts the virtual key message into a character message. General, this is to achieve the need for menu shortcuts. The Menu Menu

Most applications include a menu to provide a means for the user to select commands. The most common way to create a menu is to define it as a resource in the resource-definition file. The Generic application has a single menu, named Help, with A Single Command, About. The Resource IS Defined As Follows: Most applications contain a menu to provide a means to the user selection command. The most common method for creating a menu is to define a menu resource in the resource definition file. The generic application has only one menu called Help; this menu has only one command, About. This resource is defined in accordance with the following code:

GenericAppMenu Menu {popup "& help" {menuitem "& itt", IDM_about}}

The name of the menu resource is specified when the menu resource is specified in the window class when the registration window class is registered.

Selecting The About Command Causes Generic To Display The About Dialog Box. Select the About Command will make the Generic application to display the About dialog on the screen.

The Window ProCedure window process

Every Window Must Have A Window Procedure. The Name of The Window Procedure IS User-Defined. The Generic Application Uses The Following Window Procedure for the Main Window: Each window must have a window process. The name of the window process is user-defined. Generic Application Use the following window process as the window process of the main window:

Lresult WinApi MainWndProc (HWND HWND, UINT UMSG, WPARAM WPARAM, LPARAM LPARAM);

The WinApi Modifier IS Used Because The Window Procedure Must Be deClared with the Standard Call Calling Convertion. It is because the window process must be declared as a standard CALL call agreement.

The window procedure receives messages from the system. These may be input messages or window-management messages. You can optionally handle a message in your window procedure or pass the message to the system for default processing by calling the DefWindowProc function. The Generic application processes The WM_Paint, WM_COMMAND, And WM_DESTROY Messages, Using A Switch Statement That IS STRUCTED AS FOLLOWS: Window procedure receives messages from the system. These messages may be the user's input message, or the window management message. You can select a message during the window, or pass the message to the system as the default process by calling the DEFWINDOWPROC function. Generic application handles WM_Paint, WM_COMMAND with WM_DESTROY messages, uses Switch Conditions statements to construct these message processing as follows: Switch (umsg) {Case WM_Paint: ... case wm_command: ... case wm_destroy: ... Default : Return (DefWindowProc (HWND, UMSG, WPARAM, LPARAM);

The WM_PAINT message indicates that you should redraw what's in all or part of your application's window. Use the BeginPaint function to get a handle to a device context, then use the device context for drawing within the application's window, with functions like TextOut. Use EndPaint To Release The Device Context. The Generic Application Displays A Text String, "Hello, World!", in the window Using the FOLLOWING CODE: WM_PAINT Message Indicates that you should scrute over the entire application window or some of them. Use the BeginPaint function to get the handle of a device descriptor, and then use the device description of the table sector and Textout to draw in the application window. Finally, use the EndPaint function to release the device descriptor. The generic app uses the code to display a string "Hello, World!" In the window:

Case WM_Paint: HDC = BeginPaint (HWND, & PS);

Textout (HDC, 10, 10, "Hello, World!", 13);

Endpaint (hwnd, & ps); Break;

The WM_COMMAND message indicates that a user has selected a command item from a menu The Generic application uses the following code to check if its About menu item has been selected:. WM_COMMAND message indicates that the user has selected a command option from the menu. The Generic application uses the following code to determine if the item menu item is already selected: Case WM_Command: Switch (wparam) {CASE IDM_ABOUT: ... BREAK;}

Most window procedures process the WM_DESTROY message. The system sends this message to the window procedure immediately after destroying the window. The message gives you the opportunity to finish processing and post a WM_QUIT message in the application queue. The Generic application handles the WM_DESTROY message as FOLLOWS: Most window processes the message of WM_DESTROY. The system will immediately send this message to the window process after the window is cleared. This message allows you to have a chance to complete some data processing, and put the WM_QUIT message into the application's message queue. The Generic application handles the WM_DESTROY message according to the following code:

Case WM_DESTROY: POSTQUITMESSAGE (0); BREAK;

The About Dialog BoxAbout dialog

A dialog box is a temporary window that displays information or prompts the user for input. The Generic application includes an About dialog box. Every application should include an About dialog box. The dialog box displays such information as the application's name and copyright information. Dialogue The box is a temporary window that is used to display information or prompt user input. The Generic application contains an About dialog. Each application should contain an About dialog. It is generally used to display some information, such as the name of the application, and copyright information.

You Create and Display a Dialog Box by Using The Dialogbox Function. This function and create the Dialog Box. You can create and display a dialog with a Dialogbox function. This function uses a dialog template to create a dialog.

The generic application incrudes The Following Dialog Box Template In The Resource-Definition File: Generic Application In the Resource Definition file contains the following dialog templates:

AboutDlg DIALOG FIXED 6, 21, 198, 99STYLE DS_MODALFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENUCAPTION "About Generic" FONT 8, "MS Shell Dlg" BEGIN DEFPUSHBUTTON "& OK", IDOK, 72, 74, 40, 14 LTEXT "Generic Application ", 104, 45, 14, 128, 8 LText" Written As a Sample ", 105, 45, 35, 59, 8 LText" Microsoft Corporation ", 106, 45, 45, 98, 8 LText" Copyright (C) 2004 ", 107, 45, 54, 138, 8endthe name of the source isot, the name of the menu resource is specified as Aboutdlg. For more information on this, please refer to "Dialog Resource".

When a User ClickS About from the help menu

Hinstance Ghinstance; hwnd hwnd;

Case WM_COMMAND: Switch (wparam) {Case IDM_About: DialogBox (Ghinstance, "Aboutdlg", hwnd, aboutdlgproc); BREAK;

The Last Parameter Is A Pointer to a Dialog Box Procedure. It has the folding prototype. The last parameter is a pointer to the dialog process. The prototype of this function is as follows:

INT_PTR WINAPI ABOTDLGPROC (HWND, UINT, WPARAM, LPARAM);

A dialog box procedure is similar to a window procedure, but usually processes only dialog initialization and user-input messages The Generic application contains the following message processing code:. A process similar to a dialog window procedure, but it is often only dialog processing Initialization and user input messages. The dialog process in the generic application contains the following message processing code:

UINT UMSG;

Switch (UMSG) {Case WM_INITDIALOG: RETURN TRUE; Case WM_Command: Switch (wparam) {Case IDOK: EndDialog (HDLG, TRUE); Return True;} Break;} Return False;

Source Code Source Code

The generic application consists of the full Files: Generic application consists of the following files:

Generic.c generic.h generic.rc generic.dlg

Generic.cGeneric.c Contains Code for the generic application. It incduDes generic.h.Generic.c file contains the source code for the Generic application. It contains header file generic.h.

/ ************************************************** ******************* / * generic.c: source code for generic ** ** Comments: generic application ** ** functions: ** Winmain - Application Entry Point ** mainwndproc - main window procedure ** AboutdlgProc - Dialog procedure for About Dialog ** ** * / ******************************************** *************************************** /

/ ******************** Header files ***************************** /

#include #include "generic.h"

/ ******************** Prototypes ********************** /

Lresult WinApi MainwndProc (HWND, UINT, WPARAM, LPARAM); LRESULT WINAPI AboutdlgProc (HWND, UINT, WPARAM, LPARAM)

/ ******************* GLOBAL VARIABLES **************************** /

Hinstance ghinstance;

/ ************************************************** ****************** / * Function: int Pascal Winmain (Hinstance, Hinstance, LPSTR, INT) ** ** purpose: Initializes Application ** ** Comments: register Window class, create and display the main ** window, and enter message loop. ** ** * / ************************************* ********************************************************************** / INT PASCAL WINSTANCE (Hinstance Hinstance, Hinstance HPREVINSTANCE, LPSTR LPSZCMDLINE, INT NCMDSHOW) {WNDCLASS WC; MSG Msg; HWND HWND; BOOL BRET;

if (hPrevInstance!) {wc.lpszClassName = "GenericAppClass"; wc.lpfnWndProc = MainWndProc; wc.style = CS_OWNDC | CS_VREDRAW | CS_HREDRAW; wc.hInstance = hInstance; wc.hIcon = LoadIcon (NULL, IDI_APPLICATION); wc.hCursor = LOADCURSOR (NULL, IDC_ARROW); wc.hbrbackground = (Hbrush) (color_window 1); wc.lpszMenuname = "genericappmenu"; wc.cbclsextra = 0; wc.cbWndextra = 0;

RegisterClass (& WC);

GHINSTANCE = Hinstance;

HWND = CREATEWINDOW ("GenericAppClass", "Generic Application", WS_OVERLAPPEDWINDOW | WS_HSCROLL | WS_VSCROLL, 0, 0, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, HINSTANCE, NULL;

ShowWindow (HWND, NCMDSHOW);

While ((Bret = GetMessage (& MSG, NULL, 0, 0))! = 0) {if (Bret == -1) {// Handle The Error and Possibly EXIT} else {TranslateMessage (& MSG); DispatchMessage (& MSG) }} Return (int) msg.wparam;}

/ ************************************************** ****************** / * Function: Lresult Callback MainWndProc (HWND, UINT, WPARAM, LPARAM) ** ** PURPOSE: Processes Application Messages ** ** Comments: THE FOLLOWING Messages Are Processed ** ** WM_Paint ** WM_Command ** WM_DESTROY ** ** * / **************************************** *************************************** /

Lresult Callback MainWndProc (HWND HWND, UINT MSG, WPARAM WPARAM, LPARAM LPARAM) {PAINTSTRUCT PS; HDC HDC;

Switch (msg) {

/ ************************************************** ************ / * WM_Paint: * / *************************************** ****************************** /

Case WM_Paint: HDC = BeginPaint (HWND, & PS);

Textout (HDC, 10, 10, "Hello, World!", 13);

Endpaint (hwnd, & ps); Break;

/ ************************************************** ************ / * WM_COMMAND: * / *************************************** **************************************** / CASE WM_COMMAND: SWITCH (WPARAM) {CASE IDM_ABOUT: DialogBox (Ghinstance, "Aboutdlg" , hwnd, (dlgproc) AboutdlgProc); Break;} Break;

/ ************************************************** ************ / * WM_DESTROY: postquitmessage () IS Called * / ************************************* ******************************************* /

Case WM_DESTROY: POSTQUITMESSAGE (0); BREAK;

/ ************************************************** ************* / * Let the default window proc Handle All Other Messages * / ********************************** *********************************************** /

Default: Return (DEFWIndowProc (HWND, MSG, WPARAM, LPARAM);

Return 0;}

/ ************************************************** ****************** / * Function: LResult Callback AboutdlgProc (HWND, UINT, WPARAM, LPARAM) ** ** PURPOSE: Processes "About" Dialog Box Messages ** ** Comments: The About Dialog Box Is Displayed when The user clicks ** About from the help menu. ** * / ************************************** ************************************************* /

LRESULT CALLBACK AboutDlgProc (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) {switch (uMsg) {case WM_INITDIALOG: return TRUE; case WM_COMMAND: switch (wParam) {case IDOK: EndDialog (hDlg, TRUE); return TRUE;} Break;} Return False;

Generic.hGeneric.h Contains Header Information for the generic application. It is incr.Generic.c and generic.rc.generic.h file contains the title information of the generic application. It is included in Generic.c with generic.rc files.

/ ************************************************** *********** / * generic.h: header file for generic ** ** * / ****************************** *************************************** /

/ ******* menu defines ******* /

#define IDM_About 1000

Generic.rcGeneric.rc Contains Resource Information. The Dialog Resources Are INCLUDED IN Generic.dlg.Generic.rc file contains the resource information of the generic application. The dialog resource is included in the generic.dlg file.

/ ************************************************** *********** / * generic.rc: resource script for generic ** ** * / ****************************** *************************************** /

#include #include "generic.h" #include "generic.dlg"

GenericAppMenu Menu {popup "& help" {menuitem "& itt", IDM_about}}

Generic.dlgGeneric.dlg defines The About Dialog Box for the generic application. This file is incroducted in Generic.rc.Generic.dlg file defines the About dialog in the Generic application. This file is included in the generic.rc file. / ************************************************** *********** / * generic.dlg: dialogs for generic ** ** * / ******************************* *********************************************** /

1 DLGINCLUDE "generic.h"

AboutDlg DIALOG FIXED 6, 21, 198, 99STYLE DS_MODALFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENUCAPTION "About Generic" FONT 8, "MS Shell Dlg" BEGIN DEFPUSHBUTTON "& OK", IDOK, 72, 74, 40, 14 LTEXT "Generic Application ", 104, 45, 14, 128, 8 LText" Written As a Sample ", 105, 45, 35, 59, 8 LText" Microsoft Corporation ", 106, 45, 45, 98, 8 LText" Copyright (C) 1996 ", 107, 45, 54, 138, 8und

转载请注明原文地址:https://www.9cbs.com/read-103364.html

New Post(0)