SDK问题?消息WM_ERASEBKGND的处理,能解释一下背景的处理吗?问题在下面,谢谢!

leemuxiang 2001-07-20 10:19:59
// 程序使用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);
}


...全文
347 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
tiongkohlang 2001-07-27
  • 打赏
  • 举报
回复
第一个问题的现象可以理解。
第二个问题我不明白,可以用spy++或者别的东西看一看。
leemuxiang 2001-07-25
  • 打赏
  • 举报
回复
to all thanks.!
leemuxiang 2001-07-23
  • 打赏
  • 举报
回复
再问几个问题?
问题一:下面是WM_PAINT的处理程序
hdc=BeginPaint(hWnd,&ps);
hdc1=GetWindowDC(hWnd);
GetClientRect(hWnd,&rect);// ==>rect声明为:static RECT rect;
SetTextColor (hdc,RGB(255,0,0));
SetBkColor(hdc,RGB(128,128,128));
TextOut(hdc1,0,0,szBuffer,wsprintf(szBuffer,TEXT("hdc:%5d"),hdc));
TextOut(hdc1,cxChar*20,0,szBuffer,wsprintf(szBuffer,TEXT("hWnd:%5d"),hWnd));
// 在标题处显示hdc & hWnd.
ReleaseDC(hWnd,hdc1);
EndPaint(hWnd,&ps);
return 0;
在一个菜单的处理程序中调用:InvalidateRect(hWnd,&rect,TRUE);
可以观察到hdc在变化,应该变化吗?为什么?(我的疑问来自hWnd一直没变?)

问题二:在窗口改变大小后,窗体客户区,及背景均重绘,
WM_PAINT在WM_ERASEBKGND之前执行?
不好意思:能从一个HDC知道其属主窗口(能这么说吗?)的HWND吗?

to all 谢谢!
leemuxiang 2001-07-23
  • 打赏
  • 举报
回复
谢谢!
原来返回非零值,试试!
tiongkohlang 2001-07-21
  • 打赏
  • 举报
回复
“需要在处理程序中建立一个刷子吗?就象MFC中一样?”

不用不用,你干什么都行。你贴一张位图也行,你画一个渐变地背景也行,怎么都行。只要你返回一个非零值就可以。那个WNDCLASSEX的hbrBackground是DefWindowProc用来刷背景的。

以下是msdn的解释。

WM_ERASEBKGND
The WM_ERASEBKGND message is sent when the window background must be erased (for example, when a window is resized). The message is sent to prepare an invalidated portion of a window for painting.

A window receives this message through its WindowProc function.

LRESULT CALLBACK WindowProc(
HWND hwnd, // handle to window
UINT uMsg, // WM_ERASEBKGND
WPARAM wParam, // handle to device context (HDC)
LPARAM lParam // not used
);
Parameters
wParam
Handle to the device context.
lParam
This parameter is not used.
Return Values
An application should return nonzero if it erases the background; otherwise, it should return zero.

Remarks
The DefWindowProc function erases the background by using the class background brush specified by the hbrBackground member of the WNDCLASS structure. If hbrBackground is NULL, the application should process the WM_ERASEBKGND message and erase the background.

An application should return nonzero in response to WM_ERASEBKGND if it processes the message and erases the background; this indicates that no further erasing is required. If the application returns zero, the window will remain marked for erasing. (Typically, this indicates that the fErase member of the PAINTSTRUCT structure will be TRUE.)
MSVCer 2001-07-21
  • 打赏
  • 举报
回复
WM_ERASEBKGND信息的参数wParam传递的是一个设备上下文句柄,有了句柄,怎么画就随你便了
leemuxiang 2001-07-21
  • 打赏
  • 举报
回复
to MSVCer:您好:我需要在处理程序中建立一个刷子吗?就象MFC中一样?
不明白怎么去做!例如我要画渐变色的背景?
另外,能给一个处理此消息的例程吗?很需要清楚到底在窗口改变后,
此消息的响应如何工作?
谢谢

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧