您的当前位置:首页正文

C++创建窗口

2024-11-27 来源:个人技术集锦
windows窗口类,利用windows API创建窗口。


#include <Windows.h> //windows窗口标准库函数
#include <stdio.h> //c语言需要的库函数

LRESULT CALLBACK WindowProc(//窗口过程函数原型
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);
int WINAPI WinMain(//窗口主函数,相当于 DOS下的main函数
HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine, // pointer to command line
int nCmdShow // show state of window
)
{
WNDCLASS wcs;
wcs.cbClsExtra=0; //窗口类附加参数
wcs.cbWndExtra=0; //窗口附加参数
wcs.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);//窗口DC背景
wcs.hCursor=LoadCursor(hInstance,IDC_CROSS); //鼠标样式
wcs.hIcon=LoadIcon(NULL,IDI_ERROR);//窗口icon
wcs.hInstance=hInstance;//应用程序实例
wcs.lpfnWndProc=WindowProc;
wcs.lpszClassName="练习窗口";
wcs.lpszMenuName=NULL;
wcs.style=CS_VREDRAW|CS_HREDRAW;
RegisterClass(&wcs);
HWND hWnd;
hWnd=CreateWindow("练习窗口","练习窗口",WS_OVERLAPPEDWINDOW,0,0,600,600,NULL,NULL,hInstance,NULL);
ShowWindow(hWnd,nCmdShow);
UpdateWindow(hWnd);
MSG msg;
//消息循环
while(GetMessage(&msg,0,NULL,NULL))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WindowProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
)
{
switch(uMsg)
{
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
break;
}
return 0;
}
显示全文