Start writing Win32 Application programs

xiaoxiao2021-04-08  4

First, start writing Win32 Application programs today

Int Winapi WinMain

Hinstance Hinstance, // Handle to Current Instance

Hinstance Hprevinstance, // Handle to Previous Instance

LPSTR LPCMDLINE, / / ​​POINTER TO COMMAND LINE

Int ncmdshow // show State of window

);

This is the main function, the default portable function based on Win32 application.

Parameters: Hinstance: Handle of the current instance of the application

HPREVINSTANCE: The handle of an instance of the application, for WIN32-based programs, this parameter is empty.

If you need to detect if there is another instance exist, call the CreatMutex function to create a unique mutual exclusive naming, even if a mutually exclusive naming already exists, CreatMutex will succeed, but the getLastError function will return ERROR_ALREADY_EXISTS, this is to say another application The example of the program already exists because it creates a mutex.

LPCMDLINE: In the command line program, point to a string ended with empty, does not contain the program name. Retrieve the entire command line and use the getcommandline function.

NCMDSHOW:: Specify how the window is displayed, the parameter can be the following value:

value

Significance

SW_HIDE hides the window to activate another window SW_MINIMIZE minimizes the specified window, activating the top window SW_RESTORE in the system list activates and displays the window. If the window minimizes or maximizes, return the window to the initial size and initial position. SW_SHOW activates the window in the current size and location. SW_SHOWMAXIMIZED

. . . . . .

return value:

If the function is successful, and when it is received, it is terminated when it receives the WM_PAINT message, it should return an exit value in the WPARAM parameter included in the message. If the function terminates before entering the message cycle, you should return 0.

Note: To use the message mechanism for Windows.

Second, the step of creating an app

Firstly, a window class is designed, then register the window class, then create the window, last display and update.

Design a window class;

2. Register the window class;

3. Create a window;

4. Display and update the window.

The process of designing window classes defines a WNDCLASS object to assign a value for the properties of the object. as follows:

WNDCLASS information:

This is a structure that contains the properties of the window class, which can be registered using the RegisterClass function.

The structure has been replaced by the WndClassex function, and the registration function is also replaced by the RegisterClassex function. The difference is that WNDCLASS and REGISTERCLASS can be continued when the small icons related to the window class can continue.

The structural definition is as follows:

Typedef struct _wndclass {

Uint style; // Specify the type of style. You can use | Connect a few styles.

WndProc LPfnWndProc; // Windows process pointer, you must use the CallWindowPro function to call the Window process.

INT CBCLSEXTRA; / / Specify an additional byte number of allocations followed by the Window class

INT CBWndextra; / / Specifies the number of additional bytes that are allocated later after the Window instance.

Handle hinstance; // instance handle of the Window program in this class. The icon of the Hicon Hicon; // The icon of this class needs to be an icon resource handle, if it is empty, the application must draw an icon when the user minimizes the window.

Hcursor hcursor; // cursor

Hbrush hbrbackground; // background handle

LPCTSTR LPSZMENUNAME; / / Pointer to the resource name of a class menu.

LPCTSTR LPSZCLASSNAME; / / pointing to a string or a atomic pointer.

} WNDCLASS;

RegisterClass registers a window class

The registration class function registers a window class so that the next step is used in CreatWindow or CreatWindowEx.

The registration class function is now replaced by the RegisterClassex function. You can continue to use the function if you do not have to use a small icon.

Atom RegisterClass

Const Wndclass * Lpwndclass // Address of Structure with Class Data

);

Parameters: lpwndclass: Pointer for WNDCLASS structure. Fill in the appropriate properties before passing.

Return Value: If the function is successful, the return value is one atom value, its unique logo registration class. If it fails, return 0 ,. You can call the getLastError value to see the error message.

Atom is actually a word, there is an ATOM table, which points to a reference to a string in this table.

CREATEWINDOW

Create a window and return the value as a window handle.

HWND CREATEWINDOW

LPCTSTR LPCLASSNAME, / / ​​POINTER TO Registered Class Name

LPCTSTR LPWINDOWNAME, / / ​​POINTER TO WINDOW NAME

DWORD DWSTYLE, / / ​​WINDOW STYLE

Int x, // horizontal position of window

Int Y, // Vertical Position of Window

INT NWIDTH, / / ​​WINDOW WIDTH

INT NHEIGHT, // WINDOW HEIGHT

HWND HWNDPARENT, // Handle to Parent or Owner Window

Hmenu Hmenu, // Handle to Menu Or Child-Window Identifier

Handle Hinstance, // Handle to Application Instance

LPVOID LPPARAM / / POINTER TO WINDOW-CREATION DATA

);

This function creates an overlapped, pops up or sub-windows. It specifies the title, style, and initial position and size of the window (optional). This function also specifies the parent window of the window (if any), and the menu of the window.

HMENU: Points to MENU, or specifies a sign of a child window flag (depending on the Window style)

Hinstance: The handle of the module instance related to the window.

LPPARAM: Parameters passed in the WM_CREAT message pass through the pointer of the value of the window through the CreatStruct structure.

Return Value: If successful, return a new window handle. Returns NULL if it fails. Note: Before returning, the function sends a WM_CREATE message to the Window process.

Display and refresh window showWindow () showwindow (hwnd, sw_shownormal);

UpdateWindow (HWND);

Third, transfer news

After displaying the window, you should distribute and process the messages within the window.

First define the object of the message.

MSG

The message MSG is a structure that contains messages from a message queue from a process.

Typedef struct tagmsg {// msg

HWND HWND; // Receive the serial handle of the message

Uint message; // Message Number

WPARAM WPARAM; // Specify the extra information of the message, accurately means that Message members

LParam lparam; // is also an additional information of the specified message, which is the same.

DWORD TIME; // The time emitted.

Point pt; // The position of the pointer is the coordinate system,

} Msg;

GetMessage ()

got the message

This function reads the message from the calling thread message queue and places it in the specified structure. It can read and specify messages related to the specified window, and the thread messages passed through the PostthreadMessage function. This function reads messages located within the specified range. It does not get messages that belong to other processes.

Bool getMessage

LPMSG LPMSG, // Address of Structure with Message

HWND HWND, // Handle of window

UINT WMSGFILTERMIN, / / ​​FIRST MESSAGE

Uint WMSGFILTERMAX // Last Message

);

LPMSG: Gets a message from the process message, and placed an MSG structure that points to the pointer.

HWND: Window handle, the message is obtained from the window. The NULL value has a special meaning, indicating that the function acquires the message from all windows belonging to this process and gets messages in the process passed by calling PostthReadMessage.

WMSGFILTERMIN: Minimum message, integer

WMSGFILTERMAX: Maximum Message, Integer

Return Value: If the function gets a non-WM_PAINT message, the return value is not 0

If it is wm_paint, return 0.

Return -1 if an error

TranslateMessage

Translation message

Bool TranslateMessage

Const msg * lpmsg // address of structure with message

);

This function translates the virtual key value into a character message. The character message is sent to the message queue that calls the process, and is read when the process is called next time.

The only parameter lpmsg is an MSG pointer,

Return Value: If the message is translated (ie, the character message is transmitted to the process message queue) returns non-0; or if the message is WM_KeyDown, WM_Keyup, WM_SYSKEYDOWN,

When WM_SYSKEYUP, no matter whether the translation is returned; if there is no translation, return 0.

DispatchMessage

Distribute news

This function distributes the message to the window process. Typical messages for getting through the getMessage function. Long DispatchMessage

Const msg * lpmsg // Pointer to Structure with Message

);

Parameters lpmsg: Pointer to the MSG structure.

Return Value: The value returned for the window process. It is often ignored.

Remarks: The MSG structure must contain legal message values. If the LPMSG parameter points to a WM_TIMER message, and the WM_TIMER's LPARAM parameter is not null, the LParam points to a called function rather than the Window process.

Third, write window handling functions WindowProc

The WindowProc function is a function that is defined by an application that handles messages sent to the window. The WndProc type defines a pointer that calls the return function. WindowProc is a placeholder for the application defines a function name.

LResult Callback WindowProc

HWND HWND, // Handle to Window

UINT UMSG, // Message Identifier

WPARAM WPARAM, // First Message Parameter

LParam lParam // Second Message Parameter

);

parameter:

HWND window handle

UMSG specified message

WPARAM specifies additional information for messages. The content depends on the value of the UMSG parameter.

lparam

Return Value: The return value is the result of the message processing, depending on the message sent.

During the process of processing the message, be sure to write a default situation in the last Switch () (this is also noted in all switch ()). There is also a WM_DESTROY message,

Case WM_DESTROY:

PostquitMessage (0);

Break;

DEFAULT:

Return DefwindowProc (HWND, UMSG, WPARAM, LPARAM);

This time, there is some problem when the menu is displayed.

When designing window classes, many places are directly assigned,

WNDCLASS.LPSZMENUNAME = IDR_MENU1;

But sometimes problems, so you need to use the following way

Wndcls.lpszMenuname = makeintResource (iDR_Menu1);

MakeintResource (): This is a macro, the parameter is an integer, corresponding to the resources corresponding to the resources, and a 32-bit application resource.

The menu command message belongs to WM_Command. That is, the process menu command is required.

Case WM_COMMAND: Processing, judge which menu is clicked, with lparam parameters,

Case WM_COMMAND:

{

Switch (WPARAM)

{

Case idm_exit:

IF (iDyes == MessageBox (hwnd, "really close", "guanbi", mb_yesno))

{

DestroyWindow (hwnd);

}

}

}

Break;

All menu command processing is in this.

Below is the problem that appears during the process of writing:

1. After closing the program, you can't completely close the process.

The wrong report is as follows:

Link: Fatal Error LNK1168: Cannot Open Debug / Winmain.exe for WritInGror Executing Link.exe.

This is because the code may be as follows when the message loop is:

While (GetMessage (& MSG, Hwnd, 0)) {TranslateMessage (& MSG); DispatchMessage (& MSG);} Note: The second parameter of getMessage is as follows

HWND HWND, // Retrieves the window of the message, if null, refers to all windows. Therefore, if it is not set to NULL, the message is still retrieved after the program is still retrieved, so the county town still exists. The solution to this question is to set this parameter to NULL.

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

New Post(0)