http://blog.csdn.net/lanyzh0909/article/details/5756023
窗口的创建一般分为以下几步:
1、实例化一个WNDCLASS(WNDCLASSEX)对象
2、调用RegisterClass(RegisterClassEx)函数注册窗口类
3、调用CreateWindow(CreateWindowEx)方法创建窗口
4、调用ShowWindow(hwnd,SW_SHOWNORMAL)设置窗口详细显示状态
5、调用UpdateWindow(hwnd)刷新显示窗口
6、进入消息循环等待消息
函数作用:
RegisterClass函数:通知系统我们要定义一个新的窗口类型,系统会为我们新的窗口类型分配相应的内存空间记录新窗口类的信息。
CreateWindow函数:创建一个新类型的窗体。基于此同一类型的窗体都具有相同的属性,比如,背景色,光标,图标等等。
ShowWindow函数:将我们创建的窗口显示到显示器上。
UpdateWindow函数:强制客户区域无效,给窗口过程发送一个WM_PAINT消息。
实现代码:
helloce.h
[cpp]
view plain
copy
- /*************************************************************/
- #define dim(x) (sizeof(x)/sizeof(x[0]))
- /*************************************************************/
- struct decodeUINT
- {
- UINT Code;
- LRESULT (*Fxn)(HWND,UINT,WPARAM,LPARAM);
- };
- /*************************************************************/
- HWND InitInstance(HINSTANCE,LPWSTR,int);
- int TermInstance(HINSTANCE,int);
- DWORD WINAPI PaintThrd(PVOID pArg);
- /*************************************************************/
- LRESULT CALLBACK MainWndProc(HWND,UINT,WPARAM,LPARAM);
- LRESULT DoPaintMain(HWND,UINT,WPARAM,LPARAM);
- LRESULT DoDestroyMain(HWND,UINT,WPARAM,LPARAM);
- LRESULT DoEraseBkgndMian(HWND,UINT,WPARAM,LPARAM);
helloce.cpp
[cpp]
view plain
copy
- // helloce.cpp : Defines the entry point for the application.
- //
- #include "stdafx.h"
- #include "helloce.h"
- #include "resource.h"
- const TCHAR szAppName[]=TEXT("Hello CE");
- HINSTANCE hInst;
- // Message dispatch table for MainWindowProc
- const struct decodeUINT MainMessages[] =
- {
- WM_PAINT,DoPaintMain,
- WM_DESTROY,DoDestroyMain,
- WM_ERASEBKGND,DoEraseBkgndMian,
- };
- int WINAPI WinMain( HINSTANCE hInstance,
- HINSTANCE hPrevInstance,
- LPTSTR lpCmdLine,
- int nCmdShow)
- {
- MSG msg;
- int rc =0;
- HWND hwndMain;
- hwndMain =InitInstance(hInstance,lpCmdLine,nCmdShow);
- if(hwndMain == 0) return 0x10;
- while(GetMessage(&msg,NULL,0,0))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- return TermInstance(hInstance,msg.wParam);
- }
- HWND InitInstance(HINSTANCE hInstance,LPWSTR lpCmdLine,int nCmdShow)
- {
- WNDCLASS wc;
- HWND hWnd;
- hInst = hInstance;
- #if defined(WIN32_PLATFORM_PSPC)||defined(WIN32_PLATFORM_WFSP)
- hWnd =FindWindow(szAppName,NULL);
- if(hWnd)
- {
- SetForegroundWindow((HWND)(((DWORD)hWnd)|0x01));
- return 0;
- }
- #endif
- //register application main window class
- wc.style = 0;
- wc.lpfnWndProc =MainWndProc;
- wc.cbClsExtra = 0;
- wc.cbWndExtra =0;
- wc.hInstance =hInstance;
- wc.hIcon = NULL;
- wc.hCursor =LoadCursor(NULL,IDC_ARROW);
- wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
- wc.lpszMenuName = NULL;
- wc.lpszClassName = szAppName;
- if(RegisterClass(&wc) == 0) return 0;
- hWnd = CreateWindow(szAppName,
- TEXT("Hello Win CE"),
- WS_VISIBLE|WS_CAPTION|WS_SYSMENU,
- CW_USEDEFAULT,
- CW_USEDEFAULT,
- CW_USEDEFAULT,
- CW_USEDEFAULT,
- NULL,
- NULL,
- hInstance,
- NULL
- );
- if(!IsWindow(hWnd)) return 0;
- ShowWindow(hWnd,nCmdShow);
- UpdateWindow(hWnd);
- return hWnd;
- }
- int TermInstance( HINSTANCE hInstance,int nDefRC)
- {
- return nDefRC;
- }
- LRESULT CALLBACK MainWndProc(HWND hWnd,UINT wMsg,WPARAM wParam,LPARAM lParam)
- {
- INT i;
- for(i =0; i<dim(MainMessages);i++)
- {
- if(wMsg == MainMessages[i].Code)
- return (*MainMessages[i].Fxn)(hWnd,wMsg,wParam,lParam);
- }
- return DefWindowProc(hWnd,wMsg,wParam,lParam);
- }
- LRESULT DoPaintMain(HWND hWnd,UINT wMsg,WPARAM wParam,LPARAM lParam)
- {
- PAINTSTRUCT ps;
- RECT rect;
- HDC hdc;
- GetClientRect(hWnd,&rect);
- hdc =BeginPaint(hWnd,&ps);
- int iOldMode = ::SetBkMode(hdc,TRANSPARENT);
- DrawText(hdc,TEXT("Hello windows ce !!"),-1,&rect,DT_CENTER|DT_VCENTER|DT_SINGLELINE);
- ::SetBkMode(hdc,iOldMode);
- EndPaint(hWnd,&ps);
- return 0;
- }
- LRESULT DoDestroyMain(HWND hWnd,UINT wMsg,WPARAM wParam,LPARAM lParam)
- {
- PostQuitMessage(0);
- return 0;
- }
- LRESULT DoEraseBkgndMian(HWND hWnd,UINT wMsg,WPARAM wParam,LPARAM lParam)
- {
- return 0;
- }