I'm sorry, but some things were busy the other day. I just had a little time off yesterday, so I went on with my API Primer series, and we've basically implemented a dialog box in the middle of my implementation messagebox, and we can display our text in the center and display a certain button, However, I left a question at the end of the last article, and that is that the OK button does not change depending on the size of the window. So how are we going to solve this problem?
I gave a hint that I could do this by dealing with wm_size. So today we are going to finish this legacy problem. There are not many new API functions to use, just two, and the message to be processed is two wm_size and wm_command
GetDlgItem
MoveWindow
So two, as for the other API functions, we have been contacted before, if you forget, you can review the use of their own methods.
First, let's look at how the WM_SIZE message processing function is written.
case WM_SIZE:
{
const int buttonWidth = 80;
const int buttonHeight = 25;
int buttonx, buttony;
RECT rctClient;
GetClientRect( hwnd, &rctClient );
buttonx = rctClient.right/2 - buttonWidth/2;
buttony = rctClient.bottom/2 - buttonHeight/2;
HWND hButton = GetDlgItem( hwnd, 2 );
MoveWindow( hButton, buttonx, buttony, buttonWidth, buttonHeight, TRUE );
}
break;
As the content of this article is less, then I can give a detailed description of WM_SIZE this message processing function in the implementation of the button is always centered code. Wm_size This message is the message handler function that is sent to the window when the size of a window is changed. We are here to intercept the wm_size can be changed every time the size of the window to do some processing, we are here to deal with this window is a child window, that is, the OK button to move to the center.
First of all, we still have the old rules, define Buttonwidth, and buttonheight two of regular reshaping to store the length and height of the OK button. Then two shape variable buttonx are defined, and buttony is used to hold the coordinate position of the upper left corner of the determined button which is computed by the calculation.
A RECT structure is then defined to hold the long width of the window that is fetched by the GetClientRect API function, where the right in rctclient preserves the length of the window, and the bottom saves the height of the window. In order for the button to remain in the center of the main window, we need to have the x-coordinate of the top-left corner of the button in the length of the main window minus the long half of the button's position. High is the same principle. So Buttonx = RCTCLIENT.RIGHT/2-BUTTONWIDTH/2; Buttony = RCTCLIENT.BOTTOM/2-BUTTONHEIGHT/2; These two sentences are based on the current main window of the length and height of the calculation button should be in position.
Once the calculation is complete, we just need to move the button. But before we move the button, we need to get the handle to the button, how does this handle get, there are a lot of methods, here I use Gedlgitem this API function to get, it requires two parameters, the first parameter is a handle to the main window, where we pass the button the main window of the handle HWND, The second argument is an identifier for the button, because the identifier given to the button when we created the child window with CreateWindowEx in the previous article is 2, so we pass 2 here so that GetDlgItem returns the handle to the button.
After we get the handle, we need to use MoveWindow to move the child window button to where we want it. The first argument is the handle to the Word window, which is the handle we obtained above, and the second and third arguments are the x,y coordinates that are moved to. Here we pass Buttonx and Buttony, the fourth and fifth are the length and height of the moving window, and if you also need to change the length and height of the window, then here you can also pass the changed long and high values, where we only move the position, do not change the size, so pass Buttonwidth and Buttonheight. The last argument is a bool type, indicating whether it needs to be redrawn, passing true here, which is to redraw it after the move.