窗口的创建一般分为以下几步:
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
view plaincopy to clipboardprint?
/*************************************************************/
#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);
/*************************************************************/
#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
view plaincopy to clipboardprint?
// 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;
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/lanyzh0909/archive/2010/07/22/5756023.aspx
¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥
WinMain里用DialogBox直接创建dialog窗口,这样做有什么缺点?
以前一直用的书上的代码,今天试了下WinMain里直接DialogBox也行。省去了那些retister class什么的,代码如下:
-
C/C++ code
-
#include <windows.h> #include "resource.h" LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM) ; int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { DialogBox( hInstance, ( LPCTSTR ) IDD_DIALOG1, NULL, ( DLGPROC ) WndProc ); return 0; } LRESULT CALLBACK WndProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch(uMsg){ case WM_INITDIALOG: { } break; case WM_COMMAND: switch(LOWORD( wParam )) { case IDOK: { EndDialog( hDlg, 0 ); } break; }; break; case WM_CLOSE: EndDialog( hDlg, 0 ); break; }; return 0; }
比起先register class,CreateWindow,然后GetMessage,TranslateMessage,DispatchMessage , 直接把所有工作交给DialogBox不是更方便?请问这样做有什么缺点?
20
第1个回答
没什么缺点。
模态对话框有自己的消息循环。
第2个回答
这样写小程序比较快的.
第3个回答
其实对话框和普通窗口都差不多.只不过普通窗口程序需要自己定义窗口过程,而对话框系统负责,但是修改rc文件,可以把默认的窗口过程替换成用户自己的,有点忘记了,大概这样.对话框只是一个特殊的窗口.
第4个回答
没什么缺点。这样写比自己注册类简单一点,比用MFC麻烦。主要看个人爱好。
第5个回答
缺点就是自由度不够。但凡省事的,总有缺失的。
第6个回答
好用就好,管那么多优点缺点干嘛
第7个回答
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) {
DialogBox( hInstance, ( LPCTSTR ) IDD_DIALOG1, NULL, ( DLGPROC ) WndProc );
return 0;
}
=================================================================================================
Windows uses its own internal window procedure to process messages to a dialog box window. Windows then passes these messages to a dialog box procedure within the program that creates the dialog box.
so application cannot catch such as WM_KEYUP,WM_KEYDOWN messages in its own DialogProc.
( try to catch them)