mfcexp1_2.cpp

来自「清华 MFC Windows应用程序设计 教材部分源代码」· C++ 代码 · 共 93 行

CPP
93
字号
//设计Windows程序必须包含的头文件-------------------------------------------------
#include<windows.h>
//定义全局变量--------------------------------------------------------------------------------
HINSTANCE		hInst;	
HWND 			hWnd;		
MSG			msg;
char lpszClassName[]="窗口";
char*ShowText;
//定义函数--------------------------------------------------------------------------------------
ATOM			MyRegisterClass(HINSTANCE hInstance);//注册窗口类函数
BOOL			Create(HINSTANCE, int);     //程序实例初始化函数
int				Run();                          //消息循环函数
LRESULT CALLBACK	WndProc(HWND, UINT, 
WPARAM, LPARAM); //窗口函数
//主函数-----------------------------------------------------------------------------------------
int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
	
	MyRegisterClass(hInstance);        	//定义和注册窗口类
	Create(hInstance, nCmdShow);  		//创建窗口
	ShowWindow(hWnd, nCmdShow);   	//显示窗口
	UpdateWindow(hWnd);            	//更新屏幕显示
	return Run();                     	//消息循环
}
//注册窗口类的函数--------------------------------------------------------------------------
ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASS wc;
	wc.style=0;
	wc.lpfnWndProc=WndProc;
	wc.cbClsExtra=0;
	wc.cbWndExtra=0;
	wc.hInstance=hInstance;
	wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);
	wc.hCursor=LoadCursor(NULL,IDC_ARROW);
	wc.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
	wc.lpszMenuName=NULL;
	wc.lpszClassName=lpszClassName;
	return RegisterClass(&wc);
}
//创建窗口的函数-----------------------------------------------------------------------------
BOOL Create(HINSTANCE hInstance, int nCmdShow)
{
	hWnd=CreateWindow(	lpszClassName,
						"Windows",
						WS_OVERLAPPEDWINDOW,
						120,50,800,600,
						NULL,
						NULL,
						hInstance,
						NULL);
   return TRUE;
}
//消息循环的函数-----------------------------------------------------------------------------
int Run( )
{
	while (GetMessage(&msg, NULL, 0, 0)) 
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return msg.wParam;
}
//窗口函数--------------------------------------------------------------------------------------
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, 
WPARAM wParam, LPARAM lParam)
{
	PAINTSTRUCT ps;
	HDC hdc;
	switch (message) 
	{
	case WM_LBUTTONDOWN:
		ShowText="Hello!";
		InvalidateRect(hWnd,NULL,1);
		break;
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		TextOut(hdc,50,50,ShowText,6);
		EndPaint(hWnd, &ps);
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}
//--------------------------------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?