创建一个完整Win32程序

 
创建一个Win32程序的步骤:
1           WinMain函数的的定义
2           创建一个窗口
2.1.1     设计一个窗口类
2.1.2     注册窗口类
2.1.3     创建窗口
2.1.4     显示及更新窗口
3           进行消息循环
4           编写消息过程函数
 
WinMain
int WINAPI WinMain (
 HINSTANCE hInstance, // handle to current instance,given by OS
 HINSTANCE hPrevInstance, // handle to previous instance ,Null in Win32
 LPSTR lpCmdLine,       // pointer to command line
 int nCmdShow           // show state of window
);
 
 
创建一个窗口
通过WNDCLASS结构体来创建窗口类
typedef struct _WNDCLASS {
    UINT    style; 指定窗口的样式
    WNDPROC lpfnWndProc; 窗口的回调函数
    int     cbClsExtra; 0
    int     cbWndExtra; 0
    HANDLE hInstance;
    HICON   hIcon;
    HCURSOR hCursor;
    HBRUSH hbrBackground;
    LPCTSTR lpszMenuName;
    LPCTSTR lpszClassName;
} WNDCLASS;
 
注册窗口类
ATOM RegisterClass(
 CONST WNDCLASS *lpWndClass    // address of structure with class
                               // data
);
创建窗口
HWND CreateWindow(
 LPCTSTR lpClassName, // pointer to registered class name
 LPCTSTR lpWindowName, // pointer to window name
 DWORD dwStyle,         // window style
 int x,                 // horizontal position of window
 int y,                 // vertical position of window
 int nWidth,            // window width
 int nHeight,           // window height
 HWND hWndParent,       // handle to parent or owner window
 HMENU hMenu,           // handle to menu or child-window identifier
 HANDLE hInstance,      // handle to application instance
 LPVOID lpParam         // pointer to window-creation data
);
 
显示及更新窗口
BOOL ShowWindow( int nCmdShow );
BOOL UpdateWindow(
 HWND hWnd    // handle of window
);
消息循环
BOOL GetMessage(
 LPMSG lpMsg,          // address of structure with message
 HWND hWnd,            // handle of window
 UINT wMsgFilterMin,  // first message
 UINT wMsgFilterMax    // last message
);
If the function retrieves a message other than WM_QUIT, the return value is nonzero.
If the function retrieves the WM_QUIT message, the return value is zero.
 
MSG msg;
While (GetMessage (&msg,NULL,0,0))
{
TranslateMessage();
DispatchMessage();
 
}
 
消息过程函数
LRESULT CALLBACK WindowProc(
 HWND hwnd,      // handle to window
 UINT uMsg,      // message identifier
 WPARAM wParam, // first message parameter
 LPARAM lParam   // second message parameter
);
 函数原型一定要相同,函数名可以更改,函数用switch对消息进行处理.

程序:

#include <windows.h>
#include <stdio.h>
LRESULT CALLBACK WinProc(
  HWND hwnd,      // handle to window
  UINT uMsg,      // message identifier
  WPARAM wParam,  // first message parameter
  LPARAM lParam   // second message parameter
);
int WINAPI WinMain
(
  HINSTANCE hInstance,      // handle to current instance
  HINSTANCE hPrevInstance,  // handle to previous instance
  LPSTR lpCmdLine,          // command line
  int nCmdShow              // show state)
{
 WNDCLASS wndcls;

 wndcls.cbClsExtra=0;
 wndcls.cbWndExtra=0;
 wndcls.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
 wndcls.hCursor=LoadCursor(NULL,IDC_CROSS);
 wndcls.hIcon=LoadIcon(NULL,IDI_ERROR);
 wndcls.hInstance= hInstance;
 wndcls.lpfnWndProc=WinSunProc;
  wndcls.lpszClassName="wangjing";
 wndcls.lpszMenuName=NULL;
 wndcls.style=CS_HREDRAW | CS_VREDRAW;

 RegisterClass(& wndcls);
 HWND hwnd;

 hwnd=CreateWindow(" wangjing","Title",WS_OVERLAPPEDWINDOW,
  0,0,600,400,NULL,NULL, hInstance,NULL);
 ShowWindow(hwnd,SW_SHOWNORMAL);

 UpdateWindow(hwnd);
  MSG msg;
 while(GetMessage(&msg,NULL,0,0))
 {
  TranslateMessage(&msg);
  DispatchMessage(&msg);
 }

 return msg.wParam;
}
LRESULT CALLBACK WinSunProc(
  HWND hwnd,      // handle to window
  UINT uMsg,      // message identifier
  WPARAM wParam,  // first message parameter
  LPARAM lParam   // second message parameter
)
{
 switch(uMsg)
 {
 case WM_CHAR:
  char szChar[20];
  sprintf(szChar,"char code is %d",wParam);
  MessageBox(hwnd,szChar,"char",0);
  break;
 case WM_LBUTTONDOWN:
  MessageBox(hwnd,"mouse clicked","message",0);
  HDC hdc;
  hdc=GetDC(hwnd);
  TextOut(hdc,0,50,"程序员之家",strlen("程序员之家"));
  //ReleaseDC(hwnd,hdc);
  break;
 case WM_PAINT:
  HDC hDC;
  PAINTSTRUCT ps;
  hDC=BeginPaint(hwnd,&ps);
  TextOut(hDC,0,0,"Test",strlen("Test"));
  EndPaint(hwnd,&ps);
  break;
 case WM_CLOSE:
  if(IDYES==MessageBox(hwnd,"是否真的结束?","message",MB_YESNO))
  {
   DestroyWindow(hwnd);
  }
  break;
 case WM_DESTROY:
  PostQuitMessage(0);
  break;
 default:
  return DefWindowProc(hwnd,uMsg,wParam,lParam);
 }
 return 0;
}
 

你可能感兴趣的:(Win32)