编写一个最简单的常用的windows窗口程序,需要编写两部分。下面是这两部分的描述及对应的数据结构。
第一部分:编写主函数WinMain
1.设计一个窗口类
typedef struct _WNDCLASS {UINT style;
WNDPROC lpfnWndProc;
int cbClsExtra;
int cbWndExtra;
HINSTANCE hInstance;
HICON hIcon;
HCURSOR hCursor;
HBRUSH hbrBackground;
LPCTSTR lpszMenuName;
LPCTSTR lpszClassName;
} WNDCLASS, *PWNDCLASS;
2.注册此窗口类
ATOM RegisterClass(
CONST WNDCLASS *lpWndClass // class data
);
3.创建一个窗口,并返回该窗口句柄
HWND CreateWindow(
LPCTSTR lpClassName, // registered class name
LPCTSTR lpWindowName, // 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, // menu handle or child identifier
HINSTANCE hInstance, // handle to application instance
LPVOID lpParam // window-creation data
);
4.显示及更新窗口
BOOL ShowWindow(
HWND hWnd, // handle to window
int nCmdShow // show state
);
BOOL UpdateWindow(
HWND hWnd // handle to window
);
5.进入消息循环
BOOL GetMessage(
LPMSG lpMsg, // message information
HWND hWnd, // handle to window
UINT wMsgFilterMin, // first message
UINT wMsgFilterMax // last message
);
BOOL TranslateMessage(
CONST MSG *lpMsg // message information
);
LRESULT DispatchMessage(
CONST MSG *lpmsg // message information
);
第二部分:编写窗口过程函数(又叫回调函数),其形式如下:
LRESULT CALLBACK WindowProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);
下面是书中的代码例子:
#include <windows.h> #include <stdio.h> LRESULT CALLBACK winsunproc( 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 nShowCmd // show state ) { //1. 设计一个窗口类 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; //应用程序的实例句柄由WinMain函数传进来 wndcls.lpfnWndProc = winsunproc; wndcls.lpszClassName = "winsun2003"; wndcls.lpszMenuName = NULL; wndcls.style = CS_VREDRAW | CS_HREDRAW; //2. 注册此窗口类 RegisterClass(&wndcls); //3. 创建一个窗口,并返回该窗口的句柄 HWND hwnd; hwnd = CreateWindow("winsun2003","hello",WS_OVERLAPPEDWINDOW,400,200,600,400,NULL,NULL,hInstance,NULL); //4. 显示及更新窗口 ShowWindow(hwnd,SW_SHOWNORMAL); UpdateWindow(hwnd); //5. 进入消息循环 MSG msg; BOOL bRet; while((bRet = GetMessage(&msg,hwnd,0,0)) != 0) { if(bRet == -1) { return -1; } else { TranslateMessage(&msg); DispatchMessage(&msg); } } return 0; } // 编写窗口过程函数(又叫回调函数) LRESULT CALLBACK winsunproc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) { 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); //ReleaseDC()和GetDC()函数配合使用 break; case WM_PAINT: HDC hDC; PAINTSTRUCT ps; hDC=BeginPaint(hwnd,&ps); TextOut(hDC,0,0,"http://www.sunxin.org",strlen("http://www.sunxin.org")); EndPaint(hwnd,&ps); //EndPaint()和BeginPaint()函数配合使用 break; case WM_CLOSE: DestroyWindow(hwnd); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd,uMsg,wParam,lParam); } return 0; }
2011-04-19 00:03:40