win32空项目创建窗体

时间:2021-10-08 01:35:09

#include "stdafx.h"

//窗口过程函数(系统自动调用,即回调函数)
LRESULT WINAPI MsgProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
    switch(msg)
 {
 case WM_DESTROY:
  PostQuitMessage(0);
  return 0;
  break;
 case WM_KEYUP:
  if(wParam==VK_ESCAPE)
   PostQuitMessage(0);
  break;
 }
 return DefWindowProc(hWnd,msg,wParam,lParam);
}

int WINAPI WinMain(HINSTANCE hInst,HINSTANCE prevhInst,LPSTR cmdLine,int
show)
{
 //Register the window class
 WNDCLASSEX wc={sizeof(WNDCLASSEX),CS_CLASSDC,MsgProc,0L,0L,
  GetModuleHandle(NULL),NULL,NULL,NULL,NULL,
  "AppClass",NULL};
 RegisterClassEx(&wc);

//create the application's window
    HWND hWnd=CreateWindow("AppClass","Batman",WS_OVERLAPPEDWINDOW,
  100,100,640,480,NULL,NULL,
  hInst,NULL);
    ShowWindow(hWnd,SW_SHOWDEFAULT);
 UpdateWindow(hWnd);

//enter the message loop
 MSG msg;
 ZeroMemory(&msg,sizeof(msg));//宏用0来填充一块内存区域

while(msg.message!=WM_QUIT)
 {
  if (PeekMessage(&msg,NULL,0U,0U,PM_REMOVE))//从消息队列中获取下一条消息
  {
   TranslateMessage(&msg);//对相关消息进行一些转换
   DispatchMessage(&msg);//将转换后的消息发送给消息过程函数
  }
  else
  {
           //处理向屏幕绘制图像的代码部分
  }
 }
 UnregisterClass("AppClass",wc.hInstance);//取消对窗口类的注册
 return 0;
}