Windows 窗口创建代码(C语言)
- W_Z_C
- 共 290 字,阅读约 1 分钟
这是一个使用C语言创建空白窗口的基本框架代码案例。
main.c
#include <Windows.h>
#include <WindowsX.h>
//窗体备用
HWND g_hwnd;
HINSTANCE g_hInstance;
//过程函数
LRESULT CALLBACK WinProc(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;
}
//主函数
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrecInstance, LPSTR lpCmdLine, int nCmdShow)
{
//定义填充窗口
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_VREDRAW | CS_HREDRAW;
wcex.lpfnWndProc = WinProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = TEXT("WinApp");
wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
//注册窗口类
if (!RegisterClassEx(&wcex))
{
return 0;
}
g_hInstance = hInstance;
//创建窗口
HWND hwnd;
if (!(hwnd = CreateWindowEx(0, TEXT("WinApp"), TEXT("HelloWord"), WS_OVERLAPPEDWINDOW,
0, 0, 800, 600, NULL, NULL, hInstance, NULL)))
{
return 0;
}
g_hwnd = hwnd;
//显示更新窗口
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
//消息循环
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}