/*--
014 Programming Guru Win32 API daily practice.
The 14th example is wm destroyThe C:** function --- process WM destroy messages.
wm destroy message.
postquitmessage function.
Note: After closing the window, the program exits.
c) www.bcdaren.com coding whiz.
#include
Functions***
lresult callback wndproc(hwnd, uint, wparam, lparam);Message function.
*Program Entry**
int winapi winmain(hinstance hinstance, hinstance hprevinstance,lpstr lpcmdline, int ncmdshow)
Omitted) return msgwparam;From a message indicating an exit, this value is returned to the system.
Functions - Message processing***
Window function.
lresult callback wndproc(hwnd hwnd, uint message, wparam wparam, lparam lparam)
hdc hdc;Device environment – where the drawing is made.
paintstruct ps;Drawing struct variables.
rect rect;The extent of the plot area.
switch (message)
case wm create: Creates a window message.
return 0;
break;You can also.
case wm paint: Drawing function, drawing on a window!
hdc = beginpaint(hwnd, &ps);
Get the client area size.
getclientrect(hwnd, &rect);
Draw strings.
textout(hdc, 200, 200, text("Ida!"), lstrlen(text("Ida!")))
Draw an elliptical shape.
ellipse(hdc, 250, 250, 1200, 500);
Draw formatted text with the middle of the client area aligned vertically and centered.
drawtext(hdc, text("My first window program!"), 1, &rect,dt_center |
dt_vcenter | dt_singleline);
endpaint(hwnd, &ps);
break;
case wm destroy: Handles exit messages.
Draw an elliptical shape.
ellipse(hdc, 250, 250, 1200, 500);Test.
postquitmessage(0);This message goes directly to the head of the message queue!By default, the defwindowproc function calls the destroywindow function to destroy the window.
break;
default: Can be placed at the end.
return defwindowproc(hwnd, message, wparam, lparam);
return 0;
The default window procedure is invoked to provide default processing for any window messages that are not processed by the application.
return defwindowproc(hwnd, message, wparam, lparam);
*NOTE***
WM DESTROY message: After the window is destroyed (after the destroywindow() call is called), the WM DESTROY message is added to the message queue.
Process: After the user clicks the close program button, the message queue adds a message WM close, and then the program takes WM close from the message queue, and the default processing method of WM close messages is to call destroywindow() to send a WM destroy message to the window, and the message queue increments WM destroy, the main program message loop is taken out again and handed to the Windows system, the Windows system calls the window procedure to process the WM DESTROY message, the processing method is to call ostquitMessage(), add the WM quit flag to the message queue, the message loop obtains the WM quit and exits the message loop, and the program exits.
If the window procedure does not handle the WM close message, by default, the default window procedure defwindowproc function is also called the destroywindow function.
PostquitMessage function: Indicates to the system that the thread has requested termination (exit). It is typically used in response to a WM DESTROY message.
void postquitmessage(
int nexitcode is an integer value. This exit code is usually used to indicate the result or status of the program's execution. This value is used as the wparam parameter of the WM quit message.
Remark. The postquitmessage function adds a WM quit exit flag to the thread's message queue and returns immediately;This function simply indicates to the system that this thread is requesting an exit at some point in the future.
When a thread retrieves the WM quit flag from its message queue, it should exit its message loop and return control to the system. The exit value returned to the system must be the wparam parameter of the WM quit message.
Some books treat wm quit as a message, insert it into the message queue header, and then exit immediately, the remaining messages in the message queue will not be processed, although it is more vivid, but it is not very rigorous, wm quit is actually an exit marker. )。
*NOTE***1 In this example, after closing the window, you can finally exit the program normally. The reason for this is that the window process wndproc processes the WM DESTROY message and adds a WM quit exit flag to the window message queue. The getmessage function of the message loop returns a value of 0 after obtaining the wm quit, exiting the message loop and exiting the process.
2 Hands-on Lab: Add a function to draw an ellipse in the WM Destroy message processing module
ellipse(hdc, 250, 250, 1200, 500);
Compiler prompts: error c4700: The uninitialized local variable "hdc" is used.
Error cause: The HDC set during the window process is a local variable that is used in the WM Paint message module, but cannot be recognized in the WM Detroy module, indicating that the local variable cannot be used across message modules during the window process, and the scope is limited to one message module. Remember that the iceberg below the surface of the water is not implemented by us, Windows programs are message-driven programs.
Solution: Define the variable as a static hdc hdc; in the global regionIf a variable needs to be used across message modules, define it as a static variable. However, it is not recommended to define HDC as a static variable here, because HDC will continue to occupy a large amount of memory space, and you can take it as you go.
At the breakpoint of the ellipse function, when the window is closed, the window will not be displayed when the ellipse function is called. The reason is simple, because the window is no longer there.
015 Programming Guru Win32 API daily practice.
The 15th example is WM CloseThe c:** function --- handle WM close messages.
WM close message.
destroywindow function.
Note: After closing the window, the program exits.
c) www.bcdaren.com coding whiz.
#include
lresult callback wndproc(hwnd, uint, wparam, lparam);Message function.
*Program Entry**
int winapi winmain(hinstance hinstance, hinstance hprevinstance,lpstr lpcmdline, int ncmdshow)
static tchar szappname = text("hellowin");The name of the window class.
Omitted) return msgwparam;From a message indicating an exit, this value is returned to the system.
Functions - Message processing***
Window function.
lresult callback wndproc(hwnd hwnd, uint message, wparam wparam, lparam lparam)
hdc hdc;Device environment – where the drawing is made.
paintstruct ps;Drawing struct variables.
rect rect;The extent of the plot area.
switch (message)
case wm create: Creates a window message.
return 0;
case wm paint: Draw on a window!
hdc = beginpaint(hwnd, &ps);
Get the client area size.
getclientrect(hwnd, &rect);
Draw strings.
textout(hdc, 200, 200, text("Ida!"), lstrlen(text("Ida!")))
Draw an elliptical shape.
ellipse(hdc, 250, 250, 1200, 500);
Draw formatted text with the middle of the client area aligned vertically and centered.
drawtext(hdc, text("My first window program!"), 1, &rect,dt_center |
dt_vcenter | dt_singleline);
endpaint(hwnd, &ps);
return 0;
case wm_close:
1. Generally, the response is WM close: call destroywindow(), destroywindow() and send a WM destroy message;In response to WM DESTROY, the PostQUITMessage() function sends a WM QUIT tag to the message queue, and getMessage() exits the program when it finds WM QUIT.
2. When a window or application should be closed, a WM Close message is issued, and when a WM Close message is received, if you wish, you can ask the user whether you really want to exit through the messagebox.
3. The user chooses to opt out or not to opt out. */
if (idyes == messagebox(hwnd,text("Whether you really want to return.
Out!),text("ok?"),mb_yesno))
destroywindow(hwnd);
break;
elsebreak;Didn't quit, and did nothing.
case wm destroy: Handles exit messages.
postquitmessage(0);This message goes directly to the head of the message queue!
break;
default: Can be placed at the end.
return defwindowproc(hwnd, message, wparam, lparam);The default window procedure is invoked to provide default processing for any window messages that are not processed by the application.
return 0;
return defwindowproc(hwnd, message, wparam, lparam);The default window procedure is invoked to provide default processing for any window messages that are not processed by the application.
*NOTE***
WM CLOSE message: Sent as a signal that the window or application should be terminated.
#define wm_close 0x0010
Return value type: lresult
If the application processes this message, it should return zero.
Remark. The application can prompt the user for confirmation before destroying the window by handling the WM Close message and calling the destroywindow function only after the user confirms the selection.
By default, the defwindowproc function calls the destroywindow function to destroy the window.
destroywindow function: destroys the specified window. The function sends WM DESTROY and WM NCdestroy messages to the window to deactivate it and move keyboard focus out of it.
The function also breaks the window's menu, refreshes the threaded message queue, breaks the timer, removes clipboard ownership, and breaks the clipboard viewer chain if the window is at the top of the viewer chain.
If the specified window is a parent or owner window, destroywindow automatically destroys the associated child or owner window when the parent or owner window is destroyed. The function first destroys the child or owner window, and then the parent or owner window. DestroyWindow also breaks modeless dialogs created by the createdialog function.
bool destroywindow(
hwnd hwnd destroys the handle of the window.
Return value type: Boolean.
If the function succeeds, the return value is non-zero.
If the function fails, the return value is zero. To get the error message for the extension, call getlasterror.
After clicking the Close system menu, a dialog box pops up, as shown in Figure 2-6
Figure 2-6 WM close message
*NOTE***Readers may wonder how the WM Close message is generated and retrieved when the user clicks on the system menu "Close" windowThe answer is that the Windows operating system gets the "Close" system menu message and then sends a WM Close message. We'll explain this in more detail in the next section.
016 Programming Guru Win32 API daily practice.
The 16th example is wm sizeC: The window process --- process WM size messages.
WM size message --- resize the client area.
invalidaterect function.
Note: After closing the window, the program exits.
c) www.bcdaren.com coding whiz.
#include
lresult callback wndproc(hwnd, uint, wparam, lparam);Message function.
*Program Entry**
int winapi winmain(hinstance hinstance, hinstance hprevinstance,lpstr lpcmdline, int ncmdshow)
static tchar szappname = text("hellowin");The name of the window class.
Omitted) return msgwparam;From a message indicating an exit, this value is returned to the system.
Functions - Message processing***
Window Procedure function.
lresult callback wndproc(hwnd hwnd, uint message, wparam wparam, lparam lparam)
hdc hdc;Device environment – where the drawing is made.
paintstruct ps;Drawing struct variables.
rect rect;The extent of the plot area.
static int cxclient,cyclient;The width and height of the client area, defined as static local variables.
switch (message)
case wm create: Creates a window message.
return 0;
break;You can also.
case wm size: A message that changes the size of the window's client area.
getclientrect(hwnd, &rect);Equivalent to the role of the lparam parameter.
invalidaterect(hwnd, &rect, true);true redraw the background of the window client area.
cxclient = loword(lparam);The new width of the client area.
cyclient = hiword(lparam);A new level in the customer area.
break;
case wm paint: Draw on a window!
hdc = beginpaint(hwnd, &ps);
Get the client area size.
getclientrect(hwnd, &rect);
Draw strings --- change with the size of the client area.
textout(hdc, 200, 200, text("Ida!"), lstrlen(text("Ida!
Comparative testing.
textout(hdc, cxclient / 5, cyclient / 5, text("Ida!"),lstrlen(text("Ida!")))
Draw an elliptical shape --- change with the size of the client area.
ellipse(hdc, 250, 250, 1200, 500);Comparative testing.
ellipse(hdc, cxclient / 4 , cyclient / 4 , cxclient / 4 * 3,cyclient / 4 * 3);
Draw formatted text with the middle of the client area aligned vertically and centered.
drawtext(hdc, text("My first window program!"), 1, &rect,dt_center |
dt_vcenter | dt_singleline);
endpaint(hwnd, &ps);
break;
case wm_close:
if (idyes == messagebox(hwnd,text("Do you really want to quit!,text("ok?"),mb_yesno))
destroywindow(hwnd);
break;
elsebreak;Didn't quit, and did nothing.
case wm destroy: Handles exit messages.
postquitmessage(0);This message goes directly to the head of the message queue!
break;
default: Can be placed at the end.
return defwindowproc(hwnd, message, wparam, lparam);Invoke the default window procedure.
return 0;
return defwindowproc(hwnd, message, wparam, lparam);Invoke the default window procedure.
*NOTE***
WM size message: When the client area of the main window changes in size, our application will receive a WM size message.
The high part of the lparam is the height of the client area, and the low part is the width of the client area.
The resize type of the wparam request. This parameter can be one of the following values.
size maxhide: When one of the other windows is maximized, the message will be sent to all popups.
size maximized: The window has been maximized.
size maxshow: When some other windows return to their original size, a message will be sent to all pop-ups.
size minimized: The window is minimized.
size restored: The window has been resized, but neither the size minimized nor size maximized values apply.
Note: Lparam has the same function as GetClientRect, and sometimes WM size is more efficient than using GetClientRect.
WM size can be used in the program to save the size of the client area for later use.
invalidaterect function: Add a rectangle to the specified form update area, and then this part of the window customer area will be redrawn.
bool invalidaterect(
hwnd hwnd, the handle of the window where the new area has changed. If this parameter is null, the system will invalidate all windows (not just those of this application) and redraw.
All windows and send WM erasebkgnd and WM ncpaint to eliminate before the function returns.
Cease. It is not recommended to set this parameter to null.
const rect *lprect, a pointer to a rect structure that contains the guests of the rectangle to be added to the update area.
Household coordinates. If this parameter is null, the entire workspace is added to the update area.
Bool berase specifies whether you want to erase the background within the update area when working with the update area.
If this parameter is true, the background will be erased when the beginpaint function is called.
If this parameter is false, the background remains the same.
Running Results:
Figure 2-7 WM sizee message.
*NOTE***1 Readers remember that when we execute the showwindow function, we send a WM size message to the message queue. The wm size message is used to send a message to the window that the size has changed. When the size of the window changes, the system generates and sends a WM size message to the window, enabling the program to respond to the change in window size. The high part of the lparam parameter of the wm size message is the height of the client area, and the low part is the width of the client area. The wparam parameter is the resize type requested. Each Windows message has a wparam parameter and an lparam parameter attached to it. The meaning of the wparam and lparam parameters is different for different messages. We'll do it in 2Explained in detail in 4 sections.
2 Attentive readers will notice that the window in this example can be changed by dragging the mouse, and the graphics and text drawn in the window client area will also change the corresponding position. Note that when processing the WM PAINT message, the GDI drawing function is proportional to the coordinate position of the drawing and text in the window client area. First, define the static variables cxclient and cyclient, and then use the lparam parameter to get the height and width of the window client area when processing the wm size message. Of course, you can also use the getclientrect function to get the width and height of the window client area, and you can use one of the methods.
3 Ask the reader to compare and test how it will look if the coordinates and regions of the drawn text and ellipses are set to fixed values.