windows编程入门-定时刷新客户区

要求:

  在窗口中使用一个定时器,每隔1S交替使用红色、绿色、蓝色刷新整个窗口客户区。

分析一下:

  首先是

定义三个画刷分别定义为红色、蓝色、绿色。

  然后是获得整个窗口客户区,使用函数 GetClientRect(hwnd,&rc); 可以获取当前客户窗口。

GetClientRect(hwnd,&rc);  
			FillRect(hDC,&rc,hBrush);


hwnd 是 当前窗口句柄, rc 指向含有将填充矩形的逻辑坐标的RECT结构的指针。

关于 rc 的定义是 RECT rc;

hBrush = CreateSolidBrush(RGB(255,0,0));
			SelectObject(hDC,hBrush);


最后,利用函数FillRect(hDC,&rc,hBrush);来填充选中的区域。至于交替填充那就非常简单了。

FillRect(hDC,&rc,hBrushA);

完整代码如下

//运行环境: vs2008
#include 
#include 
#include 
TCHAR lpszClassName[] = L"window";
TCHAR lpszTitle[] = L"my_windows";
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
void InitWindow(WNDCLASS *WndClass,HINSTANCE hInstance);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInst,LPSTR lpszCmdLine,int nCmdShow)
{
	HWND hwnd;
	MSG Msg;
	WNDCLASS WndClass;

	InitWindow(&WndClass,hInstance);
	if(!RegisterClass(&WndClass))
	{
		MessageBeep(0);
		return FALSE;
	}

	hwnd = CreateWindow(
		lpszClassName,
		lpszTitle,
		WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		NULL,
		NULL,
		hInstance,
		NULL
		);

	ShowWindow(hwnd,nCmdShow);
	UpdateWindow(hwnd);
	while(GetMessage(&Msg,NULL,0,0))
	{
		TranslateMessage(&Msg);
		DispatchMessage(&Msg);
	}

	return Msg.wParam;
}

void InitWindow(WNDCLASS *WndClass,HINSTANCE hInstance)
{
	(*WndClass).style = 0;
	(*WndClass).lpfnWndProc = WndProc;
	(*WndClass).cbClsExtra = (*WndClass).cbWndExtra = 0;
	(*WndClass).hInstance = hInstance;
	(*WndClass).hIcon = LoadIcon(NULL,IDI_APPLICATION);
	(*WndClass).hCursor = LoadCursor(NULL,IDC_ARROW);
	(*WndClass).hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	(*WndClass).lpszMenuName = NULL;
	(*WndClass).lpszClassName = lpszClassName;
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
	HDC hDC;
	HBRUSH hBrush,hBrushA,hBrushB;
	HPEN hPen;
	PAINTSTRUCT PtStr;

	//设置flag为静态变量因为每次讲窗口无效化的时候 flag 回到初始值
	//设置为 static 变量可以避免这个问题
	static int flag=0;
	RECT rc;
	switch(message)
	{
	case WM_PAINT:

		//获取设备环境
		hDC = BeginPaint(hwnd,&PtStr);
		//创建画笔
		hPen = CreatePen(
			PS_SOLID,
			1,
			RGB(255,255,255)
			);

		//将画笔选入设备环境
		SelectObject(hDC,hPen);
		if(flag == 0)
		{
			//创建画刷并且将画刷选入设备环境
			hBrush = CreateSolidBrush(RGB(255,0,0));
			SelectObject(hDC,hBrush);
			//选取整个窗口
			GetClientRect(hwnd,&rc);
			//填充窗口
			FillRect(hDC,&rc,hBrush);
			//删除画刷节约资源
			DeleteObject(hBrush);
		}
		else if(flag == 1)
		{
			hBrushA = CreateSolidBrush(RGB(0,255,0));
			SelectObject(hDC,hBrushA);
			GetClientRect(hwnd,&rc);  
			FillRect(hDC,&rc,hBrushA);
			DeleteObject(hBrushA);
		}
		else if(flag == 2)
		{
			hBrushB = CreateSolidBrush(RGB(0,0,255));
			SelectObject(hDC,hBrushB);
			GetClientRect(hwnd,&rc);  
			FillRect(hDC,&rc,hBrushB);
			DeleteObject(hBrushB);
		}
		flag = (flag+1) % 3;
		//无效化窗口并且清空缓冲区
		InvalidateRect(hwnd,NULL,1);
		Sleep(1000);
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
	default:
		return DefWindowProc(hwnd,message,wParam,lParam);
	}

	return 0;
}





你可能感兴趣的:(Windows,程序设计)