一直对这两个消息的关系不是太了解,借重新深刻学习windows编程的机会研究一番。

1)当窗口从无效变为有效时,比方将部分覆盖的窗口恢复时会重绘窗口时:程序首先会通过发送其他消息调用DefWindowProc,它内部会发送WM_ERASEBKGND消息然后才会发送WM_PAINT消息,而且不经过消息队列(笔记:这结论从而何来?)。用Delphi的代码当场验证:

procedure TWinControl.WMSize(var Message: TWMSize);
begin
UpdateBounds; // 类函数
inherited;
Realign; // 类函数
if not (csLoading in ComponentState) then Resize; // 类函数,简单调用程序员事件
end; procedure TWinControl.WMMove(var Message: TWMMove);
begin
inherited;
UpdateBounds;
end;

果然发现两个inherited,会把WM_SIZE和WM_MOVE消息发送到DefWindowProc,从而触发WM_ERASEBKGND。

2)诸如UpdateWindow也会先调用WM_ERASEBKGND消息的处理过程,然后才会调用WM_PAINT消息的处理过程

procedure TControl.Update;
begin
if Parent <> nil then Parent.Update;
end; procedure TControl.Repaint;
var
DC: HDC;
begin
if (Visible or (csDesigning in ComponentState) and
not (csNoDesignVisible in ControlStyle)) and (Parent <> nil) and
Parent.HandleAllocated then
if csOpaque in ControlStyle then
begin
DC := GetDC(Parent.Handle);
try
IntersectClipRect(DC, Left, Top, Left + Width, Top + Height);
Parent.PaintControls(DC, Self);
finally
ReleaseDC(Parent.Handle, DC);
end;
end else
begin
Invalidate;
Update;
end;
end; procedure TWinControl.Update;
begin
if HandleAllocated then UpdateWindow(FHandle); // API,只有这一处使用这个API函数,但是有另外三处会来调用当前函数,因为这有这个API函数可以真正做到立刻刷新
end; procedure TWinControl.Repaint;
begin
Invalidate;
Update;
end;

3)当调用InvalidateRect时(三个参数:hWnd,RECT,bErase),如果bErase参数为TRUE时会先调用WM_ERASEBKGND消息的处理过程。为False时,只会发送WM_PAINT消息到队列。所以InvalidateRect不是立刻调用WM_PAINT消息的处理过程,他只是给程序窗口增加一个更新区域(参阅MSDN)。

Delphi里一共有三处调用这个API函数:

procedure TControl.InvalidateControl(IsVisible, IsOpaque: Boolean);
var
Rect: TRect;
begin
if (IsVisible or (csDesigning in ComponentState) and
not (csNoDesignVisible in ControlStyle)) and (Parent <> nil) and
Parent.HandleAllocated then
begin
Rect := BoundsRect;
InvalidateRect(Parent.Handle, @Rect, not (IsOpaque or // 注意,处理的是父窗口的句柄
(csOpaque in Parent.ControlStyle) or BackgroundClipped));
end;
end; procedure TWinControl.CMInvalidate(var Message: TMessage);
var
I: Integer;
begin
if HandleAllocated then
begin
if Parent <> nil then Parent.Perform(CM_INVALIDATE, , );
if Message.WParam = then
begin
InvalidateRect
(FHandle, nil, not (csOpaque in ControlStyle)); // 注意,处理的是自己的句柄,nil表示自己的整个客户区都重绘
{ Invalidate child windows which use the parentbackground when themed }
if ThemeServices.ThemesEnabled then
for I := to ControlCount - do
if csParentBackground in Controls[I].ControlStyle then
Controls[I].Invalidate;
end;
end;
end; procedure TWinControl.InvalidateFrame;
var
R: TRect;
begin
R := BoundsRect;
InflateRect(R, , );
InvalidateRect(Parent.FHandle, @R, True); // 也是重绘自己,但却是无条件重绘,而不需要考虑是否透明之类的问题,因为设计期也要绘制边框。其次,边框绘制出来以后,其内容可以被覆盖,因此不用管内容是否需要使用Theme
end;

另外关于WM_ERASEBKGND处理函数的返回值,如果处理WM_ERASEBKGND消息时返回FALSE,BeginPaint标记pt.fErase为TRUE, 如果处理WM_ERASEBKGND时返回TRUE,BeginPaint标记pt.fErase为FALSE。注意,Delphi就是这样做的,意思就是Delphi已经处理过了,不需要进一步处理。看这里:

procedure TWinControl.WMEraseBkgnd(var Message: TWMEraseBkgnd);
begin
with ThemeServices do
if ThemesEnabled and Assigned(Parent) and (csParentBackground in FControlStyle) then
begin
{ Get the parent to draw its background into the control's background. }
DrawParentBackground(Handle, Message.DC, nil, False);
end
else
begin
{ Only erase background if we're not doublebuffering or painting to memory. }
if not FDoubleBuffered or
(TMessage(Message).wParam = TMessage(Message).lParam) then
FillRect(Message.DC, ClientRect, FBrush.Handle);
end; Message.Result := ; // 表示已经处理过了
end;

如果pt.fErase标记为TRUE,指示应用程序应该处理背景,但是应用程序不一定需要处理,pt.fErase只是作为一个标记(笔记:留给程序员的灵活性依然很强)。

http://www.aichengxu.com/view/2426114

终于懂了:WM_PAINT 与 WM_ERASEBKGND(三种情况:用户操作,UpdateWindow,InvalidateRect产生的效果并不相同),并且用Delphi代码验证 good的更多相关文章

  1. Spring如何使用JdbcTemplate调用存储过程的三种情况

    注:原文 <Spring如何使用JdbcTemplate调用存储过程的三种情况 > Spring的SimpleJdbcTemplate将存储过程的调用进行了良好的封装,下面列出使用Jdbc ...

  2. Tomcat内存溢出的三种情况及解决办法分析

    Tomcat内存溢出的原因 在生产环境中tomcat内存设置不好很容易出现内存溢出.造成内存溢出是不一样的,当然处理方式也不一样. 这里根据平时遇到的情况和相关资料进行一个总结.常见的一般会有下面三种 ...

  3. TI C66x DSP 系统events及其应用 - 5.10(创建ISR的三种情况)

    ISFP是服务中断的指令包,创建ISR的三种情况: 1.一个ISFP包的8条指令就能够满足ISR watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveWl5ZW ...

  4. SSO单点登录三种情况的实现方式详解

    单点登录(SSO——Single Sign On)对于我们来说已经不陌生了.对于大型系统来说使用单点登录可以减少用户很多的麻烦.就拿百度来说吧,百度下面有很多的子系统——百度经验.百度知道.百度文库等 ...

  5. .net webapi 接收 xml 格式数据的三种情况

    webapi 接收 xml 的三种方法 前段时间接到一个任务写一个小接口,要接收java端返回过来的短信xml数据. 刚拿到项目,我的第一想法是对方会以什么形式发送xml格式的数据给我呢,设想三种情况 ...

  6. DG备库,实时应用如何判断,MR进程,及MRP应用归档,三种情况的查询及验证

    本篇文档学习,DG备库,实时应用如何判断,MR进程,及MRP应用归档,三种情况的查询及验证 1.取消MRP进程 备库查询进程状态select process,client_process,sequen ...

  7. 对象的notify方法的含义和对象锁释放的三种情况

    1,notify的含义     (1)notify一次只随机通知一个线程进行唤醒 (2)在执行了notify方法之后,当前线程不会马上释放该对象锁,呈wait状态的线程也不能马上获得该对象锁, 要等到 ...

  8. SSO单点登录三种情况的实现方式详解(转载)

    单点登录(SSO——Single Sign On)对于我们来说已经不陌生了.对于大型系统来说使用单点登录可以减少用户很多的麻烦.就拿百度来说吧,百度下面有很多的子系统——百度经验.百度知道.百度文库等 ...

  9. Java实现PV操作 | 读者与写者(在三种情况下进行讨论)

    注 :本文应结合[天勤笔记]进行学习. 1.读者优先 设置rmutex信号量来对readcount变量进行互斥访问.mutex信号量对写者与读者进行同步. static syn rmutex=new ...

  10. js中的数据类型隐式转换的三种情况

    js的数据类型隐式转换主要分为三种情况: 1. 转换为boolean类型 2. 转换为number类型 3. 转换为string类型 转换为boolean类型 数据在 逻辑判断 和 逻辑运算 之中会隐 ...

随机推荐

  1. ContentProvider的一些总结

    ContentProvider中的URI, The URI that identifies the provider   一个特定的uri对应着唯一一个内容提供者, 谷歌官方文档里的说明, Query ...

  2. PyQt主窗体设置停靠窗口(QDockWidget)的叠加顺序

    PyQt提供了方便的停靠窗口控件,我们可以很方便的编写一个停靠窗口,代码和效果如下: # -*- coding: utf-8 -*-from PyQt4 import QtGui, QtCore cl ...

  3. POJ 3261 可重叠的 k 次最长重复子串【后缀数组】

    这也是一道例题 给定一个字符串,求至少出现 k 次的最长重复子串,这 k 个子串可以重叠.算法分析:这题的做法和上一题差不多,也是先二分答案,然后将后缀分成若干组.不同的是,这里要判断的是有没有一个组 ...

  4. HDU 3917 最大权闭合图 求最小割

    具体参考http://blog.csdn.net/power721/article/details/6665750 TODO //#pragma comment(linker, "/STAC ...

  5. 关于ActionContext.getContext()的使用方法心得

    这个也是我在另外一位仁兄的博客中看到的,原博客的有点长,我把它精简了一下,算看起来比較方便吧. 为了避免与Servlet API耦合在一起,方便Action类做单元測试,Struts 2对HttpSe ...

  6. 另一种数据库连接字符串的编写方式(Sqlbuilder)

    SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(); builder.DataSource = "&q ...

  7. GCD基本使用

    主要概念 队列 dispatch_queue_t,队列名称在调试时辅助,无论什么队列和任务,线程的创建和回收不需要程序员操作,有队列负责. 串行队列:队列中的任务只会顺序执行(类似跑步) dispat ...

  8. iphone开发,模拟器生成文件路径

    原文链接:http://blog.csdn.net/gf771115/article/details/7722023 Finder---位置----apple(用户名)---application s ...

  9. git教程1

    主要参考: 官方书籍: Pro Git 中文版:http://git.perlchina.org/book/zh 英文版:http://git.perlchina.org/book http://gi ...

  10. cairo graphics.org

    cairographics.org Latest news: 2013-08-26: cairo 1.12.16 snapshot available 2013-02-10: cairo 1.12.1 ...