Windows应用程序的封装

         在本文中,我们将讨论Windows应用程序的封装,此处的封装是用函数来封装函数,而不是用类来封装函数(MFC是这样做的)。那么,我们为什么要用函数来封装函数呢?因为这对理解MFC有很大好处。

        先看没有封装的程序:

 

#include <windows.h>

// 回调函数
LRESULT CALLBACK MyFun(
	HWND hwnd,
	UINT uMsg,
	WPARAM wParam,
	LPARAM lParam				   
)
{
	switch(uMsg)
	{
	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;
}

int WINAPI WinMain(
  HINSTANCE hInstance,
  HINSTANCE hPrevInstance, 
  LPSTR lpCmdLine,     
  int nShowCmd  
  )
{
	WNDCLASS wnd;
	wnd.cbClsExtra = 0;
	wnd.cbWndExtra = 0;
	wnd.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wnd.hCursor = LoadCursor(NULL, IDC_CROSS);
	wnd.hIcon = LoadIcon(NULL, IDI_ERROR);
	wnd.hInstance = hInstance;  
	wnd.lpfnWndProc = MyFun;   // 指定回调函数
	wnd.lpszClassName = "ClassName"; 
	wnd.lpszMenuName = NULL;
	wnd.style = CS_HREDRAW | CS_VREDRAW;
	RegisterClass(&wnd);

	HWND hwnd;
	hwnd = CreateWindow("ClassName", "温馨提示:", WS_OVERLAPPEDWINDOW, 
		500, 500, 200, 200, NULL, NULL, hInstance, NULL); 
	ShowWindow(hwnd, SW_SHOWNORMAL);
	
	MSG msg;
	while(GetMessage(&msg, NULL, 0, 0)) // 从消息队列中循环地抓取消息
	{
		// 可以调用TranslateMessage函数来产生新的消息,并投到消息队列中

		DispatchMessage(&msg); // 把消息给Windows
	}

	return msg.wParam;
}

     

 

      将程序进行封装,得到:

 

#include <windows.h>

HINSTANCE g_hInst;
HWND      g_hWnd;

LRESULT CALLBACK MyFun(
	HWND hwnd,
	UINT uMsg,
	WPARAM wParam,
	LPARAM lParam				   
)
{
	switch(uMsg)
	{
	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;
}

void InitApplication(HINSTANCE hInstance)
{
	WNDCLASS wnd;
	wnd.cbClsExtra = 0;
	wnd.cbWndExtra = 0;
	wnd.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wnd.hCursor = LoadCursor(NULL, IDC_CROSS);
	wnd.hIcon = LoadIcon(NULL, IDI_ERROR);
	wnd.hInstance = hInstance;  
	wnd.lpfnWndProc = MyFun;
	wnd.lpszClassName = "ClassName"; 
	wnd.lpszMenuName = NULL;
	wnd.style = CS_HREDRAW | CS_VREDRAW;

	RegisterClass(&wnd);
}

void InitInstance(HINSTANCE hInstance, int nCmdShow)
{
	g_hWnd = CreateWindow("ClassName", "温馨提示:", WS_OVERLAPPEDWINDOW, 
		500, 500, 200, 200, NULL, NULL, hInstance, NULL); 

	ShowWindow(g_hWnd, nCmdShow);
}

int WINAPI WinMain(
	HINSTANCE hInstance, 
	HINSTANCE hPrevInstance,
	LPSTR lpCmdLine,    
	int nCmdShow
	)
{
	InitApplication(hInstance);

	InitInstance(hInstance, nCmdShow);

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

	return msg.wParam;
}

       InitApplication函数和InitInstance函数是MFC中非常重要的两个函数,理解了上面的程序,可以为理解MFC提供很大帮助。

 

 

       上面两个程序的作用相同,结果为:



 

 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值