Chapter 4 An Exercise in Text Output 第四章 文本输出练习 — Painting and Repainting - 绘制与重绘

ps:您可以转载,但请注明出处;你可以修改,但请将修改结果告诉我。
 
Painting and Repainting
绘制与重绘
 
In character-mode environments, programs can generally write to any part of the video display. What the program puts on the display will stay there and not mysteriously disappear. The program can then discard the information needed to re-create the screen display.
在字符模式环境下,程序通常可以在视频显示器的任何部分输出。程序在显示器上的输入将停留在那儿而不会神秘消失。因而程序可以丢掉重新创建屏幕显示所需要的信息。
 
In Windows, you can draw text and graphics only in the client area of your window, and you cannot be assured that what you put will remain there until your program specifically writes over it. For instance, the user may move another program's window on the screen so that it partially covers your application's window. Windows will not attempt to save the area of your window that the other program covers. When the program is moved away, Windows will request that your program repaint this portion of your client area.
在 Windows 中,你只能在窗口的客户区绘制文本和图形,而且你不能确定你的输出将保留在那儿直到你的程序明确地输出覆盖它。例如,用户可能在屏幕上移动另一个程序的窗口以致它部分覆盖你的应用程序的窗口。Windows 不能尝试保留其它程序覆盖的窗口区域。当程序移开时,Windows 将要求你的程序重绘客户区的这部分。
 
Windows is a message-driven system. Windows informs applications of various events by posting messages in the application's message queue or sending messages to the appropriate window procedure. Windows informs a window procedure that part of the window's client area needs painting by posting a WM_PAINT message.
Windows 是消息驱动系统。Windows 通过在应用程序的消息队列粘帖消息或发送消息到适当的窗口处理程序来通知应用程序不同的事件。Windows 通过粘帖 WM_PAINT 消息通知窗口处理程序窗口客户区的某部分需要绘制。
 
WM_PAINT 消息
 
Most Windows programs call the function UpdateWindow during initialization in WinMain shortly before entering the message loop. Windows takes this opportunity to send the window procedure its first WM_PAINT message. This message informs the window procedure that the client area must be painted. Thereafter, that window procedure should be ready at almost any time to process additional WM_PAINT messages and even to repaint the entire client area of the window if necessary. A window procedure receives a WM_PAINT message whenever one of the following events occurs:
在进入消息循环之前,大多数 Windows 程序在 WinMain 初始化期间都要调用函数 UpdateWindow。Windows 利用这个机会将第一个 WM_PAINT 消息发送给窗口处理程序。该消息通知窗口处理程序客户区必须被绘制。其后,窗口处理程序应该准备在几乎任何时间处理额外的 WM_PAINT 消息,如果必要的话甚至重绘窗口的整个客户区。下列事件中的任意一个无论何时发生,窗口处理程序都会接收到一个 WM_PAINT 消息:
  • A previously hidden area of the window is brought into view when a user moves a window or uncovers a window.
  • 当用户移开窗口或不覆盖窗口时前一个隐藏的窗口可见。
  • The user resizes the window (if the window class style has the CS_HREDRAW and CS_VREDRAW bits set).
  • 用户改变窗口尺寸(如果窗口类风格设置为 CS_HREDRAW 和 CS_VREDRAW)。
  • The program uses the ScrollWindow or ScrollDC function to scroll part of its client area.
  • 程序用 ScrollWindow 和 ScrollDC 函数控制客户区的部分。
  • The program uses the InvalidateRect or InvalidateRgn function to explicitly generate a WM_PAINT message.
  • 程序用 InvalidateRect 或 InvalidateRgn 函数明确地产生 WM_PAINT 消息。
In some cases when part of the client area is temporarily written over, Windows attempts to save an area of the display and restore it later. This is not always successful. Windows may sometimes post a WM_PAINT message when:
在一些实例中,当部分客户区临时被覆盖时,Windows 尝试保存显示区域并在以后恢复它。这不总是成功的。当下列发生时 Windows 有时可能发送一个 WM_PAINT 消息:
  • Windows removes a dialog box or message box that was overlaying part of the window.
  • Windows 移开覆盖部分窗口的对话框或消息框。

     

  • A menu is pulled down and then released.
  • 菜单被展开然后被释放。

  • A tool tip is displayed.
  • 工具提示被显示。
In a few cases, Windows always saves the area of the display it overwrites and then restores it. This is the case whenever:
在很少的实例中,Windows 总是保存它覆盖的显示区域然后恢复它。这些情况是:

  • The mouse cursor is moved across the client area.
  • 鼠标指针移过客户区。

     

  • An icon is dragged across the client area.
  • 图标被拖过客户区。
Dealing with WM_PAINT message requires that you alter the way you think about how you write to the video display. Your program should be structured so that it accumulates all the information necessary to paint the client area but paints only "on demand"—when Windows sends the window procedure a WM_PAINT message. If your program needs to update its client area at some other time, it can force Windows to generate this WM_PAINT message. This may seem a roundabout method of displaying something on the screen, but the structure of your program will benefit from it.
处理 WM_PAINT 消息需要你改变考虑如何输出到视频显示器的方式。你的程序应该组织起来以便积聚所有绘制客户区需要的信息,但仅在需要时——当 Windows 发送 WM_PAINT 消息给窗口处理程序时——绘制。如果你的程序需要在其它某些时候更新客户区,那么它可以强制 Windows 产生 WM_PAINT 消息。这看来了可能是在屏幕上显示的迂回方法,但你的程序结构将会因此受益。
 
Although a window procedure should be prepared to update the entire client area whenever it receives a WM_PAINT message, it often needs to update only a smaller area, most often a rectangular area within the client area. This is most obvious when a dialog box overlies part of the client area. Repainting is required only for the rectangular area uncovered when the dialog box is removed.
尽管窗口处理程序应该准备好在接收到 WM_PAINT 消息的任何时候更新整个客户区,但是它常常只需要更新一小部分区域,大多数是客户区中的矩形区域。这在对话框覆盖部分客户区时尤其明显。重画只需要对对话框移开时显示的矩形区域进行。
 
That area is known as an "invalid region" or "update region." The presence of an invalid region in a client area is what prompts Windows to place a WM_PAINT message in the application's message queue. Your window procedure receives a WM_PAINT message only if part of your client area is invalid.
这个区域称为“无效区域”或“更新区域”。客户区中无效区域的存在指示 Windows 将 WM_PAINT 消息放置到应用程序的消息队列。只有在部分客户区无效时你的窗口处理程序才会接收到 WM_PAINT 消息。
 
Windows internally maintains a "paint information structure" for each window. This structure contains, among other information, the coordinates of the smallest rectangle that encompasses the invalid region. This is known as the "invalid rectangle." If another region of the client area becomes invalid before the window procedure processes a pending WM_PAINT message, Windows calculates a new invalid region (and a new invalid rectangle) that encompasses both areas and stores this updated information in the paint information structure. Windows does not place multiple WM_PAINT messages in the message queue.
Windows 自行为每个窗口维护一个“绘制信息结构”。该结构包含了环绕无效区域的最小矩形的坐标和其它信息,称作“无效矩形”。如果另一个客户区区域在窗口处理程序处理未处理的 WM_PAINT 消息之前变成无效的,那么 Windows 将计算环绕这两个区域的新的无效区域(和新的无效矩形),并将这些最新的信息存储在绘制信息结构中。Windows 不会在消息队列中放置多个 WM_PAINT 消息。
 
A window procedure can invalidate a rectangle in its own client area by calling InvalidateRect. If the message queue already contains a WM_PAINT message, Windows calculates a new invalid rectangle. Otherwise, it places a WM_PAINT message in the message queue. A window procedure can obtain the coordinates of the invalid rectangle when it receives a WM_PAINT message (as we'll see later in this chapter). It can also obtain these coordinates at any other time by calling GetUpdateRect.
窗口处理程序通过调用 InvalidateRect 能使它自己客户区中的矩形无效。如果消息队列已经包含了 WM_PAINT 消息,那么 Windwos 计算新的无效矩形。否则,它将在消息队列中放置一个 WM_PAINT 消息。当窗口处理程序接收到 WM_PAINT 消息时(正如我们将在本章后面看到的),它可以获得无效矩形的坐标。它也可以通过调用 GetUpdateRect 在任何其它时候获得这些坐标。
 
After the window procedure calls BeginPaint during the WM_PAINT message, the entire client area is validated. A program can also validate any rectangular area within the client area by calling the ValidateRect function. If this call has the effect of validating the entire invalid area, then any WM_PAINT message currently in the queue is removed.
在处理 WM_PAINT 消息期间,窗口处理程序调用 BeginPaint 之后,整个客户区是有效的。程序也可以通过调用 ValidateRect 函数使客户区中的任何矩形区域有效。如果该函数调用有使整个无效区域有效的效果,那么当前消息队列中的任何 WM_PAINT 消息将被删除。
 
 
Valid and Invalid Rectangles
有效和无效的矩形
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值