#include"stdafx.h"
HINSTANCE g_hInstance = 0;
//窗口处理函数
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_DESTROY:
PostQuitMessage(0); //能够使GetMessage返回0?
break;
}
return DefWindowProc(hWnd, message, wParam, lParam); //给各种消息做默认处理
}
//注册窗口类
void Register(LPSTR lpClassName, WNDPROC wndproc){
WNDCLASS wce = { 0 };
//wce.cbSize = sizeof(wce);
wce.cbClsExtra = 0;
wce.cbWndExtra = 0;
wce.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
wce.hCursor = NULL;
wce.hIcon = NULL;
//wce.hIconSm = NULL;
wce.hInstance = g_hInstance;
wce.lpfnWndProc = wndproc;
wce.lpszClassName = _TEXT("lpClassName");
wce.lpszMenuName = NULL;
wce.style = CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wce);//将以上所有赋值全部写入操作系统
}
//创建主窗口
HWND CreateMain(LPSTR lpClassName, LPSTR lpWndName){
HWND hWnd = CreateWindowEx(0,_TEXT("lpClassName"),_TEXT("lpWndName"),WS_OVERLAPPEDWINDOW,100,100,700,500,NULL,NULL,g_hInstance,NULL);
return hWnd;
}
//显示窗口
void Display(HWND hWnd){
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
}
//消息循环
void Message(){
MSG nMsg = { 0 };
while (GetMessage(&nMsg,NULL,0,0))
{
TranslateMessage(&nMsg);
DispatchMessage(&nMsg); //交给窗口处理函数
}
}
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){
//HWND hWnd;
g_hInstance = hInstance; //当前程序实例句柄
Register("Main",WndProc);
HWND hWnd=CreateMain("Main", "window");
Display(hWnd);
Message();
return 0;
}
以上代码自己写的有错望纠正!