﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-Error-随笔分类-win32 gui</title><link>http://www.cppblog.com/Error/category/20480.html</link><description /><language>zh-cn</language><lastBuildDate>Mon, 10 Mar 2014 22:16:44 GMT</lastBuildDate><pubDate>Mon, 10 Mar 2014 22:16:44 GMT</pubDate><ttl>60</ttl><item><title>OnPaint和OnDraw的区别 </title><link>http://www.cppblog.com/Error/archive/2014/03/10/206129.html</link><dc:creator>Enic</dc:creator><author>Enic</author><pubDate>Mon, 10 Mar 2014 10:12:00 GMT</pubDate><guid>http://www.cppblog.com/Error/archive/2014/03/10/206129.html</guid><wfw:comment>http://www.cppblog.com/Error/comments/206129.html</wfw:comment><comments>http://www.cppblog.com/Error/archive/2014/03/10/206129.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Error/comments/commentRss/206129.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Error/services/trackbacks/206129.html</trackback:ping><description><![CDATA[<div id="article_content"> <p><span style="font-family: Verdana; font-size: 10pt"><span style="font-family: 宋体; font-size: 10pt">问题：我在视图画的图象或者文字，当窗口改变后为什么不见了？</span><span style="font-size: 10pt;">OnDraw()</span><span style="font-family: 宋体; font-size: 10pt">和</span><span style="font-size: 10pt;">OnPaint()</span><span style="font-family: 宋体; font-size: 10pt">两个都是解决上面的问题，有什么不同？</span><span style="font-size: 10pt;"><br /><br /></span><span style="font-family: 宋体; font-size: 10pt">答：</span><span style="font-size: 10pt;">OnDraw()</span><span style="font-family: 宋体; font-size: 10pt">和</span><span style="font-size: 10pt;">OnPaint()</span><span style="font-family: 宋体; font-size: 10pt">好象兄弟俩，因为它们的工作类似。</span><span style="font-size: 10pt;"><br /></span><span style="font-family: 宋体; font-size: 10pt">至于不见了的问题简单，因为当你的窗口改变后，会产生无效区域，这个无效的区域需要重画。一般</span><span style="font-size: 10pt;">Windows</span><span style="font-family: 宋体; font-size: 10pt">会发送两个消息</span><span style="font-size: 10pt;">WM_PAINT</span><span style="font-family: 宋体; font-size: 10pt">（通知客户区</span> <span style="font-family: 宋体; font-size: 10pt">有变化）和</span><span style="font-size: 10pt;">WM_NCPAINT</span><span style="font-family: 宋体; font-size: 10pt">（通知非客户区有变化）。非客户区的重画系统自己搞定了，而客户区的重画需要我们自己来完成。这就需要</span><span style="font-size: 10pt;">OnDraw()</span><span style="font-family: 宋体; font-size: 10pt">或</span><span style="font-size: 10pt;"> OnPaint()</span><span style="font-family: 宋体; font-size: 10pt">来重画窗口。</span><span style="font-size: 10pt;"><br /><br />OnDraw()</span><span style="font-family: 宋体; font-size: 10pt">和</span><span style="font-size: 10pt;">OnPaint()</span><span style="font-family: 宋体; font-size: 10pt">有什么区别呢？首先：我们先要明确</span><span style="font-size: 10pt;">CView</span><span style="font-family: 宋体; font-size: 10pt">类派生自</span><span style="font-size: 10pt;">CWnd</span><span style="font-family: 宋体; font-size: 10pt">类。而</span><span style="font-size: 10pt;">OnPaint()</span><span style="font-family: 宋体; font-size: 10pt">是</span><span style="font-size: 10pt;">CWnd</span><span style="font-family: 宋体; font-size: 10pt">的类成员，同时负责响应</span><span style="font-size: 10pt;">WM_PAINT</span><span style="font-family: 宋体; font-size: 10pt">消息。</span><span style="font-size: 10pt;">OnDraw()</span><span style="font-family: 宋体; font-size: 10pt">是</span><span style="font-size: 10pt;">CVIEW</span><span style="font-family: 宋体; font-size: 10pt">的成员函数，并且没有响应消息的功能。这就是为什么你用</span><span style="font-size: 10pt;">VC</span><span style="font-family: 宋体; font-size: 10pt">成的程序代码时，在视图类只有</span><span style="font-size: 10pt;"> OnDraw</span><span style="font-family: 宋体; font-size: 10pt">没有</span><span style="font-size: 10pt;">OnPaint</span><span style="font-family: 宋体; font-size: 10pt">的原因。</span><span style="font-size: 10pt;"><br /><br /></span><span style="font-family: 宋体; font-size: 10pt">其次，要想在屏幕上绘图或显示图形，首先需要建立设备环境</span><span style="font-size: 10pt;">DC</span><span style="font-family: 宋体; font-size: 10pt">。其实</span><span style="font-size: 10pt;">DC</span><span style="font-family: 宋体; font-size: 10pt">是一个数据结构，它包含输</span> <span style="font-family: 宋体; font-size: 10pt">出设备（不单指你</span><span style="font-size: 10pt;">17</span><span style="font-family: 宋体; font-size: 10pt">寸的纯屏显示器，还包括打印机之类的输出设备）的绘图属性的描述。</span><span style="font-size: 10pt;">MFC</span><span style="font-family: 宋体; font-size: 10pt">提供了</span><span style="font-size: 10pt;">CPaintDC</span><span style="font-family: 宋体; font-size: 10pt">类和</span><span style="font-size: 10pt;">CWindwoDC</span><span style="font-family: 宋体; font-size: 10pt">类来实时的响</span> <span style="font-family: 宋体; font-size: 10pt">应，而</span><span style="font-size: 10pt;">CPaintDC</span><span style="font-family: 宋体; font-size: 10pt">支持重画。</span><span style="font-size: 10pt;"><br /><br /></span><span style="font-family: 宋体; font-size: 10pt">当视图变得无效时（包括大小的改变，移动，被遮盖等等），</span><span style="font-size: 10pt;">Windows </span><span style="font-family: 宋体; font-size: 10pt">将</span><span style="font-size: 10pt;"> WM_PAINT </span><span style="font-family: 宋体; font-size: 10pt">消息发送给它。该视图的</span><span style="font-size: 10pt;"> OnPaint </span><span style="font-family: 宋体; font-size: 10pt">处理函数通过创建</span><span style="font-size: 10pt;"> CPaintDC </span><span style="font-family: 宋体; font-size: 10pt">类的</span><span style="font-size: 10pt;">DC</span><span style="font-family: 宋体; font-size: 10pt">对象来响应该消息并调用视图的</span><span style="font-size: 10pt;"> OnDraw </span><span style="font-family: 宋体; font-size: 10pt">成员函数。通常我们不必编写重写的</span><span style="font-size: 10pt;"> OnPaint </span><span style="font-family: 宋体; font-size: 10pt">处理成员函数。</span><span style="font-size: 10pt;"><br /><br />///CView</span><span style="font-family: 宋体; font-size: 10pt">默认的标准的重画函数<br /></span></span></p> <div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><span style="color: #0000ff">void</span>&nbsp;CView::OnPaint()<br />{ <br />&nbsp;&nbsp;&nbsp; CPaintDC&nbsp;dc(<span style="color: #0000ff">this</span>);<br />&nbsp;&nbsp;&nbsp;  OnPreparDC(&amp;dc)；<br />&nbsp;&nbsp;&nbsp; OnDraw(&amp;dc);&nbsp;<span style="color: #008000">//</span><span style="color: #008000">调用了OnDraw<br />}<br /></span></div><br /><span style="font-family: Verdana; font-size: 10pt"><br /></span><span style="font-family: 宋体; font-size: 10pt">既然</span><span style="font-family: Verdana; font-size: 10pt">OnPaint</span><span style="font-family: 宋体; font-size: 10pt">最后也要调用</span><span style="font-family: Verdana; font-size: 10pt">OnDraw,</span><span style="font-family: 宋体; font-size: 10pt">因此我们一般会在</span><span style="font-family: Verdana; font-size: 10pt">OnDraw</span><span style="font-family: 宋体; font-size: 10pt">函数中进行绘制。下面是一个典型的程序</span><span style="font-family: Verdana; font-size: 10pt"><br /><br /> <div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; width: 98%;"><span style="color: #808080">///</span><span style="color: #008000">视图中的绘图代码首先检索指向文档的指针，然后通过DC进行绘图调用。</span><span style="color: #808080"><br /></span><span style="color: #0000ff">void</span>&nbsp;CMyView::OnDraw(&nbsp;CDC*&nbsp;pDC&nbsp;)<br />{&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;CMyDoc*&nbsp;pDoc&nbsp;=&nbsp;GetDocument();&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;CString&nbsp;s&nbsp;=&nbsp;pDoc-&gt;GetData();&nbsp;<span style="color: #008000">//</span><span style="color: #008000">&nbsp;Returns&nbsp;a&nbsp;CString</span><span style="color: #008000"><br /></span>&nbsp;&nbsp;&nbsp;  CRect&nbsp;rect;<br />&nbsp;&nbsp;&nbsp; GetClientRect(&nbsp;&amp;rect&nbsp;);&nbsp;<br />&nbsp;&nbsp;&nbsp;  pDC-&gt;SetTextAlign(&nbsp;TA_BASELINE&nbsp;|&nbsp;TA_CENTER&nbsp;);&nbsp;<br />&nbsp;&nbsp;&nbsp; pDC-&gt;TextOut(&nbsp;rect.right&nbsp;/&nbsp;2,&nbsp;rect.bottom&nbsp;/&nbsp;2,&nbsp;s,&nbsp;s.GetLength()&nbsp;);<br />}</div> <p><span style="font-family: 宋体; font-size: 10pt">最后：现在大家明白这哥俩之间的关系了吧。因此我们一般用</span><span style="font-size: 10pt;">OnPaint</span><span style="font-family: 宋体; font-size: 10pt">维护窗口的客户区（例如我们的窗口客户区加一个背景图片），用</span><span style="font-size: 10pt;">OnDraw</span><span style="font-family: 宋体; font-size: 10pt">维护视图的客户区（例如我们通过鼠标在视图中画图）。当然你也可以不按照上面规律来，只要达到目的并且没有问题，怎么干都成。</span><span style="font-size: 10pt;"><br /><br /></span><span style="font-family: 宋体; font-size: 10pt">补充：我们还可以利用</span><span style="font-size: 10pt;">Invalidate(),ValidateRgn(),ValidateRect()</span><span style="font-family: 宋体; font-size: 10pt">函数强制的重画窗口</span></p></span> <p>&nbsp;</p></div><img src ="http://www.cppblog.com/Error/aggbug/206129.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Error/" target="_blank">Enic</a> 2014-03-10 18:12 <a href="http://www.cppblog.com/Error/archive/2014/03/10/206129.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>import_gdiplus.h</title><link>http://www.cppblog.com/Error/archive/2013/04/19/199558.html</link><dc:creator>Enic</dc:creator><author>Enic</author><pubDate>Thu, 18 Apr 2013 16:39:00 GMT</pubDate><guid>http://www.cppblog.com/Error/archive/2013/04/19/199558.html</guid><wfw:comment>http://www.cppblog.com/Error/comments/199558.html</wfw:comment><comments>http://www.cppblog.com/Error/archive/2013/04/19/199558.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Error/comments/commentRss/199558.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Error/services/trackbacks/199558.html</trackback:ping><description><![CDATA[<div><div>// filename: import_gdiplus.h</div><div>// author: enic</div><div>// date: 2013-04-18</div><div></div><div>#pragma once</div><div></div><div>#include &lt;GdiPlus.h&gt;</div><div></div><div>#pragma comment(lib,"GdiPlus.lib")</div><div></div><div>namespace {</div><div></div><div>class CAutoInitGdiPlusEnvionment</div><div>{</div><div>public:</div><div>&nbsp; &nbsp; CAutoInitGdiPlusEnvionment()</div><div>&nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; Gdiplus::GdiplusStartup(&amp;m_gdiplusToken, &amp;m_gdiplusStartupInput, NULL);</div><div>&nbsp; &nbsp; }</div><div>&nbsp; &nbsp;&nbsp;</div><div>&nbsp; &nbsp; ~CAutoInitGdiPlusEnvionment()</div><div>&nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; Gdiplus::GdiplusShutdown(m_gdiplusToken);</div><div>&nbsp; &nbsp; }</div><div>&nbsp; &nbsp;&nbsp;</div><div>private:</div><div>&nbsp; &nbsp; Gdiplus::GdiplusStartupInput m_gdiplusStartupInput;</div><div>&nbsp; &nbsp; ULONG_PTR m_gdiplusToken;</div><div>};</div><div></div><div>static CAutoInitGdiPlusEnvionment autoInitGdiPlus;</div><div></div><div>};</div><div></div><div></div></div><img src ="http://www.cppblog.com/Error/aggbug/199558.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Error/" target="_blank">Enic</a> 2013-04-19 00:39 <a href="http://www.cppblog.com/Error/archive/2013/04/19/199558.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>