Windows 编程创建窗口

//函数

#include <windows.h>

#define WND_POS_X 100
#define WND_POS_Y 100
#define WND_WIDTH 500
#define WND_HEIGHT 600

//声明
LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

//一.创建窗口结构体

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPreInstance,LPSTR lpCmdLine,int nCmdShow)

{

  HWND hWnd;//窗口句柄

 MSG mSg;


//创建窗口结构体

WNDCLASSEX wc;
wc.cbClsExtra = 0;//暂时不用
wc.cbSize = sizeof(wc);//结构体大小
wc.cbWndExtra = 0;//暂时不用
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;//背景颜色
wc.hCursor = NULL;//光标
wc.hIcon = NULL;//窗口左上角的图标
wc.hIconSm = NULL;//状态栏中的图标,默认与左上角图标一致
wc.hInstance = hInstance;//实例句柄
wc.lpfnWndProc = WndProc;//回调函数
wc.lpszClassName = "nanhang";//结构体名字
wc.lpszMenuName = NULL;//菜单栏名字

wc.style = CS_HREDRAW | CS_VREDRAW;//窗口水平变化或是垂直变化时重绘窗口

                                         //注册窗口结构体

if (0 == RegisterClassEx(&wc))
{
int eNum = GetLastError();//注册失败的错误代码
return 0;//注册失败
}

//创建窗口
hWnd = CreateWindowEx(WS_EX_WINDOWEDGE, "nanhang",
"俄罗斯方块", WS_OVERLAPPEDWINDOW,
WND_POS_X, WND_POS_Y, WND_WIDTH, WND_HEIGHT,
NULL, NULL, hInstance, NULL);
if (NULL == hWnd)
{
return 0;//创建失败
}


//显示窗口
ShowWindow(hWnd, nCmdShow);


//消息循环
while (GetMessage(&mSg, NULL, 0, 0))
{
//翻译消息
TranslateMessage(&mSg);
//分发消息
DispatchMessage(&mSg);
}

运行结果



猜你喜欢

转载自blog.csdn.net/qq_41603898/article/details/80968266
今日推荐