A sample win32 window creation

  • 2020-04-02 02:18:46
  • OfStack



#include<Windows.h>
#include<tchar.h> 
//Declare window function
LRESULT CALLBACK WindowProc(HWND hwnd,
       UINT uMsg,
       WPARAM wParam,
       LPARAM lparam
       );
int WINAPI WinMain(
     HINSTANCE hInstance,
     HINSTANCE hPrevInatance,
     LPSTR lpCmdLine,
     int nCmdShow
     )
{
 WNDCLASS wndclass;
 wndclass.lpfnWndProc=WindowProc;
 wndclass.cbClsExtra=0;
 wndclass.cbWndExtra=0;
 wndclass.style=CS_HREDRAW|CS_VREDRAW;
 wndclass.lpszClassName=_T(" My form ");
 wndclass.hInstance=hInstance;
 wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
 wndclass.hIcon=0;
 wndclass.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);
 wndclass.lpszMenuName=0;
 //Register window class
 if(RegisterClass(&wndclass)==0)
 {
  MessageBox(0,_T("Register window class failure "),_T(" My form "),MB_OK);
  return 0;
 }
 //Create the window real column
 HWND hWnd = CreateWindow(_T(" My form "),_T(" My first form "),WS_OVERLAPPEDWINDOW,100,100,500,400,0,0,hInstance,0);
 //Displays and updates Windows
 ShowWindow(hWnd,SW_SHOW);
 UpdateWindow(hWnd);
 //Message loop
 MSG msg;
 while(GetMessage(&msg,0,0,0))
 {
  TranslateMessage(&msg);
  DispatchMessage(&msg);
 }
 return 0;
}
//Define the window function
LRESULT CALLBACK WindowProc(
       HWND hwnd,
       UINT uMsg,
       WPARAM wParam,
       LPARAM IParam
       )
{
 switch(uMsg)
 {
 case WM_CLOSE:
  PostQuitMessage(0);
  break;
 default:
  return DefWindowProc(hwnd,uMsg,wParam,IParam);
 }
 return 0;
}


Related articles: