存在窗口A和B的win32程序 , 当点击窗口A中的按钮后会弹出窗口B
#include <windows.h>// 窗口 B 的窗口过程
LRESULT CALLBACK WindowProcB(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {switch (uMsg) {case WM_DESTROY:PostQuitMessage(0);break;default:return DefWindowProc(hwnd, uMsg, wParam, lParam);}return 0;
}// 窗口 A 的窗口过程
LRESULT CALLBACK WindowProcA(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {switch (uMsg) {case WM_COMMAND:if (LOWORD(wParam) == 1) { // 按钮 ID 为 1// 注册窗口类 BWNDCLASS wcB = {};wcB.lpfnWndProc = WindowProcB;wcB.hInstance = GetModuleHandle(NULL);wcB.lpszClassName = L"WindowClassB";RegisterClass(&wcB);// 创建窗口 BHWND hwndB = CreateWindow(wcB.lpszClassName, L"Window B", WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, CW_USEDEFAULT, 300, 200, NULL, NULL, GetModuleHandle(NULL), NULL);// 显示窗口 BShowWindow(hwndB, SW_SHOW);UpdateWindow(hwndB);}break;case WM_CREATE: {// 创建按钮CreateWindow(L"BUTTON", L"Open Window B", WS_VISIBLE | WS_CHILD,10, 10, 120, 30, hwnd, (HMENU)1, GetModuleHandle(NULL), NULL);break;}case WM_DESTROY:PostQuitMessage(0);break;default:return DefWindowProc(hwnd, uMsg, wParam, lParam);}return 0;
}int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {// 注册窗口类 AWNDCLASS wcA = {};wcA.lpfnWndProc = WindowProcA;wcA.hInstance = hInstance;wcA.lpszClassName = L"WindowClassA";RegisterClass(&wcA);// 创建窗口 AHWND hwndA = CreateWindow(wcA.lpszClassName, L"Window A", WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, CW_USEDEFAULT, 400, 300, NULL, NULL, hInstance, NULL);// 显示窗口 AShowWindow(hwndA, nCmdShow);UpdateWindow(hwndA);// 消息循环MSG msg = {};while (GetMessage(&msg, NULL, 0, 0)) {TranslateMessage(&msg);DispatchMessage(&msg);}return (int)msg.wParam;
}