SDK问题?消息WM_ERASEBKGND的处理,能解释一下背景的处理吗?问题在下面,谢谢!
// 程序使用MFC头文件,须MFC支持!
#include "afxwin.h"
#include "resource.h"// 资源
//#include "sysmets.h"
LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine,int iShowCmd)
{
static TCHAR szAppName[]=TEXT("Sys");
HWND hWnd;
MSG msg;
WNDCLASS wc;
wc.style =CS_HREDRAW|CS_VREDRAW;
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);
// 《==如果参数定义为NULL,将导致背景不会被擦除!!!
wc.lpszMenuName ="menu";
wc.lpszClassName =szAppName;
if(!RegisterClass(&wc))
{
MessageBox(NULL,
TEXT("This program requires Windows NT!"),szAppName,MB_ICONERROR);
return 0;
}
hWnd=CreateWindow(szAppName,
TEXT("Get System Metrics No.1"),
WS_OVERLAPPEDWINDOW,
0x80000000,
0x80000000,
0x80000000,
0x80000000,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hWnd,iShowCmd);
UpdateWindow(hWnd);
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
static int cxChar,cxCaps,cyChar;
HDC hdc;
int i;
PAINTSTRUCT ps;
TCHAR szBuffer[10];
TEXTMETRIC tm;
RECT rect;
// ps解释
// 不定义参数wc.hbrBackground,将导致客户区背景不会被擦除,
// 参数的定义将导致ps.fErease=0;
// 如果要在窗口中自己定义一个背景的擦除,可以处理处理WM_ERASEBKEND消息
// ps.rcPaint:导致无效的客户区大小
// ps.fRestore:windows内部使用
// ps.IncUpdate:windows内部使用
// ps.rgbReserved:windows使用,缺省值为:6683512?
switch(message)
{
case WM_CREATE:
hdc=GetDC(hWnd);
GetTextMetrics(hdc,&tm);
cxChar=tm.tmAveCharWidth ;
cxCaps=(tm.tmPitchAndFamily &1?3:2)*cxChar/2;
cyChar=tm.tmHeight +tm.tmExternalLeading ;
ReleaseDC(hWnd,hdc);
return 0;
// WM_ERASEBKGND消息处理但没有指定刷子,将导致背景擦除无效!
// **************************************************************
// 问题一:如果我要把背景设置为一个渐变色,该如何做?或者设置为红色背景
// ***************************************************************
/* case WM_ERASEBKGND:
// 怎么做?
return 0;
*/
// ***************************************************************
case WM_PAINT:
hdc=BeginPaint(hWnd,&ps);
// 下面是ps结构的值显示
TextOut(hdc,0,0,szBuffer,wsprintf(szBuffer,TEXT("%5d"),hdc));
TextOut(hdc,0,10+cxChar*2,
szBuffer,wsprintf(szBuffer,TEXT("%5d"),ps.hdc ));
TextOut(hdc,0,20+cxChar*3,
szBuffer,wsprintf(szBuffer,TEXT("%5d"),ps.fErase ));
TextOut(hdc,0,30+cxChar*4,
szBuffer,wsprintf(szBuffer,TEXT("%5d %5d %5d %5d"),ps.rcPaint .left,
ps.rcPaint .right ,ps.rcPaint .top ,ps.rcPaint .bottom ));
TextOut(hdc,0,40+cxChar*5,szBuffer,wsprintf(szBuffer,TEXT("%5d"),ps.fRestore ));
TextOut(hdc,0,50+cxChar*6,szBuffer,wsprintf(szBuffer,TEXT("%5d"),ps.fIncUpdate ));
TextOut(hdc,0,60+cxChar*7,szBuffer,wsprintf(szBuffer,TEXT("%5d"),ps.rgbReserved ));
/* for(i=0;i<NUMLINES;i++)
{
TextOut(hdc,0,cyChar*(i+8),sysmetrics[i].szLabel,lstrlen(sysmetrics[i].szLabel));
TextOut(hdc,cxCaps*22,cyChar*(i+8),sysmetrics[i].szDesc,lstrlen(sysmetrics[i].szDesc));
SetTextAlign(hdc,TA_RIGHT|TA_TOP);
TextOut(hdc,22*cxCaps+40*cxChar,cyChar*(i+8),szBuffer,wsprintf(szBuffer,TEXT("%5d"),GetSystemMetrics(sysmetrics[i].iIndex)));
SetTextAlign(hdc,TA_LEFT|TA_TOP);
}*/
EndPaint(hWnd,&ps);
return 0;
case WM_COMMAND:
GetClientRect(hWnd,&rect);
rect.top =rect.top;
rect.left =rect.left;
rect.right =(rect.right-rect.left ) /2;
rect.bottom =(rect.bottom -rect.top )/2;
{
switch(LOWORD(wParam))
{
case ID_TEST:
InvalidateRect(hWnd,&rect,TRUE);
// 第四个参数为TRUE!!!
break;
case ID_QUIT:
DestroyWindow(hWnd);
break;
default:
return (DefWindowProc(hWnd,message,wParam,lParam));
}
break;
}
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd,message,wParam,lParam);
}