12Windows_Modeless_Dialog对话框
#include <windows.h>
#include <iostream>
#include "resource.h"
CHAR szText[256] = { 0 };
#define PrintLog(x) WriteConsole(g_hStdout, x, strlen(x), NULL, NULL)
HINSTANCE g_hInst = NULL; //窗口句柄
HANDLE g_hStdout = NULL; //控制台句柄
//查找窗口处理函数
BOOL CALLBACK FindDlgProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
switch (nMsg)
{
//可以看出子窗口的创建销毁和初始化都与主窗口不同
//1、创建用CreateDialog
//2、初始化用WM_INITDIALOG消息
//3、销毁用SC_COLOSE消息
//WM_CREATE消息在这里被屏蔽了
case WM_CREATE: //这个消息在这里没有用
MessageBox(NULL, "Create", "WinDlg", MB_OK);
break;
case WM_INITDIALOG: //大量的初始化是在这里做的
MessageBox(NULL, "WM_INITDIALOG", "WinDlg", MB_OK);
break;
case WM_SYSCOMMAND:{
switch (wParam)
{
case SC_CLOSE:
EndDialog(hWnd, 0);
break;
}
}
break;
}
return 0;
}
//查找函数
void Find(HWND hWnd)
{
//创建对话框
HWND hDlg = CreateDialog(g_hInst, MAKEINTRESOURCE(IDD_FIND), hWnd, FindDlgProc);
//显示对话框
ShowWindow(hDlg,SW_SHOW);
}
//OnCommand
void OnCommand(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
//获取命令ID
int nCmdId = LOWORD(wParam);
switch (nCmdId)
{
case ID_EXIT:
PostQuitMessage(0);
break;
case ID_FIND:
Find(hWnd);
break;
}
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
switch (nMsg)
{
case WM_COMMAND:
OnCommand(hWnd, nMsg, wParam, lParam);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd, nMsg, wParam, lParam);
}
BOOL RegisterWnd(LPSTR pszClassName)
{
WNDCLASSEX wce = { 0 };
wce.cbSize = sizeof(wce);
wce.cbClsExtra = 0;
wce.cbWndExtra = 0;
wce.hbrBackground = HBRUSH(COLOR_BTNFACE + 1);
wce.hCursor = NULL;
wce.hIcon = NULL;
wce.hIconSm = NULL;
wce.hInstance = g_hInst;
wce.lpfnWndProc = WndProc;
wce.lpszClassName = pszClassName;
wce.lpszMenuName = NULL;
wce.style = CS_HREDRAW | CS_VREDRAW;
ATOM atom = RegisterClassEx(&wce);
if (atom == NULL)
{
return FALSE;
}
else
{
return TRUE;
}
}
HWND CreateWnd(LPSTR pszClassName)
{
HMENU hMenu = LoadMenu(g_hInst, MAKEINTRESOURCE(IDR_MAIN));
HWND hWnd = CreateWindowEx(0, pszClassName, "灭天", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, NULL, hMenu, g_hInst, 0);
return hWnd;
}
void ShowWnd(HWND hWnd)
{
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
}
void Msg()
{
MSG msg = { 0 };
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
void ConsoleWnd()
{
AllocConsole();
g_hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
CHAR szText[] = "Debug start:\n";
WriteConsole(g_hStdout, szText, strlen(szText), NULL, NULL);
}
int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd)
{
g_hInst = hInstance; //ConsoleWnd();
RegisterWnd("oooo");
HWND hWnd = CreateWnd("oooo");
ShowWnd(hWnd);
Msg();
return 0;
}
转载于:https://www.cnblogs.com/nfking/p/5573172.html