一、Win32 API
/********************************************************************
created: 2014/04/14 15:30
filename: WinMain.c
author: Justme0 (http://blog.csdn.net/justme0) purpose: Win32 API
*********************************************************************/ #include <windows.h>
#include <string.h> WCHAR *strText = L"请单击本窗口用户区"; /*
** 某事件发生时,处理该事件对应的消息
*/
LRESULT CALLBACK WndProc(
HWND hWindow,
UINT message,
WPARAM wParam,
LPARAM IParam)
{
PAINTSTRUCT ps;
HDC hdc; switch (message) {
case WM_LBUTTONDOWN:
strText = L"Hello, world!";
InvalidateRect(hWindow, NULL, );
break; case WM_PAINT:
hdc = BeginPaint(hWindow, &ps); TextOut(hdc, , , strText, wcslen(strText));
EndPaint(hWindow, &ps);
break; case WM_DESTROY:
PostQuitMessage();
break; default:
return DefWindowProc(hWindow, message, wParam, IParam);
} return ;
} /*
** 注册窗口,该窗口的名字是 wndName
*/
ATOM Register(HINSTANCE hInstance, WCHAR *wndName) {
WNDCLASS wnd;
wnd.cbClsExtra = ;
wnd.cbWndExtra = ;
wnd.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);
wnd.hCursor = LoadCursor(NULL, IDC_ARROW);
wnd.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wnd.hInstance = hInstance;
wnd.lpfnWndProc = WndProc;
wnd.lpszClassName = wndName;
wnd.lpszMenuName = NULL;
wnd.style = ; return RegisterClass(&wnd);
} /*
** 创建窗口,返回窗口句柄
*/
HWND Create(HINSTANCE hInstance, WCHAR *lpszClassName) {
return CreateWindow(
lpszClassName,
L"My Window",
WS_OVERLAPPEDWINDOW,
, , , ,
NULL,
NULL,
hInstance,
NULL);
} /*
** 消息循环
*/
int Run() {
MSG msg;
while (GetMessage(&msg, NULL, , )) {
TranslateMessage(&msg);
DispatchMessage(&msg);
} return msg.wParam;
} int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
HWND hWnd = NULL; // 窗口句柄
WCHAR wndName[] = L"Window_ClassName"; // 窗口名 Register(hInstance, wndName);
hWnd = Create(hInstance, wndName);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd); return Run();
}
二、OO模拟MFC
/********************************************************************
created: 2014/04/14 20:37
filename: WinMain.cpp
author: Justme0 (http://blog.csdn.net/justme0) purpose: Windows程序设计,用 OO 模拟 MFC
*********************************************************************/ #include <Windows.h> WCHAR *strText = L"请单击本窗口用户区"; /*
** 某事件发生时,处理该事件对应的消息
*/
LRESULT CALLBACK WndProc(
HWND hWindow,
UINT message,
WPARAM wParam,
LPARAM IParam)
{
PAINTSTRUCT ps;
HDC hdc; switch (message) {
case WM_LBUTTONDOWN:
strText = L"Hello, world!";
InvalidateRect(hWindow, NULL, );
break; case WM_PAINT:
hdc = BeginPaint(hWindow, &ps); TextOut(hdc, , , strText, wcslen(strText));
EndPaint(hWindow, &ps);
break; case WM_DESTROY:
PostQuitMessage();
break; default:
return DefWindowProc(hWindow, message, wParam, IParam);
} return ;
} /*
** 窗体类
*/
class CFrameWnd {
protected:
HWND hWnd; public:
CFrameWnd() : hWnd(NULL) {} /*
** 注册窗口,该窗口的名字是 className
*/
ATOM Register(HINSTANCE hInstance, WCHAR *className) {
WNDCLASS wnd;
wnd.cbClsExtra = ;
wnd.cbWndExtra = ;
wnd.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);
wnd.hCursor = LoadCursor(NULL, IDC_ARROW);
wnd.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wnd.hInstance = hInstance;
wnd.lpfnWndProc = WndProc;
wnd.lpszClassName = className;
wnd.lpszMenuName = NULL;
wnd.style = ; return RegisterClass(&wnd);
} /*
** 创建窗口,返回窗口句柄
*/
void Create(HINSTANCE hInstance, WCHAR *className, WCHAR *wndName) {
this->hWnd = CreateWindow(
className,
wndName,
WS_OVERLAPPEDWINDOW,
, , , ,
NULL,
NULL,
hInstance,
NULL);
} void ShowWindow(int nCmdShow) {
::ShowWindow(this->hWnd, nCmdShow);
} void UpdateWindow() {
::UpdateWindow(this->hWnd);
}
}; /*
** `应用程序类
*/
class CWinApp {
protected:
CFrameWnd * m_pMainWnd; public:
CWinApp() : m_pMainWnd(NULL) {} virtual void InitInstance(HINSTANCE hInstance, int nCmdShow) {
m_pMainWnd = new CFrameWnd; WCHAR className[] = L"Window_ClassName"; // 窗口类名
WCHAR wndName[] = L"我的窗口"; // 窗口名
m_pMainWnd->Register(hInstance, className);
m_pMainWnd->Create(hInstance, className, wndName);
m_pMainWnd->ShowWindow(nCmdShow);
m_pMainWnd->UpdateWindow();
} /*
** 消息循环
*/
int Run() {
MSG msg;
while (GetMessage(&msg, NULL, , )) {
TranslateMessage(&msg);
DispatchMessage(&msg);
} return msg.wParam;
} virtual ~CWinApp() {
delete m_pMainWnd;
} }; class CWinAppDerived : public CWinApp {
public:
void InitInstance(HINSTANCE hInstance, int nCmdShow) {
m_pMainWnd = new CFrameWnd; WCHAR className[] = L"Window_ClassName"; // 窗口类名
WCHAR wndName[] = L"用新的 InitInstance 函数的程序"; // 窗口名
m_pMainWnd->Register(hInstance, className);
m_pMainWnd->Create(hInstance, className, wndName);
m_pMainWnd->ShowWindow(nCmdShow);
m_pMainWnd->UpdateWindow();
}
}; int APIENTRY WinMain(
__in HINSTANCE hInstance,
__in_opt HINSTANCE hPrevInstance,
__in LPSTR lpCmdLine,
__in int nShowCmd)
{
CWinApp myApp; // 或 CWinAppDerived myApp;
myApp.InitInstance(hInstance, nShowCmd); return myApp.Run();
}
三、消息处理函数的简单封装
/********************************************************************
created: 2014/04/14 20:37
filename: WinMain.cpp
author: Justme0 (http://blog.csdn.net/justme0) purpose: Windows程序设计,用 OO 模拟 MFC
*********************************************************************/ #include <Windows.h> class CWinApp;
class CWinAppDerived;
CWinApp * AfxGetApp();
LRESULT CALLBACK WndProc(
HWND hWindow,
UINT message,
WPARAM wParam,
LPARAM IParam); WCHAR *gText = L"请单击本窗口用户区"; /*
** 具有处理消息的类
*/
class CCmdTarget {
protected:
HWND hWnd; public:
CCmdTarget() : hWnd(NULL) {} /*
** 某事件发生时,处理该事件对应的消息
*/
LRESULT CALLBACK AfxWndProc(
HWND hWindow,
UINT message,
WPARAM wParam,
LPARAM IParam)
{
switch (message) {
case WM_LBUTTONDOWN:
this->OnLButtonDown();
break; case WM_PAINT: {
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWindow, &ps);
this->OnDraw(hdc);
EndPaint(hWindow, &ps);
break; } case WM_DESTROY:
this->OnDestroy();
break; default:
return DefWindowProc(hWindow, message, wParam, IParam);
} return ;
} virtual void OnLButtonDown() {} virtual void OnDestroy() {
::PostQuitMessage();
} void OnDraw(HDC hdc) {
TextOut(hdc, , , gText, wcslen(gText));
}
}; /*
** 窗体类
*/
class CFrameWnd : public CCmdTarget {
public:
/*
** 注册窗口,该窗口的名字是 className
*/
ATOM Register(HINSTANCE hInstance, WCHAR *className) {
WNDCLASS wnd;
wnd.cbClsExtra = ;
wnd.cbWndExtra = ;
wnd.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);
wnd.hCursor = LoadCursor(NULL, IDC_ARROW);
wnd.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wnd.hInstance = hInstance;
wnd.lpfnWndProc = WndProc;
wnd.lpszClassName = className;
wnd.lpszMenuName = NULL;
wnd.style = ; return RegisterClass(&wnd);
} /*
** 创建窗口,返回窗口句柄
*/
void Create(HINSTANCE hInstance, WCHAR *className, WCHAR *wndName) {
this->hWnd = CreateWindow(
className,
wndName,
WS_OVERLAPPEDWINDOW,
, , , ,
NULL,
NULL,
hInstance,
NULL);
} void ShowWindow(int nCmdShow) {
::ShowWindow(this->hWnd, nCmdShow);
} void UpdateWindow() {
::UpdateWindow(this->hWnd);
}
}; class CFrameWndDerived : public CFrameWnd {
void OnLButtonDown() {
gText = L"重新定义的消息处理";
InvalidateRect(this->hWnd, NULL, );
}
}; /*
** `应用程序类
*/
class CWinApp {
public:
CFrameWnd *m_pMainWnd;
CWinApp *m_pSelfApp; public:
CWinApp() : m_pMainWnd(NULL) {
m_pSelfApp = this; // 不能直接初始化
} virtual void InitInstance(HINSTANCE hInstance, int nCmdShow) {
m_pMainWnd = new CFrameWnd; WCHAR className[] = L"Window_ClassName"; // 窗口类名
WCHAR wndName[] = L"CWinApp窗口"; // 窗口名
m_pMainWnd->Register(hInstance, className);
m_pMainWnd->Create(hInstance, className, wndName);
m_pMainWnd->ShowWindow(nCmdShow);
m_pMainWnd->UpdateWindow();
} /*
** 消息循环
*/
int Run() {
MSG msg;
while (GetMessage(&msg, NULL, , )) {
TranslateMessage(&msg);
DispatchMessage(&msg);
} return msg.wParam;
} virtual ~CWinApp() {
delete m_pMainWnd;
} friend CWinApp * AfxGetApp();
}; /*
** 派生的窗体类
*/
class CWinAppDerived : public CWinApp {
public:
void InitInstance(HINSTANCE hInstance, int nCmdShow) {
m_pMainWnd = new CFrameWndDerived; WCHAR className[] = L"Window_ClassName"; // 窗口类名
WCHAR wndName[] = L"CWinAppDerived窗口"; // 窗口名
m_pMainWnd->Register(hInstance, className);
m_pMainWnd->Create(hInstance, className, wndName);
m_pMainWnd->ShowWindow(nCmdShow);
m_pMainWnd->UpdateWindow();
}
}; LRESULT CALLBACK WndProc(
HWND hWindow,
UINT message,
WPARAM wParam,
LPARAM IParam)
{
CWinApp * pApp = AfxGetApp();
return pApp->m_pMainWnd->AfxWndProc(hWindow, message, wParam, IParam);
} CWinAppDerived g_myAppDerived; CWinApp * AfxGetApp() {
return g_myAppDerived.m_pSelfApp;
} int APIENTRY WinMain(
__in HINSTANCE hInstance,
__in_opt HINSTANCE hPrevInstance,
__in LPSTR lpCmdLine,
__in int nShowCmd)
{
CWinApp *pCWinApp = AfxGetApp();
pCWinApp->InitInstance(hInstance, nShowCmd); return pCWinApp->Run();
}
运行结果
四、用 MFC 设计的含有消息映射的 Windows 应用程序
/********************************************************************
created: 2014/04/16 22:08
filename: WinMain.cpp
author: Justme0 (http://blog.csdn.net/justme0) purpose: 用 MFC 设计的含有消息映射的 Windows 应用程序 注:在“项目设置”中选择使用 MFC 类库(动态、静态皆可)
*********************************************************************/ #define _WIN32_WINNT 0x0502 #include <afxwin.h> class CFrameWndDerived : public CFrameWnd {
private:
WCHAR *showText; public:
CFrameWndDerived() : showText(L"初始化的字符串") {} afx_msg void OnPaint() {
CPaintDC dc(this);
dc.TextOut(, , showText);
} afx_msg void OnLButtonDown(UINT, CPoint) { // 这个参数表针对 VS 编译器
showText = L"有消息映射表的程序";
InvalidateRect(NULL, TRUE);
} DECLARE_MESSAGE_MAP();
}; BEGIN_MESSAGE_MAP(CFrameWndDerived, CFrameWnd)
ON_WM_PAINT()
ON_WM_LBUTTONDOWN()
END_MESSAGE_MAP() /*
** 派生的窗体类
*/
class CWinAppDerived : public CWinApp {
public:
BOOL InitInstance() {
CFrameWndDerived *pMainWndDerived = new CFrameWndDerived;
pMainWndDerived->Create(NULL, L"MFC");
pMainWndDerived->ShowWindow(m_nCmdShow);
pMainWndDerived->UpdateWindow();
m_pMainWnd = pMainWndDerived; return TRUE;
}
}; CWinAppDerived MyApp; // 实在费解,程序中哪里引用到了这个全局变量?怎么检测到这个全局变量的?