CreateWindow是Windows API中的一个函数,用于创建一个窗口并返回一个窗口句柄。该函数可用于创建主窗口、子窗口、对话框以及控件等。本文将重点介绍CreateWindow创建子窗口的方法及案例。
一、CreateWindow创建子窗口的方法
1. 定义父窗口
在使用CreateWindow之前,必须先定义父窗口。父窗口是指容纳子窗口的窗口,可以是主窗口或其他子窗口。定义父窗口需要使用CreateWindow创建窗口并返回窗口句柄。
例如,创建一个主窗口:
```c++
HWND hwnd;
hwnd = CreateWindow(
TEXT("MainWndClass"),
TEXT("Main Window"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
640, 480,
NULL, NULL,
hInstance,
NULL
);
```
2. 声明子窗口类
使用CreateWindow创建子窗口之前,需要先声明一个子窗口类。子窗口类是用于注册子窗口的类,在创建子窗口时将其关联到该类。
例如,声明一个子窗口类:
```c++
WCHAR szChildClass[] = TEXT("Child_Class");
WNDCLASSEX wcexChild;
wcexChild.cbSize = sizeof(WNDCLASSEX);
wcexChild.style = CS_HREDRAW | CS_VREDRAW;
wcexChild.lpfnWndProc = ChildWndProc;
wcexChild.cbClsExtra = 0;
wcexChild.cbWndExtra = 0;
wcexChild.hInstance = hInstance;
wcexChild.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
wcexChild.hCursor = LoadCursor(NULL, IDC_ARROW);
wcexChild.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcexChild.lpszMenuName = NULL;
wcexChild.lpszClassName = szChildClass;
wcexChild.hIconSm = LoadIcon(hInstance, IDI_APPLICATION);
if (!RegisterClassEx(&wcexChild))
{
MessageBox(NULL,
TEXT("注册子窗口类失败!"),
TEXT("错误"),
MB_ICONERROR | MB_OK);
return 0;
}
```
3. 创建子窗口
使用CreateWindow创建子窗口需要指定该窗口的父窗口、子窗口类及风格等参数。
例如,创建一个包含一个编辑框的子窗口:
```c++
HWND hwndChild;
hwndChild = CreateWindow(
szChildClass,
TEXT("Child Window"),
WS_CHILD | WS_VISIBLE | WS_BORDER | ES_LEFT | ES_AUTOHSCROLL,
50, 50,
200, 20,
hwnd,
NULL,
hInstance,
NULL
);
```
二、CreateWindow创建子窗口的案例
下面是一个完整的使用CreateWindow创建子窗口的样例程序,包含一个主窗口和两个子窗口。其中一个子窗口是包含一个编辑框和一个按钮的对话框,另一个子窗口是一个静态文本框。
```c++
#include #include LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); LRESULT CALLBACK ChildWndProc(HWND, UINT, WPARAM, LPARAM); INT_PTR CALLBACK DialogProc(HWND, UINT, WPARAM, LPARAM); const WCHAR szMainClass[] = TEXT("Main_Class"); WCHAR szChildClass[] = TEXT("Child_Class"); const WCHAR szDialogClass[] = TEXT("Dialog_Class"); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { HWND hwnd; MSG msg; WNDCLASSEX wcex; // 主窗口类 wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(hInstance, IDI_APPLICATION); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); wcex.lpszMenuName = NULL; wcex.lpszClassName = szMainClass; wcex.hIconSm = LoadIcon(hInstance, IDI_APPLICATION); if (!RegisterClassEx(&wcex)) { MessageBox(NULL, TEXT("注册主窗口类失败!"), TEXT("错误"), MB_ICONERROR | MB_OK); return 0; } // 子窗口类 WNDCLASSEX wcexChild; wcexChild.cbSize = sizeof(WNDCLASSEX); wcexChild.style = CS_HREDRAW | CS_VREDRAW; wcexChild.lpfnWndProc = ChildWndProc; wcexChild.cbClsExtra = 0; wcexChild.cbWndExtra = 0; wcexChild.hInstance = hInstance; wcexChild.hIcon = LoadIcon(hInstance, IDI_APPLICATION); wcexChild.hCursor = LoadCursor(NULL, IDC_ARROW); wcexChild.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); wcexChild.lpszMenuName = NULL; wcexChild.lpszClassName = szChildClass; wcexChild.hIconSm = LoadIcon(hInstance, IDI_APPLICATION); if (!RegisterClassEx(&wcexChild)) { MessageBox(NULL, TEXT("注册子窗口类失败!"), TEXT("错误"), MB_ICONERROR | MB_OK); return 0; } // 对话框类 WNDCLASSEX wcexDialog; wcexDialog.cbSize = sizeof(WNDCLASSEX); wcexDialog.style = CS_HREDRAW | CS_VREDRAW; wcexDialog.lpfnWndProc = DefDlgProc; wcexDialog.cbClsExtra = 0; wcexDialog.cbWndExtra = DLGWINDOWEXTRA; wcexDialog.hInstance = hInstance; wcexDialog.hIcon = LoadIcon(hInstance, IDI_APPLICATION); wcexDialog.hCursor = LoadCursor(NULL, IDC_ARROW); wcexDialog.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); wcexDialog.lpszMenuName = NULL; wcexDialog.lpszClassName = szDialogClass; wcexDialog.hIconSm = LoadIcon(hInstance, IDI_APPLICATION); if (!RegisterClassEx(&wcexDialog)) { MessageBox(NULL, TEXT("注册对话框类失败!"), TEXT("错误"), MB_ICONERROR | MB_OK); return 0; } // 创建主窗口 hwnd = CreateWindow( szMainClass, TEXT("CreateWindow子窗口案例"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, NULL, NULL, hInstance, NULL ); if (!hwnd) { MessageBox(NULL, TEXT("创建主窗口失败!"), TEXT("错误"), MB_ICONERROR | MB_OK); return 0; } // 创建第一个子窗口 HWND hwndChild1; hwndChild1 = CreateWindow( szChildClass, TEXT("Child Window 1"), WS_CHILD | WS_VISIBLE | WS_BORDER, 20, 20, 200, 60, hwnd, NULL, hInstance, NULL ); if (!hwndChild1) { MessageBox(NULL, TEXT("创建子窗口失败!"), TEXT("错误"), MB_ICONERROR | MB_OK); return 0; } HWND hwndStatic1; hwndStatic1 = CreateWindow( TEXT("STATIC"), TEXT("这是一个静态文本框。"), WS_CHILD | WS_VISIBLE | WS_BORDER | SS_CENTER, 10, 10, 180, 20, hwndChild1, NULL, hInstance, NULL ); if (!hwndStatic1) { MessageBox(NULL, TEXT("创建静态文本框失败!"), TEXT("错误"), MB_ICONERROR | MB_OK); return 0; } // 创建第二个子窗口(作为对话框) HWND hwndChild2; hwndChild2 = CreateWindow( szChildClass, TEXT("Child Window 2"), WS_CHILD | WS_VISIBLE | WS_BORDER, 20, 100, 200, 80, hwnd, NULL, hInstance, NULL ); if (!hwndChild2) { MessageBox(NULL, TEXT("创建子窗口失败!"), TEXT("错误"), MB_ICONERROR | MB_OK); return 0; } HWND hwndButton; hwndButton = CreateWindow( TEXT("BUTTON"), TEXT("打开对话框"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 10, 10, 120, 30, hwndChild2, NULL, hInstance, NULL ); if (!hwndButton) { MessageBox(NULL, TEXT("创建按钮失败!"), TEXT("错误"), MB_ICONERROR | MB_OK); return 0; } ShowWindow(hwnd, nCmdShow); UpdateWindow(hwnd); // 主消息循环 while (GetMessage(&msg, NULL, 0, 0)) { if (!IsDialogMessage(hwndDialog, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } } return (int)msg.wParam; } LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd, message, wParam, lParam); } return 0; } LRESULT CALLBACK ChildWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_CTLCOLORSTATIC: if ((HWND)lParam == hwndStatic1) { SetTextColor((HDC)wParam, RGB(255, 0, 0)); SetBkColor((HDC)wParam, RGB(255, 255, 255)); return (INT_PTR)CreateSolidBrush(RGB(255, 255, 255)); } break; case WM_CREATE: hwndStatic1 = CreateWindow( TEXT("STATIC"), TEXT("这是一个静态文本框。"), WS_CHILD | WS_VISIBLE | WS_BORDER | SS_CENTER, 10, 10, 180, 20, hwnd, NULL, (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL ); if (!hwndStatic1) { MessageBox(NULL, TEXT("创建静态文本框失败!"), TEXT("错误"), MB_ICONERROR | MB_OK); return 0; } break; case WM_COMMAND: if (lParam == (LPARAM)hwndButton) { DialogBox((HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), MAKEINTRESOURCE(IDD_DIALOG1), hwnd, DialogProc); break; } break; default: return DefWindowProc(hwnd, message, wParam, lParam); } return 0; } INT_PTR CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_INITDIALOG: return TRUE; case WM_COMMAND: switch (LOWORD(wParam)) { case IDOK: case IDCANCEL: EndDialog(hwndDlg, LOWORD(wParam)); return TRUE; } break; } return FALSE; } ``` 在该程序中,通过CreateWindow创建了一个主窗口和两个子窗口。其中一个子窗口是包含一个编辑框和一个按钮的对话框,另一个子窗口是一个静态文本框。程序运行后,界面如下图所示: ![image](https://user-images.githubusercontent.com/64126328/131261952-0ce774db-102c-4ed4-8cd2-81cbf83e0852.png) 点击“打开对话框”按钮,会弹出一个对话框,如下图所示: ![image](https://user-images.githubusercontent.com/64126328/131262039-cab24b4d-aa65-43f2-9520-2d72e0db4148.png) 三、总结 本文介绍了使用CreateWindow创建子窗口的方法及案例,包括定义父窗口、声明子窗口类及创建子窗口等操作。在实际应用中,可以根据需要调整子窗口的风格、位置、大小等参数,并添加对应的消息处理函数,实现更丰富的界面效果。 如果你喜欢我们三七知识分享网站的文章,
欢迎您分享或收藏知识分享网站文章
欢迎您到我们的网站逛逛喔!https://www.37seo.cn/
发表评论 取消回复