#include <Windows.h>
#include <stdio.h> // sprintf
HINSTANCE g_hInstance = NULL;
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
BOOL Register(LPSTR lpClassName, WNDPROC wndProc)
{
/*
Windows的窗口类共分为三种:
(1)系统窗口类
(2)全局窗口类
(3)局部窗口类
*/
// 注册窗口类
WNDCLASSEX wce = {0};
wce.cbSize = sizeof(wce);
wce.style = CS_HREDRAW /*提供窗口位置变化事件和高度变化事件的处理程序,功能是重绘窗口*/
| CS_VREDRAW /*提供窗口位置变化事件和宽度变化事件的处理程序,功能是重绘窗口*/
/*| CS_GLOBALCLASS 注册全局使用的窗口类*/;
wce.lpfnWndProc = wndProc;
wce.cbClsExtra = 0;
wce.cbWndExtra = 0;
wce.hInstance = g_hInstance;
wce.hIcon = NULL;
wce.hCursor = NULL;
wce.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wce.lpszMenuName = NULL;
wce.lpszClassName = "Main";
ATOM nAtom = RegisterClassEx(&wce);
if (nAtom == 0)
{
return FALSE;
}
return TRUE;
}
HWND CreateMain(LPSTR lpClassName, LPSTR lpWndName)
{
// 创建窗口
return CreateWindowEx(0,
lpClassName,
lpWndName,
WS_OVERLAPPEDWINDOW, /*允许窗口重叠*/
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
g_hInstance,
NULL);
}
VOID Display(HWND hWnd)
{
// 显示窗口
ShowWindow(hWnd, SW_SHOW); // 设置窗口的显示属
UpdateWindow(hWnd); // 重新更新窗口属性,只有更新之后,属性才会生效
}
void MessageLoop()
{
// 消息循环
MSG nMsg = {0};
while (GetMessage(&nMsg, NULL, 0, 0))
{
TranslateMessage(&nMsg); // 将键盘上的按键等消息翻译成字符消息
DispatchMessage(&nMsg); // 将翻译后的消息再次放入到程序的消息队列中
if (nMsg.message == WM_PAINT)
{
char buf[256] = {0};
sprintf(buf, "消息处理[%d]\n", nMsg.message);
//WriteConsole(hOutput, buf, sizeof(buf), NULL, NULL);
}
}
}
HWND CreateSubWindow(LPSTR lpClassName, LPSTR lpWndName)
{
return CreateWindowEx(0, lpClassName, lpWndName,
WS_OVERLAPPEDWINDOW/*窗口重叠*/|WS_CHILD/*创建子窗口*/|WS_VISIBLE/*显示子窗口*/|WS_POPUP/*以弹出的方式显示子窗口*/,
320, 160,
960, 640,
NULL, NULL, g_hInstance, NULL);
}
int APIENTRY wWinMain(_In_ HINSTANCE hInstance /* 当前程序的实例句柄,也就是目前程序所在的内存的位置*/,
_In_opt_ HINSTANCE hPrevInstance /*当前程序的前一个程序实例句柄,目前已经废弃,不再使用*/,
_In_ LPWSTR lpCmdLine /*命令行参数,我们执行程序时可以用命令行的形式传入一些参数*/,
_In_ int nCmdShow /*窗口的显示方式,最大化、最小化那种*/)
{
g_hInstance = hInstance;
if(!Register("Main", WndProc))
{
MessageBox(NULL, "注册失败", "Infor", MB_OK);
return 0;
}
// 系统窗口 Button
//HWND hWnd = CreateMain("Button", "我是一个Button");
#if 1
HWND hWnd = CreateMain("Main", "我是一个Main");
CreateSubWindow("Main", "子窗口");
Display(hWnd);
MessageLoop();
#endif
#if 0
WNDCLASS wc;
if (GetClassInfo(NULL, "Button", &wc) == false)
{
MessageBox(NULL, "get class info failed.", NULL, NULL);
}
#endif
#if 0
if (UnregisterClass("Main", NULL) == FALSE)
{
MessageBox(NULL, "UnregisterClass failed.", NULL, NULL);
}
#endif
return 0;
}
// 窗口处理函数
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_DESTROY:
PostQuitMessage(0); // 可以使GetMessage返回0
break;
case WM_CREATE:
{
CREATESTRUCT crt = *((CREATESTRUCT*)lParam);
char buf[256] = {0};
sprintf(buf, "创建的窗口类名称是[%s],窗口名称是[%s]", crt.lpszClass, crt.lpszName);
MessageBox(NULL, buf, "消息处理", MB_OK);
}
break;
#if 0
case WM_QUIT:
{
int param = (int)wParam;
char buf[256];
sprintf(buf, "进程退出,退出码[%d]", param);
MessageBox(NULL, buf, "消息处理", MB_OK);
}
break;
#endif
case WM_SYSCOMMAND:
{
if (wParam == SC_MAXIMIZE)
{
short x = LOWORD(lParam);
short y = HIWORD(lParam);
char buf[256];
sprintf(buf, "窗口最大化,x坐标[%d],y坐标[%d]", x, y);
MessageBox(NULL, buf, "消息处理", MB_OK);
}
else if (wParam == SC_MINIMIZE)
{
short x = LOWORD(lParam);
short y = HIWORD(lParam);
char buf[256];
sprintf(buf, "窗口最小化,x坐标[%d],y坐标[%d]", x, y);
MessageBox(NULL, buf, "消息处理", MB_OK);
}
else if (wParam == SC_CLOSE)
{
short x = LOWORD(lParam);
short y = HIWORD(lParam);
char buf[256];
sprintf(buf, "窗口关闭,x坐标[%d],y坐标[%d]", x, y);
MessageBox(NULL, buf, "消息处理", MB_OK);
}
}
break;
case WM_SIZE:
{
if (wParam == SIZE_RESTORED)
{
short x = LOWORD(lParam);
short y = HIWORD(lParam);
char buf[256];
sprintf(buf, "重新调整窗口大小,x坐标[%d],y坐标[%d]", x, y);
MessageBox(NULL, buf, "消息处理", MB_OK);
}
else if (wParam == SIZE_MAXIMIZED)
{
short x = LOWORD(lParam);
short y = HIWORD(lParam);
char buf[256];
sprintf(buf, "窗口最大化显示,x坐标[%d],y坐标[%d]", x, y);
MessageBox(NULL, buf, "消息处理", MB_OK);
}
else if (wParam == SC_MINIMIZE)
{
short x = LOWORD(lParam);
short y = HIWORD(lParam);
char buf[256];
sprintf(buf, "窗口最小化显示,x坐标[%d],y坐标[%d]", x, y);
MessageBox(NULL, buf, "消息处理", MB_OK);
}
}
break;
case WM_PAINT:
{
PAINTSTRUCT pt;
HDC hdc = BeginPaint(hWnd, &pt);
Rectangle(hdc, 0, 0, 100, 100);
EndPaint(hWnd, &pt);
}
break;
case WM_LBUTTONDOWN:
{
//InvalidateRect(hWnd, NULL, true);
}
break;
default:
break;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
Windows SDK 窗口开发 小demo记录
最新推荐文章于 2023-03-18 16:45:25 发布