这一次修改事件的处理函数,当鼠标左键按下时,可在窗口的用户区显示一个字符串“凌云壮志”。
另一方面,用函数将程序模块化。
#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); //窗口函数
void OnLButtonDown(HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam);
void OnPaint(HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam);
void OnDestroy(HWND hWnd, UINT message,
WPARAM wParam, LPARAM 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,
400,300,180,160,
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)
{
switch (message)
{
case WM_LBUTTONDOWN: OnLButtonDown(hWnd, message, wParam, lParam);
break;
case WM_PAINT: OnPaint(hWnd, message,wParam, lParam);
break;
case WM_DESTROY: OnDestroy(hWnd, message, wParam, lParam);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
void OnLButtonDown(HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
ShowText="凌云壮志";
InvalidateRect(hWnd,NULL,true);
}
void OnPaint(HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
hdc = BeginPaint(hWnd, &ps);
TextOut(hdc,50,50,ShowText,6);
EndPaint(hWnd, &ps);
}
void OnDestroy(HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
PostQuitMessage(0);
}
InvalidateRect 函数,在MSDN里面的解释:The InvalidateRect function adds a rectangle to the specified window's update region. The update region represents the portion of the window's client area that must be redrawn.
主要使某个矩形框内的区域成为无效,用来产生一个WM_PAINT的消息。
它的原型:
BOOL InvalidateRect( HWND hWnd, // handle to window 窗口句柄,指出作用于哪个窗口 CONST RECT* lpRect, // rectangle coordinates 一个矩形区域 BOOL bErase // erase state,下面有官方的解释 );
BOOL bErase : Specifies whether the background within the update region is to be erased when the update region is processed. If this parameter is TRUE, the background is erased when the BeginPaint function is called. If this parameter is FALSE, the background remains unchanged. 这里面设置成true。
RECT结构体的原型如下:
typedef struct _RECT { LONG left; LONG top; LONG right; LONG bottom; } RECT, *PRECT;
至于BegainPaint()和EndPaint()等的函数看我的另一篇日志: