MFC绘制的线条消失了 [英] MFC Drawed lines is disappeared

查看:247
本文介绍了MFC绘制的线条消失了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好!



我有一个问题。



我按如下方式编写按钮处理程序:

Hello!

I have a question.

I write the button handler as follows:

void CmfcDlg::OnBnClickedButton1()
{
  CClientDC dc(&this->myGraph);
  CRect rect;
  GetClientRect(&rect);
  dc.MoveTo(x1,y1);
  dc.LineTo(x2,y2);
  dc.LineTo(x3,y3);
}





当我点击按钮时会画出线条,但在我最小化窗口然后再打开它之后线条消失。



提前谢谢!



When i click on the button the lines are drawn but after i minimize the window and then open it again then the lines is disappeared.

Thanks in advance!

推荐答案

这是因为Windows GDI / GDI +图形系统不支持持久图形。您需要执行所有渲染以响应消息 WM_PAINT ,它以非常特殊的方式在消息传递系统中处理:它绕过通常的消息队列和连续消息将更改合并在一起,以尽量减少冗余。 (实际上,还有另一个消息更少使用, WM_ERASEBKGND 。)



请参阅:

http://msdn.microsoft。 com / zh-CN / library / windows / desktop / dd145213%28v = vs.85%29.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/windows/desktop/ ms648055%28v = vs.85%29.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/01c9aaty%28v=vs.80%29.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/a0a52fkz%28v=vs.80%29.aspx [ ^ ]。br />


最基本的策略如下所示:您支持图形的一些底层数据模型并将其呈现以响应 WM_PAINT 。此事件的处理程序基本上将模型的状态转换为渲染图形。当您需要更改某些图形内容时,可以更改模型。如何使用修改后的数据触发重新渲染?这是通过函数 InvalidateRect InvalidateRgn 完成的:

http://msdn.microsoft.com/en-us/library/windows/desktop /dd145005%28v=vs.85%29.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/windows/desktop/dd145002%28v=vs.85%29.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/windows/desktop/dd145003%28v=vs.85%29.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/ax04k970%28v=vs.80%29.aspx [ ^ ],

http ://msdn.microsoft.com/en-us/library/2f3csed3%28v=vs.80%29.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/532h978c%28v=vs.80%29 .aspx [ ^ ]。



另请参阅Windows和MFC API中的相关API和渲染主题。



在绘图的情况下,您仍然可以完全按照您的方式立即渲染,但如果您希望笔划保持不变,则必须将它们保存在数据模型中。当一个窗口在没有你的代码调用的情况下失效时(通常,当窗口完全或部分隐藏在其他窗口后面并再次显示时),它的显示部分将从你的数据中重新呈现。



您还应该在调整窗口大小时注意这些情况,因此您需要在适当的消息处理程序中使其无效。



此外,您可能会遇到 flicker 这样的事情,使用双缓冲可以很好地治愈。



-SA
This is because Windows GDI/GDI+ graphics system does not support persistent graphics. You need to do all your rendering in response to the message WM_PAINT, which is processed in the messaging system in a very special way: it bypasses the usual message queue, and consecutive messages merges changes together, to minimize redundancy. (Actually, there is another message use more rarely, WM_ERASEBKGND.)

Please see:
http://msdn.microsoft.com/en-us/library/windows/desktop/dd145213%28v=vs.85%29.aspx[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/ms648055%28v=vs.85%29.aspx[^],
http://msdn.microsoft.com/en-us/library/01c9aaty%28v=vs.80%29.aspx[^],
http://msdn.microsoft.com/en-us/library/a0a52fkz%28v=vs.80%29.aspx[^].

The most basic strategy looks like this: your support some under-the-hood data model of the graphics and render it in response to WM_PAINT. The handler of this event basically translates model's state into rendered graphics. When you need to change some graphics content, you change the model. How to trigger re-rendering with modified data? This is done by functions InvalidateRect or InvalidateRgn:
http://msdn.microsoft.com/en-us/library/windows/desktop/dd145005%28v=vs.85%29.aspx[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/dd145002%28v=vs.85%29.aspx[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/dd145003%28v=vs.85%29.aspx[^],
http://msdn.microsoft.com/en-us/library/ax04k970%28v=vs.80%29.aspx[^],
http://msdn.microsoft.com/en-us/library/2f3csed3%28v=vs.80%29.aspx[^],
http://msdn.microsoft.com/en-us/library/532h978c%28v=vs.80%29.aspx[^].

See also related APIs and rendering topics in Windows and MFC API.

In the case of drawing, you still can do immediate rendering, exactly the way you do, but if you want the strokes persist, saving them in your data model is a must. When a window is invalidated without your code's call (typically, it happens when a window was fully or partially hidden behind other windows and is shown again), the showing part of it will be re-rendered from your data.

You should also take care about the cases when a window is resized, so you would need to invalidate it in appropriate message handler.

Also, you may face such thing as flicker which is well cured by using double buffering.

—SA


这篇关于MFC绘制的线条消失了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆