Dynamic Control creation-imitating windows Notepad program Part1

Source: Internet
Author: User

Create controls
Just like creating all other windows, the createmediawex () API is used to create controls. upload the expected registered class. Here is the edit box control class. This gives you a standard edit box control window. when using a dialog box to create a control, you generally need to write down the list of controls to be created so that you can call DialogBox () or CreateDialog () in the dialog box, the system can read the control list in the resource and call createmediawex () to create each one according to the position and style defined in the resource.
[Cpp]
HEdit = createmediawex (WS_EX_CLIENTEDGE, "EDIT ","",
WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL,
0, 0,100,100, hwnd, (HMENU) IDC_MAIN_EDIT, GetModuleHandle (NULL), NULL );
If (hEdit = NULL)
MessageBox (hwnd, "cocould not create edit box.", "Error", MB_ OK | MB_ICONERROR );
We can see that this createmediawex () call specifies a lot of styles, and it is also common in actual programming, especially the commonly used controls with large heap options. the four WS _ style at the beginning should be quite clear. This control is created in a subwindow of the window to make it visible, and there are vertical and horizontal moving bars.
The three patterns (ES_MULTLINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL) that are unique to the editing box specify that the editing box should contain multiple lines of text, you can also press the button automatically at the bottom and rightmost of the control.
The regular window style (WS _ *) is listed in MSDN. the extended window style (WS_EX _ *) is explained in the description of createmediawex () in MSDN. You can also find the link to the specific style description of each control. (In this example, ES _ * is exclusive to the editing box ).
The control specifies the window handle as its parent window, and specifies an ID for IDC_MAIN_EDIT to reference the control when it is created in the dialog box. the location and size parameters are not very important here, because the system will immediately respond to the WM_SIZE message to dynamically adjust the control so that it is always in the middle of the window and fit its size.
Adjust the size of dynamically created controls
In general, if the window size can be adjusted, you always want to write some code to dynamically adjust the size of the controls you create in so that they are always reasonable.
[Cpp]
GetClientRect (hwnd, & rcClient );
 
HEdit = GetDlgItem (hwnd, IDC_MAIN_EDIT );
SetWindowPos (hEdit, NULL, 0, 0, rcClient. right, rcClient. bottom, SWP_NOZORDER );
Because there is only one control, it is relatively simple to work. we use GetClientRect () to obtain the size of the window's customer partition, that is, it does not include the margin, the large (so far) area of the menu and title bar. this will fill in our RECT struct with values. left and top are always 0, so you can ignore them. the right and bottom values will be given to the region and height of your customer region.
In the next step, we use GetDlgItem () to obtain the control handle. This API can be used for both the dialog box and regular window, And SetWindowPos () is called () move and stretch to fill the entire guest zone. of course, you can change the value of SetWindowPos () to do something else, such as filling only half of the window and leaving the other half with other controls.
Apply the framework of the previous windows program.

Post Code:

[Cpp]
# Include <Windows. h>

# Define IDC_MAIN_EDIT 101


Const char g_szClassName [] = "myWindowClass ";

Lresult callback WndProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
Switch (msg)
{
Case WM_CREATE:
{
HWND hEdit;
HFONT hDefFont;
HEdit = createmediawex (WS_EX_CLIENTEDGE, "Edit", "", WS_CHILD | WS_VISIBLE
| WS_VSCROLL | WS_HSCROLL | ES_MULTILINE | ES_AUTOHSCROLL | ES_AUTOVSCROLL,
0, 0, 100,100, hwnd, (HMENU) IDC_MAIN_EDIT, GetModuleHandle (NULL), NULL );
If (hEdit = NULL)
MessageBox (hwnd, "Create Edit Failed", "Error", MB_ OK | MB_ICONERROR );
HDefFont = (HFONT) GetStockObject (DEFAULT_GUI_FONT );
SendMessage (hEdit, WM_SETFONT, (WPARAM) hDefFont, MAKELPARAM (FALSE, 0 ));
}
Break;
Case WM_SIZE:
{
HWND hEdit;
RECT rect;
GetClientRect (hwnd, & rect );
HEdit = GetDlgItem (hwnd, IDC_MAIN_EDIT );
SetWindowPos (hEdit, NULL, 0, 0, rect. right, rect. bottom, SWP_NOZORDER );
}
Break;
Case WM_CLOSE:
DestroyWindow (hwnd );
Break;
Case WM_DESTROY:
PostQuitMessage (0 );
Break;
Default:
Return DefWindowProc (hwnd, msg, wParam, lParam );
}
Return 0;
}
Int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
Wc. cbSize = sizeof (WNDCLASSEX );
Wc. style = 0;
Wc. lpfnWndProc = WndProc;
Wc. cbClsExtra = 0;
Wc. cbWndExtra = 0;
Wc. hInstance = hInstance;
Wc. hIcon = LoadIcon (NULL, IDI_APPLICATION );
Wc. hCursor = LoadCursor (NULL, IDC_ARROW );
Wc. hbrBackground = (HBRUSH) (COLOR_WINDOW + 1 );
Wc. lpszMenuName = NULL;
Wc. lpszClassName = g_szClassName;
Wc. hIconSm = LoadIcon (NULL, IDI_APPLICATION );
If (! RegisterClassEx (& wc ))
{
MessageBox (NULL, "Window Registration Failed! "," Error! ",
MB_ICONEXCLAMATION | MB_ OK );
Return 0;
}
Hwnd = createdomainwex (
WS_EX_CLIENTEDGE,
G_szClassName,
"Edit ",
WS_OVERLAPPEDWINDOW,
200,200,400,400,
NULL, NULL, hInstance, NULL );
If (hwnd = NULL)
{
MessageBox (NULL, "Window Creation Failed! "," Error! ",
MB_ICONEXCLAMATION | MB_ OK );
Return 0;
}
ShowWindow (hwnd, nCmdShow );
UpdateWindow (hwnd );
While (GetMessage (& Msg, NULL, 0, 0)> 0)
{
TranslateMessage (& Msg );
DispatchMessage (& Msg );
}
Return Msg. wParam;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.