﻿<?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-随笔分类-代码片段分享</title><link>http://www.cppblog.com/Error/category/20043.html</link><description /><language>zh-cn</language><lastBuildDate>Tue, 23 Oct 2012 16:27:47 GMT</lastBuildDate><pubDate>Tue, 23 Oct 2012 16:27:47 GMT</pubDate><ttl>60</ttl><item><title>3.Godofmoney的CNetClient</title><link>http://www.cppblog.com/Error/archive/2012/10/11/193177.html</link><dc:creator>Enic</dc:creator><author>Enic</author><pubDate>Thu, 11 Oct 2012 09:34:00 GMT</pubDate><guid>http://www.cppblog.com/Error/archive/2012/10/11/193177.html</guid><wfw:comment>http://www.cppblog.com/Error/comments/193177.html</wfw:comment><comments>http://www.cppblog.com/Error/archive/2012/10/11/193177.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Error/comments/commentRss/193177.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Error/services/trackbacks/193177.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 简化后的CNetClient类图：    &nbsp; 本次是分享下整体设计思想，不研究细节网络实现。 &nbsp;&nbsp;&nbsp; CNetClient用户客户端的网络处理。主要功能需求如下： *能接收远端数据 *能把接收到的数据送到上层处理 *能向远端发送数据 **发送和接收都是异步 **CNetClient本身是一个底层服务，对上层是异步的 &nbsp; 针对上述需求GodofMon...&nbsp;&nbsp;<a href='http://www.cppblog.com/Error/archive/2012/10/11/193177.html'>阅读全文</a><img src ="http://www.cppblog.com/Error/aggbug/193177.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> 2012-10-11 17:34 <a href="http://www.cppblog.com/Error/archive/2012/10/11/193177.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>2.GodOfMoney中用到的一种MFC体系的内存DC实现</title><link>http://www.cppblog.com/Error/archive/2012/10/08/192982.html</link><dc:creator>Enic</dc:creator><author>Enic</author><pubDate>Mon, 08 Oct 2012 02:41:00 GMT</pubDate><guid>http://www.cppblog.com/Error/archive/2012/10/08/192982.html</guid><wfw:comment>http://www.cppblog.com/Error/comments/192982.html</wfw:comment><comments>http://www.cppblog.com/Error/archive/2012/10/08/192982.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Error/comments/commentRss/192982.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Error/services/trackbacks/192982.html</trackback:ping><description><![CDATA[<p>////////////////////////////////////////////////////////////////</p> <p>简介：内存DC，又名“双缓冲”，是解决windows窗口自绘中出现“闪屏”的常规手段。</p> <p>1)、为屏幕 DC 创建兼容的内存 DC<br>2)、创建位图<br>3)、把位图选入设备环境<br>4)、把绘制好的图形“拷贝“到屏幕上  <p>&nbsp; <p>看这个代码：  <p>首先看构造，从一个CDC构造。然后看了一下成员函数，好像没几个，估计这是一个可以完全替换的CDC的省心的东东。</p> <p>然后看析构：析构是一个BitBit，联想自己做内存DC的时候，最后一步也是内存到DC的贴图动作。</p> <p>公开接口就两个，重载的CDC* 和 -&gt;操作，直接能当作CDC使用。</p> <p>这几个细节需要注意：</p> <p>1.m_bMemDC = !pDC-&gt;IsPrinting();&nbsp; // 以前关注不多，这是用于判断这个DC是不是用于print，如果是就不使用“内存DC”，至于为什么还不了解。我理解是没有需要。</p> <p>2.FillSolidRect(m_rect, pDC-&gt;GetBkColor());&nbsp; // WM_ERASEBKGND，针对这个消息的细节处理。</p> <p>&nbsp;</p> <p>这个类持有了一个“前台”DC，它本身是一个“后台”DC，每次后台克隆前台执行绘画然后把结果贴回去。</p> <p>这里还有一个细节，就是SelectObject。为了保证不泄漏，最好的办法是，每次工作完成将所有的GDI对象复位。</p> <p>///////////////////////////////////////////////////////////////</p> <p>class CMemDC : public CDC<br>{<br>public: </p> <p>&nbsp;&nbsp;&nbsp; // constructor sets up the memory DC<br>&nbsp;&nbsp;&nbsp; CMemDC(CDC* pDC) : CDC()<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ASSERT(pDC != NULL);  <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; m_pDC = pDC;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; m_pOldBitmap = NULL;<br>#ifndef _WIN32_WCE_NO_PRINTING<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; m_bMemDC = !pDC-&gt;IsPrinting();<br>#else<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; m_bMemDC = FALSE;<br>#endif  <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (m_bMemDC)&nbsp;&nbsp;&nbsp; // Create a Memory DC<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pDC-&gt;GetClipBox(&amp;m_rect);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CreateCompatibleDC(pDC);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; m_bitmap.CreateCompatibleBitmap(pDC, m_rect.Width(), m_rect.Height());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; m_pOldBitmap = SelectObject(&amp;m_bitmap);<br>#ifndef _WIN32_WCE<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SetWindowOrg(m_rect.left, m_rect.top);<br>#endif<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // EFW - Bug fix - Fill background in case the user has overridden<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // WM_ERASEBKGND.&nbsp; We end up with garbage otherwise.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // CJM - moved to fix a bug in the fix.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FillSolidRect(m_rect, pDC-&gt;GetBkColor());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Make a copy of the relevent parts of the current DC for printing<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>#if !defined(_WIN32_WCE) || ((_WIN32_WCE &gt; 201) &amp;&amp; !defined(_WIN32_WCE_NO_PRINTING))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; m_bPrinting = pDC-&gt;m_bPrinting;<br>#endif<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; m_hDC&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = pDC-&gt;m_hDC;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; m_hAttribDC = pDC-&gt;m_hAttribDC;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }  <p>&nbsp;&nbsp;&nbsp; }  <p>&nbsp;&nbsp;&nbsp; // Destructor copies the contents of the mem DC to the original DC<br>&nbsp;&nbsp;&nbsp; ~CMemDC()<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (m_bMemDC)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Copy the offscreen bitmap onto the screen.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; m_pDC-&gt;BitBlt(m_rect.left, m_rect.top, m_rect.Width(), m_rect.Height(),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this, m_rect.left, m_rect.top, SRCCOPY);  <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //Swap back the original bitmap.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SelectObject(m_pOldBitmap);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } else {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // All we need to do is replace the DC with an illegal value,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // this keeps us from accidently deleting the handles associated with<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // the CDC that was passed to the constructor.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; m_hDC = m_hAttribDC = NULL;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; }  <p>&nbsp;&nbsp;&nbsp; // Allow usage as a pointer<br>&nbsp;&nbsp;&nbsp; CMemDC* operator-&gt;() {return this;}<br>&nbsp;&nbsp;&nbsp; // Allow usage as a pointer<br>&nbsp;&nbsp;&nbsp; operator CMemDC*() {return this;}  <p>private:<br>&nbsp;&nbsp;&nbsp; CBitmap&nbsp; m_bitmap;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Offscreen bitmap<br>&nbsp;&nbsp;&nbsp; CBitmap* m_pOldBitmap;&nbsp; // bitmap originally found in CMemDC<br>&nbsp;&nbsp;&nbsp; CDC*&nbsp;&nbsp;&nbsp;&nbsp; m_pDC;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Saves CDC passed in constructor<br>&nbsp;&nbsp;&nbsp; CRect&nbsp;&nbsp;&nbsp; m_rect;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Rectangle of drawing area.<br>&nbsp;&nbsp;&nbsp; BOOL&nbsp;&nbsp;&nbsp;&nbsp; m_bMemDC;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // TRUE if CDC really is a Memory DC.<br>};  <img src ="http://www.cppblog.com/Error/aggbug/192982.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> 2012-10-08 10:41 <a href="http://www.cppblog.com/Error/archive/2012/10/08/192982.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>