win32空專案建立窗體

露水上的青蛙發表於2013-07-25

#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;
}

相關文章