<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Matthias Pospiech &#187; MFC</title>
	<atom:link href="http://www.matthiaspospiech.de/blog/category/c-plusplus/mfc/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.matthiaspospiech.de</link>
	<description></description>
	<lastBuildDate>Mon, 31 May 2010 19:37:04 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Threads in MFC</title>
		<link>http://www.matthiaspospiech.de/blog/2007/09/09/threads-in-mfc/</link>
		<comments>http://www.matthiaspospiech.de/blog/2007/09/09/threads-in-mfc/#comments</comments>
		<pubDate>Sun, 09 Sep 2007 15:09:10 +0000</pubDate>
		<dc:creator>pospiech</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[MFC]]></category>

		<guid isPermaLink="false">http://www.matthiaspospiech.de/blog/2007/09/09/threads-in-mfc/</guid>
		<description><![CDATA[While searching for documentation about Threads in MFC I found the following:

	Threads in MFC Part I: Worker Threads
	Threads with MFC




No related posts.


No related posts.]]></description>
			<content:encoded><![CDATA[<p>While searching for documentation about Threads in MFC I found the following:</p>
<ul>
	<li><a href="http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=435" class="liexternal">Threads in MFC Part I: Worker Threads</a></li>
	<li><a href="http://www.codeproject.com/threads/threads_and_mfc.asp" class="liexternal">Threads with MFC</a></li>
</ul>



<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.matthiaspospiech.de/blog/2007/09/09/threads-in-mfc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Check value of Radiobutton</title>
		<link>http://www.matthiaspospiech.de/blog/2007/06/07/check-value-of-radiobutton/</link>
		<comments>http://www.matthiaspospiech.de/blog/2007/06/07/check-value-of-radiobutton/#comments</comments>
		<pubDate>Thu, 07 Jun 2007 11:08:05 +0000</pubDate>
		<dc:creator>pospiech</dc:creator>
				<category><![CDATA[MFC]]></category>

		<guid isPermaLink="false">http://www.pospiech.eu/blog/2007/06/07/check-value-of-radiobutton/</guid>
		<description><![CDATA[Radiobuttons in MFC are a mess. It is nevertheless possible to get its values with the following instructions.
If one wants to have a group of radiobuttons the most important principle is to add them one ofter the other so that they have a ascending Resource number without any interruption. The first one in the group [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p>Radiobuttons in MFC are a mess. It is nevertheless possible to get its values with the following instructions.
If one wants to have a group of radiobuttons the most important principle is to add them one ofter the other so that they have a ascending Resource number without any interruption. The first one in the group of radiobuttons has have the value &#8220;group=true&#8221;.
For this one we add an integer Variable, lets say &#8220;m_WndRadio1&#8243;.</p>

<p>Next you can give all the Radiobuttons IDCs with meaning and add event handlers. </p>


<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> CDlg<span style="color: #008080;">::</span><span style="color: #007788;">OnBnClickedRadio1</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	UpdateData<span style="color: #008000;">&#40;</span>TRUE<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> 
	m_value<span style="color: #000080;">=</span>m_WndRadio1<span style="color: #008080;">;</span>		
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">void</span> CDlg<span style="color: #008080;">::</span><span style="color: #007788;">OnBnClickedRadio2</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	UpdateData<span style="color: #008000;">&#40;</span>TRUE<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> 
	m_value<span style="color: #000080;">=</span>m_WndRadio1<span style="color: #008080;">;</span>		
<span style="color: #008000;">&#125;</span></pre></div></div>




<p>additionally one has to add the following</p>


<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">DDX_Radio<span style="color: #008000;">&#40;</span>pDX, IDC_RADIO1, m_WndRadio1<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>




Thats it.

<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.matthiaspospiech.de/blog/2007/06/07/check-value-of-radiobutton/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Set Radio Button at runtime</title>
		<link>http://www.matthiaspospiech.de/blog/2007/06/07/set-radio-button-at-runtime/</link>
		<comments>http://www.matthiaspospiech.de/blog/2007/06/07/set-radio-button-at-runtime/#comments</comments>
		<pubDate>Thu, 07 Jun 2007 10:36:07 +0000</pubDate>
		<dc:creator>pospiech</dc:creator>
				<category><![CDATA[MFC]]></category>

		<guid isPermaLink="false">http://www.pospiech.eu/blog/2007/06/07/set-radio-button-at-runtime/</guid>
		<description><![CDATA[The following code sets the value of a radio button to checked:


	CButton *pButton = reinterpret_cast&#60;CButton*&#62;&#40;GetDlgItem&#40;IDC_RADIO1&#41;&#41;;
	pButton-&#62;SetCheck&#40;1&#41;; // Button is &#34;checked&#34;




No related posts.


No related posts.]]></description>
			<content:encoded><![CDATA[<p>The following code sets the value of a radio button to checked:</p>


<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">	CButton <span style="color: #000040;">*</span>pButton <span style="color: #000080;">=</span> <span style="color: #0000ff;">reinterpret_cast</span><span style="color: #000080;">&lt;</span>CButton<span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span>GetDlgItem<span style="color: #008000;">&#40;</span>IDC_RADIO1<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	pButton<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>SetCheck<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// Button is &quot;checked&quot;</span></pre></div></div>




<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.matthiaspospiech.de/blog/2007/06/07/set-radio-button-at-runtime/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>changing of the mouse cursor</title>
		<link>http://www.matthiaspospiech.de/blog/2007/05/21/changing-of-the-mouse-cursor/</link>
		<comments>http://www.matthiaspospiech.de/blog/2007/05/21/changing-of-the-mouse-cursor/#comments</comments>
		<pubDate>Mon, 21 May 2007 15:18:25 +0000</pubDate>
		<dc:creator>pospiech</dc:creator>
				<category><![CDATA[MFC]]></category>

		<guid isPermaLink="false">http://www.pospiech.eu/blog/2007/05/21/changing-of-the-mouse-cursor/</guid>
		<description><![CDATA[The following code sets the mouse cursor to the waiting state and back to its default before:


	HCURSOR hCursorWait;
	HCURSOR hCursorDefault;
	hCursorWait = LoadCursor&#40;NULL,IDC_WAIT&#41;;
	hCursorDefault = ::SetCursor&#40;hCursorWait&#41;;
&#160;
	::SetCursor&#40;hCursorDefault&#41;;





Related posts:Large and confusing wizard results
Set Radio Button at runtime



Related posts:<ol><li><a href='http://www.matthiaspospiech.de/blog/2007/03/07/mfc-large-and-confusing-wizard-results/' rel='bookmark' title='Permanent Link: Large and confusing wizard results'>Large and confusing wizard results</a></li>
<li><a href='http://www.matthiaspospiech.de/blog/2007/06/07/set-radio-button-at-runtime/' rel='bookmark' title='Permanent Link: Set Radio Button at runtime'>Set Radio Button at runtime</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>The following code sets the mouse cursor to the waiting state and back to its default before:</p>


<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">	HCURSOR hCursorWait<span style="color: #008080;">;</span>
	HCURSOR hCursorDefault<span style="color: #008080;">;</span>
	hCursorWait <span style="color: #000080;">=</span> LoadCursor<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">NULL</span>,IDC_WAIT<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	hCursorDefault <span style="color: #000080;">=</span> <span style="color: #008080;">::</span><span style="color: #007788;">SetCursor</span><span style="color: #008000;">&#40;</span>hCursorWait<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #008080;">::</span><span style="color: #007788;">SetCursor</span><span style="color: #008000;">&#40;</span>hCursorDefault<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>





<p>Related posts:<ol><li><a href='http://www.matthiaspospiech.de/blog/2007/03/07/mfc-large-and-confusing-wizard-results/' rel='bookmark' title='Permanent Link: Large and confusing wizard results'>Large and confusing wizard results</a></li>
<li><a href='http://www.matthiaspospiech.de/blog/2007/06/07/set-radio-button-at-runtime/' rel='bookmark' title='Permanent Link: Set Radio Button at runtime'>Set Radio Button at runtime</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.matthiaspospiech.de/blog/2007/05/21/changing-of-the-mouse-cursor/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Open, Save and Read Bitmaps</title>
		<link>http://www.matthiaspospiech.de/blog/2007/04/30/open-save-and-read-bitmaps/</link>
		<comments>http://www.matthiaspospiech.de/blog/2007/04/30/open-save-and-read-bitmaps/#comments</comments>
		<pubDate>Mon, 30 Apr 2007 14:40:37 +0000</pubDate>
		<dc:creator>pospiech</dc:creator>
				<category><![CDATA[MFC]]></category>

		<guid isPermaLink="false">http://www.pospiech.eu/blog/2007/04/30/open-save-and-read-bitmaps/</guid>
		<description><![CDATA[The task to read a bitmap into a data array, show this bitmap and save an array or dc to a file sound like a simple task but is rather difficult in reality if one uses MFC. Maybe I am totaly wrong when stating this, but that is my result of one week searching for [...]


Related posts:<ol><li><a href='http://www.matthiaspospiech.de/blog/2007/04/26/mfc-enhanced-cmemdc/' rel='bookmark' title='Permanent Link: Enhanced CMemDC'>Enhanced CMemDC</a></li>
<li><a href='http://www.matthiaspospiech.de/blog/2007/03/06/mfc-books-to-read/' rel='bookmark' title='Permanent Link: Books to read'>Books to read</a></li>
<li><a href='http://www.matthiaspospiech.de/blog/2007/03/07/mfc-large-and-confusing-wizard-results/' rel='bookmark' title='Permanent Link: Large and confusing wizard results'>Large and confusing wizard results</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>The task to read a bitmap into a data array, show this bitmap and save an array or dc to a file sound like a simple task but is rather difficult in reality if one uses MFC. Maybe I am totaly wrong when stating this, but that is my result of one week searching for solutions and implementing my own ones.</p>

<p>What I have achieved can be  as an example project.</p>

Note: There is a file embedded within this post, please visit this post to download the file. 

<span id="more-98"></span>

<p>The idea of these classes is to open a bitmap into a dc and to save the content of a dc into a bitmap.</p>

<h3 id="toc-cbitmapop">CBitmapOp</h3>


<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#pragma once</span>
&nbsp;
<span style="color: #339900;">#include &lt;vector&gt;</span>
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span> 
&nbsp;
<span style="color: #0000ff;">class</span> CBitmapOp
<span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
	CBitmapOp<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	~CBitmapOp<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
	BOOL SaveDcToBitmapFile<span style="color: #008000;">&#40;</span> LPCTSTR szFile, CPaintDC<span style="color: #000040;">*</span> pDC, CRect rect<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	BOOL WriteDIBToFile<span style="color: #008000;">&#40;</span> LPCTSTR szFile, HANDLE hDIB<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	HANDLE DDBToDIB<span style="color: #008000;">&#40;</span> CBitmap<span style="color: #000040;">&amp;</span> bitmap, DWORD dwCompression, CPalette<span style="color: #000040;">*</span> pPal <span style="color: #008000;">&#41;</span> <span style="color: #008080;">;</span>
	BOOL LoadBitmapFileToCBitmap<span style="color: #008000;">&#40;</span> LPCTSTR sBMPFile, CBitmap<span style="color: #000040;">&amp;</span> bitmap, CPalette <span style="color: #000040;">*</span>pPal <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	BOOL GetBitmapPixels<span style="color: #008000;">&#40;</span>CBitmap<span style="color: #000040;">&amp;</span> bitmap<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">void</span> SetBitmapDataSize<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> x, <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> y<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	vector<span style="color: #000080;">&lt;</span><span style="color: #000040;">/</span>vector<span style="color: #000080;">&gt;&lt;</span>vector <span style="color: #000080;">&lt;</span>COLORREF<span style="color: #000080;">&gt;</span> <span style="color: #000080;">&gt;</span> GetBitmapArray<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">private</span><span style="color: #008080;">:</span>
	<span style="color: #0000ff;">int</span> m_PixelNumberX<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span> m_PixelNumberY<span style="color: #008080;">;</span>
	vector<span style="color: #000080;">&lt;</span><span style="color: #000040;">/</span>vector<span style="color: #000080;">&gt;&lt;</span>vector <span style="color: #000080;">&lt;</span>COLORREF<span style="color: #000080;">&gt;</span> <span style="color: #000080;">&gt;</span> BitmapData<span style="color: #008080;">;</span> 
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
<span style="color: #000080;">&lt;</span><span style="color: #000040;">/</span>vector<span style="color: #000080;">&gt;</span></pre></div></div>





<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &quot;StdAfx.h&quot;</span>
<span style="color: #339900;">#include &quot;BitmapOp.h&quot;</span>
&nbsp;
&nbsp;
CBitmapOp<span style="color: #008080;">::</span><span style="color: #007788;">CBitmapOp</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	m_PixelNumberX<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
	m_PixelNumberY<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>	
<span style="color: #008000;">&#125;</span>
&nbsp;
CBitmapOp<span style="color: #008080;">::</span>~CBitmapOp<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #666666;">////////////////////////////////////////////////////////////////////</span>
<span style="color: #666666;">// www.codeguru.com</span>
<span style="color: #666666;">// Writing a window image to a BMP file </span>
<span style="color: #666666;">// Zafir Anjum</span>
<span style="color: #666666;">////////////////////////////////////////////////////////////////////</span>
<span style="color: #666666;">// To save the image of a window to a BMP file, you have to draw the image</span>
<span style="color: #666666;">// to a compatible memory device context, convert the bitmap associated with</span>
<span style="color: #666666;">// the memory DC to a device-independent bitmap and then write the DIB to a file.</span>
<span style="color: #666666;">//	In previous sections, we have already covered how to get a DIB from a </span>
<span style="color: #666666;">// device-dependent bitmap and how to save a DIB to a file. The WriteWindowToDIB()</span>
<span style="color: #666666;">// function uses the DDBToDIB() and the WriteDIB() functions to accomplish its task.</span>
<span style="color: #666666;">//	  However, this is just half of the job; the other half is to get the logical</span>
<span style="color: #666666;">// palette that can be used with the bitmap. The bitmap won't display properly if</span>
<span style="color: #666666;">// it needs a logical palette and one is not available. Depending on the display </span>
<span style="color: #666666;">// settings we don't always need a logical palette. We use GetDeviceCaps() to determine</span>
<span style="color: #666666;">// whether a display device supports palette operation. For instance, if the display is </span>
<span style="color: #666666;">// set for true color then it doesn't support a palette. We allocate enough memory for a</span>
<span style="color: #666666;">// LOGPALETTE structure and then use the GetSystemPaletteEntries() to fill the color table.</span>
<span style="color: #666666;">// Now we can create the logical palette.</span>
<span style="color: #666666;">//	  The function then uses the DDBToDIB() and the WriteDIB() functions to complete the job.</span>
<span style="color: #666666;">// You should note that if the window is partially or completely covered by other windows then</span>
<span style="color: #666666;">// the saved bitmap will show parts of these other windows too. </span>
<span style="color: #666666;">////////////////////////////////////////////////////////////////////</span>
<span style="color: #666666;">// changed by Matthias Pospiech (2007), so that it works directly with a DC.</span>
<span style="color: #666666;">////////////////////////////////////////////////////////////////////</span>
BOOL CBitmapOp<span style="color: #008080;">::</span><span style="color: #007788;">SaveDcToBitmapFile</span><span style="color: #008000;">&#40;</span> LPCTSTR szFile, CPaintDC<span style="color: #000040;">*</span> pDC, CRect rect<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	CBitmap 	bitmap<span style="color: #008080;">;</span>
	<span style="color: #666666;">//CWindowDC	dc(pWnd);</span>
	CDC 		memDC<span style="color: #008080;">;</span>
&nbsp;
	memDC.<span style="color: #007788;">CreateCompatibleDC</span><span style="color: #008000;">&#40;</span>pDC<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	bitmap.<span style="color: #007788;">CreateCompatibleBitmap</span><span style="color: #008000;">&#40;</span>pDC, rect.<span style="color: #007788;">Width</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>,rect.<span style="color: #007788;">Height</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	CBitmap<span style="color: #000040;">*</span> pOldBitmap <span style="color: #000080;">=</span> memDC.<span style="color: #007788;">SelectObject</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>bitmap<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	memDC.<span style="color: #007788;">BitBlt</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span>, <span style="color: #0000dd;">0</span>, rect.<span style="color: #007788;">Width</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>,rect.<span style="color: #007788;">Height</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, pDC, <span style="color: #0000dd;">0</span>, <span style="color: #0000dd;">0</span>, SRCCOPY<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// Create logical palette if device support a palette</span>
	CPalette pal<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> pDC<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>GetDeviceCaps<span style="color: #008000;">&#40;</span>RASTERCAPS<span style="color: #008000;">&#41;</span> <span style="color: #000040;">&amp;</span> RC_PALETTE <span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		UINT nSize <span style="color: #000080;">=</span> <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>LOGPALETTE<span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>PALETTEENTRY<span style="color: #008000;">&#41;</span> <span style="color: #000040;">*</span> <span style="color: #0000dd;">256</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		LOGPALETTE <span style="color: #000040;">*</span>pLP <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>LOGPALETTE <span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span> <span style="color: #0000dd;">new</span> BYTE<span style="color: #008000;">&#91;</span>nSize<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
		pLP<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>palVersion <span style="color: #000080;">=</span> <span style="color: #208080;">0x300</span><span style="color: #008080;">;</span>
&nbsp;
		pLP<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>palNumEntries <span style="color: #000080;">=</span> GetSystemPaletteEntries<span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>pDC, <span style="color: #0000dd;">0</span>, <span style="color: #0000dd;">255</span>, pLP<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>palPalEntry <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
		<span style="color: #666666;">// Create the palette</span>
		pal.<span style="color: #007788;">CreatePalette</span><span style="color: #008000;">&#40;</span> pLP <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
		<span style="color: #0000dd;">delete</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> pLP<span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	memDC.<span style="color: #007788;">SelectObject</span><span style="color: #008000;">&#40;</span>pOldBitmap<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// Convert the bitmap to a DIB</span>
	HANDLE hDIB <span style="color: #000080;">=</span> DDBToDIB<span style="color: #008000;">&#40;</span> bitmap, BI_RGB, <span style="color: #000040;">&amp;</span>pal <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> hDIB <span style="color: #000080;">==</span> <span style="color: #0000ff;">NULL</span> <span style="color: #008000;">&#41;</span>
		<span style="color: #0000ff;">return</span> FALSE<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// Write it to file</span>
	WriteDIBToFile<span style="color: #008000;">&#40;</span> szFile, hDIB <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// Free the memory allocated by DDBToDIB for the DIB</span>
	GlobalFree<span style="color: #008000;">&#40;</span> hDIB <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">return</span> TRUE<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #666666;">////////////////////////////////////////////////////////////////////</span>
<span style="color: #666666;">// www.codeguru.com</span>
<span style="color: #666666;">// Writing a bitmap to a BMP file </span>
<span style="color: #666666;">// Zafir Anjum</span>
<span style="color: #666666;">////////////////////////////////////////////////////////////////////</span>
&nbsp;
<span style="color: #666666;">// WriteDIB		- Writes a DIB to file</span>
<span style="color: #666666;">// Returns		- TRUE on success</span>
<span style="color: #666666;">// szFile		- Name of file to write to</span>
<span style="color: #666666;">// hDIB			- Handle of the DIB</span>
BOOL CBitmapOp<span style="color: #008080;">::</span><span style="color: #007788;">WriteDIBToFile</span><span style="color: #008000;">&#40;</span> LPCTSTR szFile, HANDLE hDIB<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	BITMAPFILEHEADER	hdr<span style="color: #008080;">;</span>
	LPBITMAPINFOHEADER	lpbi<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>hDIB<span style="color: #008000;">&#41;</span>
		<span style="color: #0000ff;">return</span> FALSE<span style="color: #008080;">;</span>
&nbsp;
	CFile file<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> <span style="color: #000040;">!</span>file.<span style="color: #007788;">Open</span><span style="color: #008000;">&#40;</span> szFile, CFile<span style="color: #008080;">::</span><span style="color: #007788;">modeWrite</span><span style="color: #000040;">|</span>CFile<span style="color: #008080;">::</span><span style="color: #007788;">modeCreate</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span>
		<span style="color: #0000ff;">return</span> FALSE<span style="color: #008080;">;</span>
&nbsp;
	lpbi <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>LPBITMAPINFOHEADER<span style="color: #008000;">&#41;</span>hDIB<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">int</span> nColors <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span> <span style="color: #000080;">&lt;</span> <span style="color: #000080;">&lt;</span> lpbi<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>biBitCount<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// Fill in the fields of the file header </span>
	hdr.<span style="color: #007788;">bfType</span>		<span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>WORD<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#40;</span><span style="color: #FF0000;">'M'</span> <span style="color: #000080;">&lt;</span> <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">8</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">|</span> <span style="color: #FF0000;">'B'</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>	<span style="color: #666666;">// is always &quot;BM&quot;</span>
	hdr.<span style="color: #007788;">bfSize</span>		<span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>DWORD<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span>GlobalSize <span style="color: #008000;">&#40;</span>hDIB<span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span> hdr <span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	hdr.<span style="color: #007788;">bfReserved1</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
	hdr.<span style="color: #007788;">bfReserved2</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
	hdr.<span style="color: #007788;">bfOffBits</span>	<span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>DWORD<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span> hdr <span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> lpbi<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>biSize <span style="color: #000040;">+</span> nColors <span style="color: #000040;">*</span> <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>RGBQUAD<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// Write the file header </span>
	file.<span style="color: #007788;">Write</span><span style="color: #008000;">&#40;</span> <span style="color: #000040;">&amp;</span>hdr, <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>hdr<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// Write the DIB header and the bits </span>
	file.<span style="color: #007788;">Write</span><span style="color: #008000;">&#40;</span> lpbi, <span style="color: #008000;">&#40;</span>UINT<span style="color: #008000;">&#41;</span>GlobalSize<span style="color: #008000;">&#40;</span>hDIB<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">return</span> TRUE<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #666666;">////////////////////////////////////////////////////////////////////</span>
<span style="color: #666666;">// www.codeguru.com</span>
<span style="color: #666666;">// Writing a window image to a BMP file </span>
<span style="color: #666666;">// Zafir Anjum</span>
<span style="color: #666666;">////////////////////////////////////////////////////////////////////</span>
<span style="color: #666666;">//</span>
<span style="color: #666666;">// A device-dependent bitmap (DDB) is structured in such a way that it matches </span>
<span style="color: #666666;">// very closely the form in which the bitmap is finally displayed by the device driver.</span>
<span style="color: #666666;">// For this reason the DDB is unlikely to be compatible with other display devices.</span>
<span style="color: #666666;">// A device-independent bitmap (DIB) on the other hand has a logical layout and can be</span>
<span style="color: #666666;">// decifered by any device driver. The disadvantage with DIB though is that it is much </span>
<span style="color: #666666;">// slower to render onto the device as compared to a DDB.</span>
<span style="color: #666666;">//</span>
<span style="color: #666666;">// One situation where we need to convert a DDB to a DIB is when we want to save the bitmap</span>
<span style="color: #666666;">// to a file. The BMP files are infact just composed of a short header followed by</span>
<span style="color: #666666;">// information in a DIB.</span>
<span style="color: #666666;">//</span>
<span style="color: #666666;">// The steps involved in creating the DIB are:</span>
<span style="color: #666666;">//   1. Initialize a BITMAPINFOHEADER data structure. We use information in the bitmap </span>
<span style="color: #666666;">//	  to determine the widht, height and the bit count. The compression field is set </span>
<span style="color: #666666;">//	  based on the argument passed into the function. It's best to use the BI_RGB </span>
<span style="color: #666666;">//	  compression for greater portability and faster rendering.</span>
<span style="color: #666666;">//   2. Select and realize the logical palette associated with the bitmap. This </span>
<span style="color: #666666;">//      is passed in as the last argument to the function. If a palette is not provided </span>
<span style="color: #666666;">//	  use the default palette.</span>
<span style="color: #666666;">//   3. Determine the number of bytes required for the bitmap bits. First allocate </span>
<span style="color: #666666;">//      enough memory for BITMAPINFOHEADER and the color table and then call GetDIBits() </span>
<span style="color: #666666;">//	  to size. Supplying a NULL for the lpBits parameter instructs GetDIBits() to </span>
<span style="color: #666666;">//	  biSizeImage field in the bitmapinfoheader. Sometimes, this does not work </span>
<span style="color: #666666;">//	  (depending on the device driver) in which case we make our own estimate. </span>
<span style="color: #666666;">//	  We can compute this since we already know the width, height, the number of bits </span>
<span style="color: #666666;">//	  per pixel and the fact that each row of pixels will occupy a multiple </span>
<span style="color: #666666;">//	  of 4 bytes (32 bits).</span>
<span style="color: #666666;">//   4. Given the final size of the bitmap bits, reallocate the memory block to hold</span>
<span style="color: #666666;">//      the bitmapinfoheader, the color table and the bitmap bits.</span>
<span style="color: #666666;">//   5. Finally call the GetDIBits() again to get the bitmap bits.</span>
<span style="color: #666666;">////////////////////////////////////////////////////////////////////</span>
&nbsp;
<span style="color: #666666;">// DDBToDIB		- Creates a DIB from a DDB</span>
<span style="color: #666666;">// bitmap		- Device dependent bitmap</span>
<span style="color: #666666;">// dwCompression	- Type of compression - see BITMAPINFOHEADER</span>
<span style="color: #666666;">// pPal			- Logical palette</span>
HANDLE CBitmapOp<span style="color: #008080;">::</span><span style="color: #007788;">DDBToDIB</span><span style="color: #008000;">&#40;</span> CBitmap<span style="color: #000040;">&amp;</span> bitmap, DWORD dwCompression, CPalette<span style="color: #000040;">*</span> pPal <span style="color: #008000;">&#41;</span> 
<span style="color: #008000;">&#123;</span>
	BITMAP			bm<span style="color: #008080;">;</span>
	BITMAPINFOHEADER	bi<span style="color: #008080;">;</span>
	LPBITMAPINFOHEADER 	lpbi<span style="color: #008080;">;</span>
	DWORD			dwLen<span style="color: #008080;">;</span>
	HANDLE			hDIB<span style="color: #008080;">;</span>
	HANDLE			handle<span style="color: #008080;">;</span>
	HDC 			hDC<span style="color: #008080;">;</span>
	HPALETTE		hPal<span style="color: #008080;">;</span>
&nbsp;
&nbsp;
	ASSERT<span style="color: #008000;">&#40;</span> bitmap.<span style="color: #007788;">GetSafeHandle</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// The function has no arg for bitfields</span>
	<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> dwCompression <span style="color: #000080;">==</span> BI_BITFIELDS <span style="color: #008000;">&#41;</span>
		<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">NULL</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// If a palette has not been supplied use defaul palette</span>
	hPal <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>HPALETTE<span style="color: #008000;">&#41;</span> pPal<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>GetSafeHandle<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>hPal<span style="color: #000080;">==</span><span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span>
		hPal <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>HPALETTE<span style="color: #008000;">&#41;</span> GetStockObject<span style="color: #008000;">&#40;</span>DEFAULT_PALETTE<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// Get bitmap information</span>
	bitmap.<span style="color: #007788;">GetObject</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>bm<span style="color: #008000;">&#41;</span>,<span style="color: #008000;">&#40;</span>LPSTR<span style="color: #008000;">&#41;</span><span style="color: #000040;">&amp;</span>bm<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// Initialize the bitmapinfoheader</span>
	bi.<span style="color: #007788;">biSize</span>		<span style="color: #000080;">=</span> <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>BITMAPINFOHEADER<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	bi.<span style="color: #007788;">biWidth</span>		<span style="color: #000080;">=</span> bm.<span style="color: #007788;">bmWidth</span><span style="color: #008080;">;</span>
	bi.<span style="color: #007788;">biHeight</span> 		<span style="color: #000080;">=</span> bm.<span style="color: #007788;">bmHeight</span><span style="color: #008080;">;</span>
	bi.<span style="color: #007788;">biPlanes</span> 		<span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
	bi.<span style="color: #007788;">biBitCount</span>		<span style="color: #000080;">=</span> bm.<span style="color: #007788;">bmPlanes</span> <span style="color: #000040;">*</span> bm.<span style="color: #007788;">bmBitsPixel</span><span style="color: #008080;">;</span>
	bi.<span style="color: #007788;">biCompression</span>	<span style="color: #000080;">=</span> dwCompression<span style="color: #008080;">;</span>
	bi.<span style="color: #007788;">biSizeImage</span>		<span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
	bi.<span style="color: #007788;">biXPelsPerMeter</span>	<span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
	bi.<span style="color: #007788;">biYPelsPerMeter</span>	<span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
	bi.<span style="color: #007788;">biClrUsed</span>		<span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
	bi.<span style="color: #007788;">biClrImportant</span>	<span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// Compute the size of the  infoheader and the color table</span>
	<span style="color: #0000ff;">int</span> nColors <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span> <span style="color: #000080;">&lt;</span> <span style="color: #000080;">&lt;</span> bi.<span style="color: #007788;">biBitCount</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> nColors <span style="color: #000080;">&gt;</span> <span style="color: #0000dd;">256</span> <span style="color: #008000;">&#41;</span> 
		nColors <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
	dwLen  <span style="color: #000080;">=</span> bi.<span style="color: #007788;">biSize</span> <span style="color: #000040;">+</span> nColors <span style="color: #000040;">*</span> <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>RGBQUAD<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// We need a device context to get the DIB from</span>
	hDC <span style="color: #000080;">=</span> GetDC<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	hPal <span style="color: #000080;">=</span> SelectPalette<span style="color: #008000;">&#40;</span>hDC,hPal,FALSE<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	RealizePalette<span style="color: #008000;">&#40;</span>hDC<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// Allocate enough memory to hold bitmapinfoheader and color table</span>
	hDIB <span style="color: #000080;">=</span> GlobalAlloc<span style="color: #008000;">&#40;</span>GMEM_FIXED,dwLen<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>hDIB<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
		SelectPalette<span style="color: #008000;">&#40;</span>hDC,hPal,FALSE<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		ReleaseDC<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">NULL</span>,hDC<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">NULL</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	lpbi <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>LPBITMAPINFOHEADER<span style="color: #008000;">&#41;</span>hDIB<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #000040;">*</span>lpbi <span style="color: #000080;">=</span> bi<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// Call GetDIBits with a NULL lpBits param, so the device driver </span>
	<span style="color: #666666;">// will calculate the biSizeImage field </span>
	GetDIBits<span style="color: #008000;">&#40;</span>hDC, <span style="color: #008000;">&#40;</span>HBITMAP<span style="color: #008000;">&#41;</span>bitmap.<span style="color: #007788;">GetSafeHandle</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, <span style="color: #0000dd;">0L</span>, <span style="color: #008000;">&#40;</span>DWORD<span style="color: #008000;">&#41;</span>bi.<span style="color: #007788;">biHeight</span>,
			<span style="color: #008000;">&#40;</span>LPBYTE<span style="color: #008000;">&#41;</span><span style="color: #0000ff;">NULL</span>, <span style="color: #008000;">&#40;</span>LPBITMAPINFO<span style="color: #008000;">&#41;</span>lpbi, <span style="color: #008000;">&#40;</span>DWORD<span style="color: #008000;">&#41;</span>DIB_RGB_COLORS<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	bi <span style="color: #000080;">=</span> <span style="color: #000040;">*</span>lpbi<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// If the driver did not fill in the biSizeImage field, then compute it</span>
	<span style="color: #666666;">// Each scan line of the image is aligned on a DWORD (32bit) boundary</span>
	<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>bi.<span style="color: #007788;">biSizeImage</span> <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
		bi.<span style="color: #007788;">biSizeImage</span> <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>bi.<span style="color: #007788;">biWidth</span> <span style="color: #000040;">*</span> bi.<span style="color: #007788;">biBitCount</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> <span style="color: #0000dd;">31</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">&amp;</span> ~<span style="color: #0000dd;">31</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">/</span> <span style="color: #0000dd;">8</span><span style="color: #008000;">&#41;</span> 
						<span style="color: #000040;">*</span> bi.<span style="color: #007788;">biHeight</span><span style="color: #008080;">;</span>
&nbsp;
		<span style="color: #666666;">// If a compression scheme is used the result may infact be larger</span>
		<span style="color: #666666;">// Increase the size to account for this.</span>
		<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>dwCompression <span style="color: #000040;">!</span><span style="color: #000080;">=</span> BI_RGB<span style="color: #008000;">&#41;</span>
			bi.<span style="color: #007788;">biSizeImage</span> <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>bi.<span style="color: #007788;">biSizeImage</span> <span style="color: #000040;">*</span> <span style="color: #0000dd;">3</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">/</span> <span style="color: #0000dd;">2</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #666666;">// Realloc the buffer so that it can hold all the bits</span>
	dwLen <span style="color: #000040;">+</span><span style="color: #000080;">=</span> bi.<span style="color: #007788;">biSizeImage</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>handle <span style="color: #000080;">=</span> GlobalReAlloc<span style="color: #008000;">&#40;</span>hDIB, dwLen, GMEM_MOVEABLE<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
		hDIB <span style="color: #000080;">=</span> handle<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">else</span><span style="color: #008000;">&#123;</span>
		GlobalFree<span style="color: #008000;">&#40;</span>hDIB<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
		<span style="color: #666666;">// Reselect the original palette</span>
		SelectPalette<span style="color: #008000;">&#40;</span>hDC,hPal,FALSE<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		ReleaseDC<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">NULL</span>,hDC<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">NULL</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #666666;">// Get the bitmap bits</span>
	lpbi <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>LPBITMAPINFOHEADER<span style="color: #008000;">&#41;</span>hDIB<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// FINALLY get the DIB</span>
	BOOL bGotBits <span style="color: #000080;">=</span> GetDIBits<span style="color: #008000;">&#40;</span> hDC, <span style="color: #008000;">&#40;</span>HBITMAP<span style="color: #008000;">&#41;</span>bitmap.<span style="color: #007788;">GetSafeHandle</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>,
				<span style="color: #0000dd;">0L</span>,				<span style="color: #666666;">// Start scan line</span>
				<span style="color: #008000;">&#40;</span>DWORD<span style="color: #008000;">&#41;</span>bi.<span style="color: #007788;">biHeight</span>,		<span style="color: #666666;">// # of scan lines</span>
				<span style="color: #008000;">&#40;</span>LPBYTE<span style="color: #008000;">&#41;</span>lpbi 			<span style="color: #666666;">// address for bitmap bits</span>
				<span style="color: #000040;">+</span> <span style="color: #008000;">&#40;</span>bi.<span style="color: #007788;">biSize</span> <span style="color: #000040;">+</span> nColors <span style="color: #000040;">*</span> <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>RGBQUAD<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>,
				<span style="color: #008000;">&#40;</span>LPBITMAPINFO<span style="color: #008000;">&#41;</span>lpbi,		<span style="color: #666666;">// address of bitmapinfo</span>
				<span style="color: #008000;">&#40;</span>DWORD<span style="color: #008000;">&#41;</span>DIB_RGB_COLORS<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>		<span style="color: #666666;">// Use RGB for color table</span>
&nbsp;
	<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> <span style="color: #000040;">!</span>bGotBits <span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		GlobalFree<span style="color: #008000;">&#40;</span>hDIB<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
		SelectPalette<span style="color: #008000;">&#40;</span>hDC,hPal,FALSE<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		ReleaseDC<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">NULL</span>,hDC<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">NULL</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	SelectPalette<span style="color: #008000;">&#40;</span>hDC,hPal,FALSE<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	ReleaseDC<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">NULL</span>,hDC<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">return</span> hDIB<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #666666;">////////////////////////////////////////////////////////////////////</span>
<span style="color: #666666;">// www.codeguru.com</span>
<span style="color: #666666;">// Creating a bitmap object from a BMP file</span>
<span style="color: #666666;">// Zafir Anjum</span>
<span style="color: #666666;">////////////////////////////////////////////////////////////////////</span>
<span style="color: #666666;">//  Create the bitmap object</span>
<span style="color: #666666;">// The LoadBMPImage() function given below will read in a BMP file,</span>
<span style="color: #666666;">// create a device dependent bitmap of it and attach it to the CBitmap </span>
<span style="color: #666666;">// object passed in as an argument. It can also create a logical palette </span>
<span style="color: #666666;">// if a pointer to a CPalette object is supplied.</span>
<span style="color: #666666;">//	This function basically reads in the BMP file, creates a logical palette</span>
<span style="color: #666666;">// and calls CreateDIBitmap() to create the GDI bitmap object. Note that when</span>
<span style="color: #666666;">// you supply a pointer to a CPalette object, the function creates a logical </span>
<span style="color: #666666;">// palette and uses the palette when creating the bitmap object. </span>
<span style="color: #666666;">////////////////////////////////////////////////////////////////////</span>
&nbsp;
<span style="color: #666666;">// LoadBMPImage	- Loads a BMP file and creates a bitmap GDI object</span>
<span style="color: #666666;">//		  also creates logical palette for it.</span>
<span style="color: #666666;">// Returns	- TRUE for success</span>
<span style="color: #666666;">// sBMPFile	- Full path of the BMP file</span>
<span style="color: #666666;">// bitmap	- The bitmap object to initialize</span>
<span style="color: #666666;">// pPal		- Will hold the logical palette. Can be NULL</span>
BOOL CBitmapOp<span style="color: #008080;">::</span><span style="color: #007788;">LoadBitmapFileToCBitmap</span><span style="color: #008000;">&#40;</span> LPCTSTR sBMPFile, CBitmap<span style="color: #000040;">&amp;</span> bitmap, CPalette <span style="color: #000040;">*</span>pPal <span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	CFile file<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> <span style="color: #000040;">!</span>file.<span style="color: #007788;">Open</span><span style="color: #008000;">&#40;</span> sBMPFile, CFile<span style="color: #008080;">::</span><span style="color: #007788;">modeRead</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span>
		<span style="color: #0000ff;">return</span> FALSE<span style="color: #008080;">;</span>
&nbsp;
	BITMAPFILEHEADER bmfHeader<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// Read file header</span>
	<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>file.<span style="color: #007788;">Read</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>LPSTR<span style="color: #008000;">&#41;</span><span style="color: #000040;">&amp;</span>bmfHeader, <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>bmfHeader<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>bmfHeader<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
		<span style="color: #0000ff;">return</span> FALSE<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// File type should be 'BM'</span>
	<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>bmfHeader.<span style="color: #007788;">bfType</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>WORD<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#40;</span><span style="color: #FF0000;">'M'</span> <span style="color: #000080;">&lt;</span> <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">8</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">|</span> <span style="color: #FF0000;">'B'</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
		<span style="color: #0000ff;">return</span> FALSE<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// Get length of the remainder of the file and allocate memory</span>
	DWORD nPackedDIBLen <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>DWORD<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span>file.<span style="color: #007788;">GetLength</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">-</span> <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>BITMAPFILEHEADER<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	HGLOBAL hDIB <span style="color: #000080;">=</span> <span style="color: #008080;">::</span><span style="color: #007788;">GlobalAlloc</span><span style="color: #008000;">&#40;</span>GMEM_FIXED, nPackedDIBLen<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>hDIB <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span>
		<span style="color: #0000ff;">return</span> FALSE<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// Read the remainder of the bitmap file.</span>
	<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>file.<span style="color: #007788;">Read</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>LPSTR<span style="color: #008000;">&#41;</span>hDIB, nPackedDIBLen<span style="color: #008000;">&#41;</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> nPackedDIBLen <span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #008080;">::</span><span style="color: #007788;">GlobalFree</span><span style="color: #008000;">&#40;</span>hDIB<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">return</span> FALSE<span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
&nbsp;
	BITMAPINFOHEADER <span style="color: #000040;">&amp;</span>bmiHeader <span style="color: #000080;">=</span> <span style="color: #000040;">*</span><span style="color: #008000;">&#40;</span>LPBITMAPINFOHEADER<span style="color: #008000;">&#41;</span>hDIB <span style="color: #008080;">;</span>
	BITMAPINFO <span style="color: #000040;">&amp;</span>bmInfo <span style="color: #000080;">=</span> <span style="color: #000040;">*</span><span style="color: #008000;">&#40;</span>LPBITMAPINFO<span style="color: #008000;">&#41;</span>hDIB <span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// If bmiHeader.biClrUsed is zero we have to infer the number</span>
	<span style="color: #666666;">// of colors from the number of bits used to specify it.</span>
	<span style="color: #0000ff;">int</span> nColors <span style="color: #000080;">=</span> bmiHeader.<span style="color: #007788;">biClrUsed</span> <span style="color: #008080;">?</span> bmiHeader.<span style="color: #007788;">biClrUsed</span> <span style="color: #008080;">:</span>
						<span style="color: #0000dd;">1</span> <span style="color: #000080;">&lt;&lt;</span> bmiHeader.<span style="color: #007788;">biBitCount</span><span style="color: #008080;">;</span>
&nbsp;
	LPVOID lpDIBBits<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> bmInfo.<span style="color: #007788;">bmiHeader</span>.<span style="color: #007788;">biBitCount</span> <span style="color: #000080;">&gt;</span> <span style="color: #0000dd;">8</span> <span style="color: #008000;">&#41;</span>
		lpDIBBits <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>LPVOID<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>LPDWORD<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span>bmInfo.<span style="color: #007788;">bmiColors</span> <span style="color: #000040;">+</span> bmInfo.<span style="color: #007788;">bmiHeader</span>.<span style="color: #007788;">biClrUsed</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span>
			<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>bmInfo.<span style="color: #007788;">bmiHeader</span>.<span style="color: #007788;">biCompression</span> <span style="color: #000080;">==</span> BI_BITFIELDS<span style="color: #008000;">&#41;</span> <span style="color: #008080;">?</span> <span style="color: #0000dd;">3</span> <span style="color: #008080;">:</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">else</span>
		lpDIBBits <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>LPVOID<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span>bmInfo.<span style="color: #007788;">bmiColors</span> <span style="color: #000040;">+</span> nColors<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// Create the logical palette</span>
	<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> pPal <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #0000ff;">NULL</span> <span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #666666;">// Create the palette</span>
		<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> nColors <span style="color: #000080;">&lt;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">256</span> <span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			UINT nSize <span style="color: #000080;">=</span> <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>LOGPALETTE<span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>PALETTEENTRY<span style="color: #008000;">&#41;</span> <span style="color: #000040;">*</span> nColors<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			LOGPALETTE <span style="color: #000040;">*</span>pLP <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>LOGPALETTE <span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span> <span style="color: #0000dd;">new</span> BYTE<span style="color: #008000;">&#91;</span>nSize<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
&nbsp;
			pLP<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>palVersion <span style="color: #000080;">=</span> <span style="color: #208080;">0x300</span><span style="color: #008080;">;</span>
			pLP<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>palNumEntries <span style="color: #000080;">=</span> nColors<span style="color: #008080;">;</span>
&nbsp;
			<span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">int</span> i<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> nColors<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
			<span style="color: #008000;">&#123;</span>
				pLP<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>palPalEntry<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span>.<span style="color: #007788;">peRed</span> <span style="color: #000080;">=</span> bmInfo.<span style="color: #007788;">bmiColors</span><span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span>.<span style="color: #007788;">rgbRed</span><span style="color: #008080;">;</span>
				pLP<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>palPalEntry<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span>.<span style="color: #007788;">peGreen</span> <span style="color: #000080;">=</span> bmInfo.<span style="color: #007788;">bmiColors</span><span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span>.<span style="color: #007788;">rgbGreen</span><span style="color: #008080;">;</span>
				pLP<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>palPalEntry<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span>.<span style="color: #007788;">peBlue</span> <span style="color: #000080;">=</span> bmInfo.<span style="color: #007788;">bmiColors</span><span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span>.<span style="color: #007788;">rgbBlue</span><span style="color: #008080;">;</span>
				pLP<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>palPalEntry<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span>.<span style="color: #007788;">peFlags</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
			<span style="color: #008000;">&#125;</span>
&nbsp;
			pPal<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>CreatePalette<span style="color: #008000;">&#40;</span> pLP <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
			<span style="color: #0000dd;">delete</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> pLP<span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	CClientDC dc<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	CPalette<span style="color: #000040;">*</span> pOldPalette <span style="color: #000080;">=</span> <span style="color: #0000ff;">NULL</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> pPal <span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		pOldPalette <span style="color: #000080;">=</span> dc.<span style="color: #007788;">SelectPalette</span><span style="color: #008000;">&#40;</span> pPal, FALSE <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		dc.<span style="color: #007788;">RealizePalette</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	HBITMAP hBmp <span style="color: #000080;">=</span> CreateDIBitmap<span style="color: #008000;">&#40;</span> dc.<span style="color: #007788;">m_hDC</span>,		<span style="color: #666666;">// handle to device context </span>
				<span style="color: #000040;">&amp;</span>bmiHeader,	<span style="color: #666666;">// pointer to bitmap size and format data </span>
				CBM_INIT,	<span style="color: #666666;">// initialization flag </span>
				lpDIBBits,	<span style="color: #666666;">// pointer to initialization data </span>
				<span style="color: #000040;">&amp;</span>bmInfo,	<span style="color: #666666;">// pointer to bitmap color-format data </span>
				DIB_RGB_COLORS<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>		<span style="color: #666666;">// color-data usage </span>
	bitmap.<span style="color: #007788;">Attach</span><span style="color: #008000;">&#40;</span> hBmp <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> pOldPalette <span style="color: #008000;">&#41;</span>
		dc.<span style="color: #007788;">SelectPalette</span><span style="color: #008000;">&#40;</span> pOldPalette, FALSE <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #008080;">::</span><span style="color: #007788;">GlobalFree</span><span style="color: #008000;">&#40;</span>hDIB<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">return</span> TRUE<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #666666;">////////////////////////////////////////////////////////////////////</span>
<span style="color: #666666;">// www.codeguru.com</span>
<span style="color: #666666;">// Original: Print transparent bitmaps via regions</span>
<span style="color: #666666;">// Wes Rogers</span>
<span style="color: #666666;">////////////////////////////////////////////////////////////////////</span>
<span style="color: #666666;">//Load pixels into BitmapData array </span>
<span style="color: #666666;">//</span>
<span style="color: #666666;">//Returns true if OK, else false</span>
<span style="color: #666666;">//</span>
<span style="color: #666666;">//This code is based on the BitmapToRegion(</span>
<span style="color: #666666;">//function contributed by Mr. Lachand-Robert</span>
<span style="color: #666666;">//</span>
<span style="color: #666666;">//It is a modification of the original function</span>
<span style="color: #666666;">//which does the following</span>
<span style="color: #666666;">//</span>
<span style="color: #666666;">//1) Given a DC which is being drawn into</span>
<span style="color: #666666;">//   (such as a printer), the function</span>
<span style="color: #666666;">//   makes sure that the regions draws</span>
<span style="color: #666666;">//   will be centered on the X,Y offsets</span>
<span style="color: #666666;">//</span>
<span style="color: #666666;">//2) The function then loops thru the bits</span>
<span style="color: #666666;">//   in the bitmap. </span>
<span style="color: #666666;">//</span>
<span style="color: #666666;">//The function assumes a mapping mode other</span>
<span style="color: #666666;">//than MM_TEXT for the passed-in Device</span>
<span style="color: #666666;">//Context. I used it successfully with</span>
<span style="color: #666666;">//MM_ANISOTROPIC, for example, and was able</span>
<span style="color: #666666;">//to &quot;zoom&quot; the bitmap correctly</span>
<span style="color: #666666;">///////////////////////////////////////////</span>
BOOL CBitmapOp<span style="color: #008080;">::</span><span style="color: #007788;">GetBitmapPixels</span><span style="color: #008000;">&#40;</span>CBitmap<span style="color: #000040;">&amp;</span> bitmap<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	HBITMAP hBmp<span style="color: #008080;">;</span>
	hBmp <span style="color: #000080;">=</span> HBITMAP<span style="color: #008000;">&#40;</span>bitmap<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">//Sanity..</span>
	<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>hBmp <span style="color: #000080;">==</span> <span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">return</span> FALSE<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// Create a memory DC inside which we will scan the bitmap content</span>
	HDC hMemDC <span style="color: #000080;">=</span> CreateCompatibleDC<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	ASSERT<span style="color: #008000;">&#40;</span>hMemDC<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>hMemDC<span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #666666;">// Get bitmap siz</span>
		BITMAP bm<span style="color: #008080;">;</span>
		GetObject<span style="color: #008000;">&#40;</span>hBmp, <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>bm<span style="color: #008000;">&#41;</span>, <span style="color: #000040;">&amp;</span>bm<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
		<span style="color: #666666;">// Create a 32 bits depth bitmap and select it into the memory DC</span>
		BITMAPINFOHEADER RGB32BITSBITMAPINFO <span style="color: #000080;">=</span>
		<span style="color: #008000;">&#123;</span>
			<span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>BITMAPINFOHEADER<span style="color: #008000;">&#41;</span>,<span style="color: #666666;">// biSize</span>
			bm.<span style="color: #007788;">bmWidth</span>,				 <span style="color: #666666;">// biWidth;</span>
           <span style="color: #000040;">-</span>bm.<span style="color: #007788;">bmHeight</span>,			 <span style="color: #666666;">// biHeight</span>
			<span style="color: #0000dd;">1</span>,						 <span style="color: #666666;">// biPlanes;</span>
			<span style="color: #0000dd;">32</span>,						 <span style="color: #666666;">// biBitCount</span>
			BI_RGB,					 <span style="color: #666666;">// biCompression;</span>
			<span style="color: #0000dd;">0</span>,						 <span style="color: #666666;">// biSizeImage;</span>
			<span style="color: #0000dd;">0</span>,						 <span style="color: #666666;">// biXPelsPerMeter;</span>
			<span style="color: #0000dd;">0</span>,						 <span style="color: #666666;">// biYPelsPerMeter;</span>
			<span style="color: #0000dd;">0</span>,						 <span style="color: #666666;">// biClrUsed;</span>
			<span style="color: #0000dd;">0</span>						 <span style="color: #666666;">// biClrImportant;</span>
		<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
		VOID <span style="color: #000040;">*</span> pbits32<span style="color: #008080;">;</span>
&nbsp;
		HBITMAP hbm32 <span style="color: #000080;">=</span> CreateDIBSection<span style="color: #008000;">&#40;</span>
		                hMemDC,
		                <span style="color: #008000;">&#40;</span>BITMAPINFO <span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">&amp;</span>RGB32BITSBITMAPINFO,
							 DIB_RGB_COLORS,
							 <span style="color: #000040;">&amp;</span>pbits32,
							 <span style="color: #0000ff;">NULL</span>,
							 <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
		<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>hbm32<span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			HBITMAP holdBmp <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>HBITMAP<span style="color: #008000;">&#41;</span>SelectObject<span style="color: #008000;">&#40;</span>hMemDC, hbm32<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
			<span style="color: #666666;">// Create a DC just to copy the bitmap into the memory D</span>
			HDC hDC <span style="color: #000080;">=</span> CreateCompatibleDC<span style="color: #008000;">&#40;</span>hMemDC<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
			<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>hDC<span style="color: #008000;">&#41;</span>
			<span style="color: #008000;">&#123;</span>
				<span style="color: #666666;">// Get how many bytes per row w</span>
				<span style="color: #666666;">//have for the bitmap bit</span>
				<span style="color: #666666;">//(rounded up to 32 bits</span>
				BITMAP bm32<span style="color: #008080;">;</span>
				VERIFY<span style="color: #008000;">&#40;</span>GetObject<span style="color: #008000;">&#40;</span>hbm32, <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>bm32<span style="color: #008000;">&#41;</span>, <span style="color: #000040;">&amp;</span>bm32<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
				<span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span>bm32.<span style="color: #007788;">bmWidthBytes</span> <span style="color: #000040;">%</span> <span style="color: #0000dd;">4</span><span style="color: #008000;">&#41;</span>
					bm32.<span style="color: #007788;">bmWidthBytes</span><span style="color: #000040;">++</span><span style="color: #008080;">;</span>
&nbsp;
				<span style="color: #666666;">// Copy the bitmap into the memory DC</span>
				HBITMAP holdBmp <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>HBITMAP<span style="color: #008000;">&#41;</span>SelectObject<span style="color: #008000;">&#40;</span>hDC, hBmp<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
				VERIFY<span style="color: #008000;">&#40;</span>BitBlt<span style="color: #008000;">&#40;</span>hMemDC,
								  <span style="color: #0000dd;">0</span>,
								  <span style="color: #0000dd;">0</span>,
								  bm.<span style="color: #007788;">bmWidth</span>,
								  bm.<span style="color: #007788;">bmHeight</span>,
								  hDC,
								  <span style="color: #0000dd;">0</span>,
								  <span style="color: #0000dd;">0</span>,
								  SRCCOPY<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
				<span style="color: #666666;">//Scan each bitmap row from bottom to to</span>
				<span style="color: #666666;">//(the bitmap is inverted vertically</span>
				BYTE <span style="color: #000040;">*</span>p32 <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>BYTE <span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span>bm32.<span style="color: #007788;">bmBits</span> <span style="color: #000040;">+</span>
				            <span style="color: #008000;">&#40;</span>bm32.<span style="color: #007788;">bmHeight</span> <span style="color: #000040;">-</span> <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">*</span> bm32.<span style="color: #007788;">bmWidthBytes</span><span style="color: #008080;">;</span>
&nbsp;
				<span style="color: #666666;">//Scan each bitmap pixel from left to right				</span>
				SetBitmapDataSize<span style="color: #008000;">&#40;</span>bm.<span style="color: #007788;">bmWidth</span>, bm.<span style="color: #007788;">bmHeight</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
				<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> y <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> y <span style="color: #000080;">&lt;</span> bm.<span style="color: #007788;">bmHeight</span><span style="color: #008080;">;</span> y<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
				<span style="color: #008000;">&#123;</span>
					<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> x <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> x <span style="color: #000080;">&lt;</span> bm.<span style="color: #007788;">bmWidth</span><span style="color: #008080;">;</span> x<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
					<span style="color: #008000;">&#123;</span>
						<span style="color: #0000ff;">int</span>  x0 <span style="color: #000080;">=</span> x<span style="color: #008080;">;</span> <span style="color: #666666;">//Ptr to start of region to fil</span>
						LONG <span style="color: #000040;">*</span>p <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>LONG <span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span>p32 <span style="color: #000040;">+</span> x<span style="color: #008080;">;</span>
&nbsp;
						BitmapData<span style="color: #008000;">&#91;</span>x<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>y<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> RGB<span style="color: #008000;">&#40;</span>GetBValue<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>DWORD<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>p<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>,
								   			  GetGValue<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>DWORD<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>p<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>,
											  GetRValue<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>DWORD<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>p<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
					<span style="color: #008000;">&#125;</span>	<span style="color: #666666;">//end for </span>
&nbsp;
					<span style="color: #666666;">// Go to next row</span>
					<span style="color: #666666;">//(remember, the bitmap is inverted vertically</span>
					p32 <span style="color: #000040;">-</span><span style="color: #000080;">=</span> bm32.<span style="color: #007788;">bmWidthBytes</span><span style="color: #008080;">;</span>
				<span style="color: #008000;">&#125;</span>	<span style="color: #666666;">//end for </span>
&nbsp;
				<span style="color: #666666;">// Clean up</span>
				SelectObject<span style="color: #008000;">&#40;</span>hDC, holdBmp<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
				DeleteDC<span style="color: #008000;">&#40;</span>hDC<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			<span style="color: #008000;">&#125;</span>
&nbsp;
			DeleteObject<span style="color: #008000;">&#40;</span>SelectObject<span style="color: #008000;">&#40;</span>hMemDC, holdBmp<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span>
&nbsp;
		DeleteDC<span style="color: #008000;">&#40;</span>hMemDC<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #0000ff;">return</span> TRUE<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">void</span> CBitmapOp<span style="color: #008080;">::</span><span style="color: #007788;">SetBitmapDataSize</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> x, <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> y<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	m_PixelNumberX <span style="color: #000080;">=</span> x<span style="color: #008080;">;</span>
	m_PixelNumberY <span style="color: #000080;">=</span> y<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>x<span style="color: #000080;">&gt;=</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">&amp;&amp;</span> <span style="color: #008000;">&#40;</span>y<span style="color: #000080;">&gt;=</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>		
		BitmapData.<span style="color: #007788;">resize</span><span style="color: #008000;">&#40;</span>m_PixelNumberX<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> 
		<span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> x <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> x <span style="color: #000080;">&lt;</span> m_PixelNumberX<span style="color: #008080;">;</span> x<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span> 
			BitmapData<span style="color: #008000;">&#91;</span>x<span style="color: #008000;">&#93;</span>.<span style="color: #007788;">resize</span><span style="color: #008000;">&#40;</span>m_PixelNumberY<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> 
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
vector<span style="color: #000080;">&lt;</span>vector<span style="color: #000080;">&lt;</span>COLORREF<span style="color: #000080;">&gt;</span> <span style="color: #000080;">&gt;</span> CBitmapOp<span style="color: #008080;">::</span><span style="color: #007788;">GetBitmapArray</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">return</span> BitmapData<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>




<h3 id="toc-cgraphctrl">CGraphCtrl</h3>

the part of the overloaded CStatic class responsible for loading and saving of the bitmap:


<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">BOOL CGraphCtrl<span style="color: #008080;">::</span><span style="color: #007788;">SaveBitmap</span><span style="color: #008000;">&#40;</span>CString csFileName<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	CPaintDC dc<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">this</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	PlotToDC<span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>dc<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	BOOL Result <span style="color: #000080;">=</span> BitmapOperations.<span style="color: #007788;">SaveDcToBitmapFile</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>LPCTSTR<span style="color: #008000;">&#41;</span>csFileName, <span style="color: #000040;">&amp;</span>dc, GetSize<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">return</span> Result<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
BOOL CGraphCtrl<span style="color: #008080;">::</span><span style="color: #007788;">LoadBitmap</span><span style="color: #008000;">&#40;</span>CString csFileName<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	BOOL Result <span style="color: #000080;">=</span> LoadBitmapData<span style="color: #008000;">&#40;</span>csFileName<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>	
	<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>Result<span style="color: #000080;">==</span>FALSE<span style="color: #008000;">&#41;</span>
		<span style="color: #0000ff;">return</span> Result<span style="color: #008080;">;</span>
	UpdatePlot<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">return</span> Result<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
BOOL CGraphCtrl<span style="color: #008080;">::</span><span style="color: #007788;">LoadBitmapData</span><span style="color: #008000;">&#40;</span>CString csFileName<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	CBitmap DrawMeBmp<span style="color: #008080;">;</span>
	BOOL Result <span style="color: #000080;">=</span> BitmapOperations.<span style="color: #007788;">LoadBitmapFileToCBitmap</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>LPCTSTR<span style="color: #008000;">&#41;</span>csFileName, DrawMeBmp, <span style="color: #0000ff;">NULL</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>Result<span style="color: #000080;">==</span>FALSE<span style="color: #008000;">&#41;</span>
		<span style="color: #0000ff;">return</span> Result<span style="color: #008080;">;</span>
	Result <span style="color: #000080;">=</span> BitmapOperations.<span style="color: #007788;">GetBitmapPixels</span><span style="color: #008000;">&#40;</span>DrawMeBmp<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>Result<span style="color: #000080;">==</span>FALSE<span style="color: #008000;">&#41;</span>
		<span style="color: #0000ff;">return</span> Result<span style="color: #008080;">;</span>
&nbsp;
	BITMAP bmpInfo<span style="color: #008080;">;</span>
    DrawMeBmp.<span style="color: #007788;">GetBitmap</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>bmpInfo<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	SetPlotDataSize<span style="color: #008000;">&#40;</span>bmpInfo.<span style="color: #007788;">bmWidth</span>, bmpInfo.<span style="color: #007788;">bmHeight</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	SetPlotData<span style="color: #008000;">&#40;</span>BitmapOperations.<span style="color: #007788;">GetBitmapArray</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">return</span> Result<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>




<p>Any ideas, correction or help concerning this classes and problems is appreciated.</p>

<p>Related posts:<ol><li><a href='http://www.matthiaspospiech.de/blog/2007/04/26/mfc-enhanced-cmemdc/' rel='bookmark' title='Permanent Link: Enhanced CMemDC'>Enhanced CMemDC</a></li>
<li><a href='http://www.matthiaspospiech.de/blog/2007/03/06/mfc-books-to-read/' rel='bookmark' title='Permanent Link: Books to read'>Books to read</a></li>
<li><a href='http://www.matthiaspospiech.de/blog/2007/03/07/mfc-large-and-confusing-wizard-results/' rel='bookmark' title='Permanent Link: Large and confusing wizard results'>Large and confusing wizard results</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.matthiaspospiech.de/blog/2007/04/30/open-save-and-read-bitmaps/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Show Dialog on Second Screen</title>
		<link>http://www.matthiaspospiech.de/blog/2007/04/27/mfc-show-dialog-on-second-screen/</link>
		<comments>http://www.matthiaspospiech.de/blog/2007/04/27/mfc-show-dialog-on-second-screen/#comments</comments>
		<pubDate>Fri, 27 Apr 2007 13:20:52 +0000</pubDate>
		<dc:creator>pospiech</dc:creator>
				<category><![CDATA[MFC]]></category>

		<guid isPermaLink="false">http://www.pospiech.eu/blog/2007/04/27/mfc-show-dialog-on-second-screen/</guid>
		<description><![CDATA[The following Code maximises a dialog on the second screen.

The code is available for download:




void DlgSecondScreen::ResizeDialog&#40;const CRect newRect&#41;
&#123;	
	// Resize Dialog 			
	SetWindowPos&#40;&#38;CWnd::wndTop, newRect.left, newRect.top, newRect.right, newRect.bottom,		
		SWP_SHOWWINDOW &#124; // Displays the window.		
		SWP_NOCOPYBITS // Do NOT Save content
		&#41;;
&#125;
&#160;
void DlgSecondScreen::MaximizeOnSecondScreen&#40;const HWND hWndParent&#41;
&#123;	
		HMONITOR hMonPrim = MonitorFromWindow&#40;hWndParent,MONITOR_DEFAULTTOPRIMARY&#41;;			
		HMONITOR hMonSecond = MonitorFromWindow&#40;hWndParent,MONITOR_DEFAULTTONEAREST&#41;;
		MONITORINFO MonitorInfo;
		MonitorInfo.cbSize = sizeof&#40;MonitorInfo&#41;;
		// Rect of Current Monitor
		::GetMonitorInfo&#40;hMonPrim,&#38;MonitorInfo&#41;;		
		CRect Rect = MonitorInfo.rcMonitor;
		// Rect [...]


Related posts:<ol><li><a href='http://www.matthiaspospiech.de/blog/2007/04/17/mfc-add-non-modal-dialog-to-project/' rel='bookmark' title='Permanent Link: Add non modal dialog to project'>Add non modal dialog to project</a></li>
<li><a href='http://www.matthiaspospiech.de/blog/2007/04/27/mfc-flicker-free-example-with-cstatic-control/' rel='bookmark' title='Permanent Link: Flicker Free example with CStatic Control'>Flicker Free example with CStatic Control</a></li>
<li><a href='http://www.matthiaspospiech.de/blog/2007/04/26/mfc-flicker-free-example-with-dialog/' rel='bookmark' title='Permanent Link: Flicker Free example with dialog'>Flicker Free example with dialog</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>The following Code maximises a dialog on the second screen.</p>

The code is available for download:
Note: There is a file embedded within this post, please visit this post to download the file.

<span id="more-96"></span>


<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> DlgSecondScreen<span style="color: #008080;">::</span><span style="color: #007788;">ResizeDialog</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> CRect newRect<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>	
	<span style="color: #666666;">// Resize Dialog 			</span>
	SetWindowPos<span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>CWnd<span style="color: #008080;">::</span><span style="color: #007788;">wndTop</span>, newRect.<span style="color: #007788;">left</span>, newRect.<span style="color: #007788;">top</span>, newRect.<span style="color: #007788;">right</span>, newRect.<span style="color: #007788;">bottom</span>,		
		SWP_SHOWWINDOW <span style="color: #000040;">|</span> <span style="color: #666666;">// Displays the window.		</span>
		SWP_NOCOPYBITS <span style="color: #666666;">// Do NOT Save content</span>
		<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">void</span> DlgSecondScreen<span style="color: #008080;">::</span><span style="color: #007788;">MaximizeOnSecondScreen</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> HWND hWndParent<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>	
		HMONITOR hMonPrim <span style="color: #000080;">=</span> MonitorFromWindow<span style="color: #008000;">&#40;</span>hWndParent,MONITOR_DEFAULTTOPRIMARY<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>			
		HMONITOR hMonSecond <span style="color: #000080;">=</span> MonitorFromWindow<span style="color: #008000;">&#40;</span>hWndParent,MONITOR_DEFAULTTONEAREST<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		MONITORINFO MonitorInfo<span style="color: #008080;">;</span>
		MonitorInfo.<span style="color: #007788;">cbSize</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>MonitorInfo<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #666666;">// Rect of Current Monitor</span>
		<span style="color: #008080;">::</span><span style="color: #007788;">GetMonitorInfo</span><span style="color: #008000;">&#40;</span>hMonPrim,<span style="color: #000040;">&amp;</span>MonitorInfo<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>		
		CRect Rect <span style="color: #000080;">=</span> MonitorInfo.<span style="color: #007788;">rcMonitor</span><span style="color: #008080;">;</span>
		<span style="color: #666666;">// Rect of Second Monitor</span>
		<span style="color: #008080;">::</span><span style="color: #007788;">GetMonitorInfo</span><span style="color: #008000;">&#40;</span>hMonSecond,<span style="color: #000040;">&amp;</span>MonitorInfo<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>		
		CRect RectSecond <span style="color: #000080;">=</span> MonitorInfo.<span style="color: #007788;">rcMonitor</span><span style="color: #008080;">;</span>
		<span style="color: #666666;">// New Rect with second monitor</span>
		Rect.<span style="color: #007788;">left</span> <span style="color: #000080;">=</span> Rect.<span style="color: #007788;">Width</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>		
		Rect.<span style="color: #007788;">right</span> <span style="color: #000080;">=</span> Rect.<span style="color: #007788;">left</span> <span style="color: #000040;">+</span> RectSecond.<span style="color: #007788;">Width</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		Rect.<span style="color: #007788;">bottom</span> <span style="color: #000080;">=</span> RectSecond.<span style="color: #007788;">Height</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
		ResizeDialog<span style="color: #008000;">&#40;</span>Rect<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>




<p>call by the parent dialog with</p>


<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> CDrawOnSecondScreenDlg<span style="color: #008080;">::</span><span style="color: #007788;">OnBnClickedButtonShow</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>theApp.<span style="color: #007788;">m_pDlgSecondScreen</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		theApp.<span style="color: #007788;">m_pDlgSecondScreen</span><span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>SetForegroundWindow<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		theApp.<span style="color: #007788;">m_pDlgSecondScreen</span><span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>SetFocus<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span>		
		theApp.<span style="color: #007788;">m_pDlgSecondScreen</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> DlgSecondScreen<span style="color: #008080;">;</span>
		theApp.<span style="color: #007788;">m_pDlgSecondScreen</span><span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>Create<span style="color: #008000;">&#40;</span>DlgSecondScreen<span style="color: #008080;">::</span><span style="color: #007788;">IDD</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>				
&nbsp;
		<span style="color: #666666;">// Get handle of Monitor		</span>
		HWND hWndParent <span style="color: #000080;">=</span> <span style="color: #008080;">::</span><span style="color: #007788;">GetParent</span><span style="color: #008000;">&#40;</span>m_hWnd<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		theApp.<span style="color: #007788;">m_pDlgSecondScreen</span><span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>MaximizeOnSecondScreen<span style="color: #008000;">&#40;</span>hWndParent<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #666666;">// Ausgabe initialisieren		</span>
		theApp.<span style="color: #007788;">m_pDlgSecondScreen</span><span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>ShowWindow<span style="color: #008000;">&#40;</span>SW_SHOW<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>	
	<span style="color: #008000;">&#125;</span>	
<span style="color: #008000;">&#125;</span></pre></div></div>




<p>Related posts:<ol><li><a href='http://www.matthiaspospiech.de/blog/2007/04/17/mfc-add-non-modal-dialog-to-project/' rel='bookmark' title='Permanent Link: Add non modal dialog to project'>Add non modal dialog to project</a></li>
<li><a href='http://www.matthiaspospiech.de/blog/2007/04/27/mfc-flicker-free-example-with-cstatic-control/' rel='bookmark' title='Permanent Link: Flicker Free example with CStatic Control'>Flicker Free example with CStatic Control</a></li>
<li><a href='http://www.matthiaspospiech.de/blog/2007/04/26/mfc-flicker-free-example-with-dialog/' rel='bookmark' title='Permanent Link: Flicker Free example with dialog'>Flicker Free example with dialog</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.matthiaspospiech.de/blog/2007/04/27/mfc-show-dialog-on-second-screen/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Flicker Free example with CStatic Control</title>
		<link>http://www.matthiaspospiech.de/blog/2007/04/27/mfc-flicker-free-example-with-cstatic-control/</link>
		<comments>http://www.matthiaspospiech.de/blog/2007/04/27/mfc-flicker-free-example-with-cstatic-control/#comments</comments>
		<pubDate>Fri, 27 Apr 2007 11:14:11 +0000</pubDate>
		<dc:creator>pospiech</dc:creator>
				<category><![CDATA[MFC]]></category>

		<guid isPermaLink="false">http://www.pospiech.eu/blog/2007/04/27/mfc-flicker-free-example-with-cstatic-control/</guid>
		<description><![CDATA[The following code shows how to paint into a CStatic Control and make sure this painting is flicker-free.

Overloading of CStaticCGraphCtrlFlicker Free drawingResizing of Images

The example code for download:



Overloading of CStatic
Unfortunately one can not draw inside a CStatic control directly. Therefore one need to overload the CStatic class an do the following (let CGraphCtrl be the [...]


Related posts:<ol><li><a href='http://www.matthiaspospiech.de/blog/2007/04/26/mfc-flicker-free-example-with-dialog/' rel='bookmark' title='Permanent Link: Flicker Free example with dialog'>Flicker Free example with dialog</a></li>
<li><a href='http://www.matthiaspospiech.de/blog/2007/04/26/mfc-enhanced-cmemdc/' rel='bookmark' title='Permanent Link: Enhanced CMemDC'>Enhanced CMemDC</a></li>
<li><a href='http://www.matthiaspospiech.de/blog/2007/04/27/mfc-show-dialog-on-second-screen/' rel='bookmark' title='Permanent Link: Show Dialog on Second Screen'>Show Dialog on Second Screen</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>The following code shows how to paint into a CStatic Control and make sure this painting is flicker-free.</p>

<a id="toc"></a><ol class="toc"><li><a href="http://www.matthiaspospiech.de/blog/2007/04/27/mfc-flicker-free-example-with-cstatic-control/#toc-overloading-of-cstatic" class="liinternal">Overloading of CStatic</a></li><li><a href="http://www.matthiaspospiech.de/blog/2007/04/27/mfc-flicker-free-example-with-cstatic-control/2/#toc-cgraphctrl" class="liinternal">CGraphCtrl</a></li><li><a href="http://www.matthiaspospiech.de/blog/2007/04/27/mfc-flicker-free-example-with-cstatic-control/3/#toc-flicker-free-drawing" class="liinternal">Flicker Free drawing</a></li><li><a href="http://www.matthiaspospiech.de/blog/2007/04/27/mfc-flicker-free-example-with-cstatic-control/3/#toc-resizing-of-images" class="liinternal">Resizing of Images</a></li></ol>

The example code for download:
Note: There is a file embedded within this post, please visit this post to download the file.

<span id="more-94"></span>

<h3 id="toc-overloading-of-cstatic">Overloading of CStatic</h3>
<p>Unfortunately one can not draw inside a CStatic control directly. Therefore one need to overload the CStatic class an do the following (let CGraphCtrl be the overloaded class)</p>
<ol>
	<li>make CGraphCtrl know what has to be draw (pass the data)</li>
	<li>Invalidate the content of CGraphCtrl and call UpdateWindow</li>
	<li>Each UpdateWindow results in OnPaint being processed. Inside this function we can now do the painting we have remembered to do</li>
</ol>

<p>The calls presented here saves its data in</p>


<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">vector<span style="color: #000080;">&lt;</span>vector <span style="color: #000080;">&lt;</span>COLORREF<span style="color: #000080;">&gt;</span> <span style="color: #000080;">&gt;</span> PlotData<span style="color: #008080;">;</span></pre></div></div>




<p>and includes methods for resizing</p>


<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">	<span style="color: #0000ff;">void</span> Resize<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">short</span> width, <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">short</span> height<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">void</span> FullResize<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> CRect newRect<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">void</span> SetSize<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">short</span> width, <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">short</span> height<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">void</span> SetPlotDataSize<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> x, <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> y<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	CRect GetSize<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	CRect GetPlotDataSize<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>




<p>and the actual painting</p>


<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">	<span style="color: #0000ff;">void</span> SetPlotData<span style="color: #008000;">&#40;</span>vector<span style="color: #000080;">&lt;</span><span style="color: #000040;">/</span>vector<span style="color: #000080;">&gt;&lt;</span>vector <span style="color: #000080;">&lt;</span>COLORREF<span style="color: #000080;">&gt;</span> <span style="color: #000080;">&gt;</span> InputArray<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">void</span> UpdatePlot<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">void</span> PaintPixel<span style="color: #008000;">&#40;</span>CMemDC<span style="color: #000040;">*</span> pDC, <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> x, <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> y, COLORREF c<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">void</span> PlotToDC<span style="color: #008000;">&#40;</span>CMemDC<span style="color: #000040;">*</span> pDC<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>




<div class="toc-page-link">Next page: <a href="http://www.matthiaspospiech.de/blog/2007/04/27/mfc-flicker-free-example-with-cstatic-control/2/" class="liinternal">&ldquo;CGraphCtrl&rdquo;</a></div>

<p>Related posts:<ol><li><a href='http://www.matthiaspospiech.de/blog/2007/04/26/mfc-flicker-free-example-with-dialog/' rel='bookmark' title='Permanent Link: Flicker Free example with dialog'>Flicker Free example with dialog</a></li>
<li><a href='http://www.matthiaspospiech.de/blog/2007/04/26/mfc-enhanced-cmemdc/' rel='bookmark' title='Permanent Link: Enhanced CMemDC'>Enhanced CMemDC</a></li>
<li><a href='http://www.matthiaspospiech.de/blog/2007/04/27/mfc-show-dialog-on-second-screen/' rel='bookmark' title='Permanent Link: Show Dialog on Second Screen'>Show Dialog on Second Screen</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.matthiaspospiech.de/blog/2007/04/27/mfc-flicker-free-example-with-cstatic-control/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Flicker Free example with dialog</title>
		<link>http://www.matthiaspospiech.de/blog/2007/04/26/mfc-flicker-free-example-with-dialog/</link>
		<comments>http://www.matthiaspospiech.de/blog/2007/04/26/mfc-flicker-free-example-with-dialog/#comments</comments>
		<pubDate>Thu, 26 Apr 2007 20:01:58 +0000</pubDate>
		<dc:creator>pospiech</dc:creator>
				<category><![CDATA[MFC]]></category>

		<guid isPermaLink="false">http://www.pospiech.eu/blog/2007/04/26/mfc-flicker-free-example-with-dialog/</guid>
		<description><![CDATA[One can find several examples for flicker free drawing in MFC. (see for example Flicker Free Drawing In MFC)

Here is an example done by myself. I used the CMemDC Class posted in Enhanced CMemDC. 

The example code for download:



What is the magic? We need to make sure, that the background of our window is not [...]


Related posts:<ol><li><a href='http://www.matthiaspospiech.de/blog/2007/04/27/mfc-flicker-free-example-with-cstatic-control/' rel='bookmark' title='Permanent Link: Flicker Free example with CStatic Control'>Flicker Free example with CStatic Control</a></li>
<li><a href='http://www.matthiaspospiech.de/blog/2007/04/27/mfc-show-dialog-on-second-screen/' rel='bookmark' title='Permanent Link: Show Dialog on Second Screen'>Show Dialog on Second Screen</a></li>
<li><a href='http://www.matthiaspospiech.de/blog/2007/04/17/mfc-add-non-modal-dialog-to-project/' rel='bookmark' title='Permanent Link: Add non modal dialog to project'>Add non modal dialog to project</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>One can find several examples for flicker free drawing in MFC. (see for example <a href="http://www.codeproject.com/gdi/flickerfree.asp" class="liexternal">Flicker Free Drawing In MFC</a>)</p>

<p>Here is an example done by myself. I used the CMemDC Class posted in <a href="/blog/2007/04/26/mfc-enhanced-cmemdc/" class="liinternal">Enhanced CMemDC</a>. </p>

The example code for download:
Note: There is a file embedded within this post, please visit this post to download the file.

<span id="more-92"></span>

<p>What is the magic? We need to make sure, that the background of our window is not painted when OnPaint is called. For that we need to include the message <code>ON_WM_ERASEBKGND()</code> with the simple content in its function</p>


<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">BOOL CFlickerFreeDemoDlg<span style="color: #008080;">::</span><span style="color: #007788;">OnEraseBkgnd</span><span style="color: #008000;">&#40;</span>CDC<span style="color: #000040;">*</span> pDC<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">return</span> FALSE<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>




The painting is triggered with a timer initialised in <code>OnInitDialog()</code>


<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">BOOL CFlickerFreeDemoDlg<span style="color: #008080;">::</span><span style="color: #007788;">OnInitDialog</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	SetTimer<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span>, <span style="color: #0000dd;">33</span>, <span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
...</pre></div></div>




with


<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> CFlickerFreeDemoDlg<span style="color: #008080;">::</span><span style="color: #007788;">OnTimer</span><span style="color: #008000;">&#40;</span>UINT nIDEvent<span style="color: #008000;">&#41;</span> 
<span style="color: #008000;">&#123;</span>
	PixelArray.<span style="color: #007788;">IterateNewContent</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	UpdatePlot<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>




<code>Updateplot()</code> is used to invalidate the window and trigger the OnPaint message


<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> CFlickerFreeDemoDlg<span style="color: #008080;">::</span><span style="color: #007788;">UpdatePlot</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>		
	Invalidate<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	UpdateWindow<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>	
<span style="color: #008000;">&#125;</span></pre></div></div>





<p>Related posts:<ol><li><a href='http://www.matthiaspospiech.de/blog/2007/04/27/mfc-flicker-free-example-with-cstatic-control/' rel='bookmark' title='Permanent Link: Flicker Free example with CStatic Control'>Flicker Free example with CStatic Control</a></li>
<li><a href='http://www.matthiaspospiech.de/blog/2007/04/27/mfc-show-dialog-on-second-screen/' rel='bookmark' title='Permanent Link: Show Dialog on Second Screen'>Show Dialog on Second Screen</a></li>
<li><a href='http://www.matthiaspospiech.de/blog/2007/04/17/mfc-add-non-modal-dialog-to-project/' rel='bookmark' title='Permanent Link: Add non modal dialog to project'>Add non modal dialog to project</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.matthiaspospiech.de/blog/2007/04/26/mfc-flicker-free-example-with-dialog/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Enhanced CMemDC</title>
		<link>http://www.matthiaspospiech.de/blog/2007/04/26/mfc-enhanced-cmemdc/</link>
		<comments>http://www.matthiaspospiech.de/blog/2007/04/26/mfc-enhanced-cmemdc/#comments</comments>
		<pubDate>Thu, 26 Apr 2007 16:09:55 +0000</pubDate>
		<dc:creator>pospiech</dc:creator>
				<category><![CDATA[MFC]]></category>

		<guid isPermaLink="false">http://www.pospiech.eu/blog/2007/04/26/mfc-enhanced-cmemdc/</guid>
		<description><![CDATA[Flicker Free drawing onto a dc can be realised by using the class CMemDC by Keith Rule (Flicker free drawing using memory DC)

I changed the Class in such a way that it supports the automatic resizing of images that have higher or lower size than the dc that it is painted onto. The change is [...]


Related posts:<ol><li><a href='http://www.matthiaspospiech.de/blog/2007/04/27/mfc-flicker-free-example-with-cstatic-control/' rel='bookmark' title='Permanent Link: Flicker Free example with CStatic Control'>Flicker Free example with CStatic Control</a></li>
<li><a href='http://www.matthiaspospiech.de/blog/2007/04/26/mfc-flicker-free-example-with-dialog/' rel='bookmark' title='Permanent Link: Flicker Free example with dialog'>Flicker Free example with dialog</a></li>
<li><a href='http://www.matthiaspospiech.de/blog/2007/04/27/mfc-show-dialog-on-second-screen/' rel='bookmark' title='Permanent Link: Show Dialog on Second Screen'>Show Dialog on Second Screen</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Flicker Free drawing onto a dc can be realised by using the class CMemDC by Keith Rule (<a href="http://www.codeguru.com/Cpp/misc/misc/flickerfreedrawing/article.php/c389" class="liexternal">Flicker free drawing using memory DC</a>)</p>

<p>I changed the Class in such a way that it supports the automatic resizing of images that have higher or lower size than the dc that it is painted onto. The change is thus from using BitBlt to StretchBlt.</p>

<span id="more-91"></span>

CMemDC.h


<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#ifndef _MEMDC_H_</span>
<span style="color: #339900;">#define _MEMDC_H_</span>
&nbsp;
<span style="color: #666666;">//////////////////////////////////////////////////</span>
<span style="color: #666666;">// CMemDC - memory DC</span>
<span style="color: #666666;">//</span>
<span style="color: #666666;">// Author: Keith Rule</span>
<span style="color: #666666;">// Email:  keithr@europa.com</span>
<span style="color: #666666;">// Copyright 1996-2002, Keith Rule</span>
<span style="color: #666666;">//</span>
<span style="color: #666666;">// You may freely use or modify this code provided this</span>
<span style="color: #666666;">// Copyright is included in all derived versions.</span>
<span style="color: #666666;">//</span>
<span style="color: #666666;">// History - 10/3/97 Fixed scrolling bug.</span>
<span style="color: #666666;">//                   Added print support. - KR</span>
<span style="color: #666666;">//</span>
<span style="color: #666666;">//           11/3/99 Fixed most common complaint. Added</span>
<span style="color: #666666;">//                   background color fill. - KR</span>
<span style="color: #666666;">//</span>
<span style="color: #666666;">//           11/3/99 Added support for mapping modes other than</span>
<span style="color: #666666;">//                   MM_TEXT as suggested by Lee Sang Hun. - KR</span>
<span style="color: #666666;">//</span>
<span style="color: #666666;">//           02/11/02 Added support for CScrollView as supplied</span>
<span style="color: #666666;">//                    by Gary Kirkham. - KR</span>
<span style="color: #666666;">//</span>
<span style="color: #666666;">// This class implements a memory Device Context which allows</span>
<span style="color: #666666;">// flicker free drawing.</span>
&nbsp;
<span style="color: #666666;">// Changed by Matthias Pospiech (2007) with additional support </span>
<span style="color: #666666;">// of stretching a bitmap onto a dc.</span>
&nbsp;
<span style="color: #0000ff;">class</span> CMemDC <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> CDC <span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">private</span><span style="color: #008080;">:</span>	
	CBitmap		m_bitmap<span style="color: #008080;">;</span>		<span style="color: #666666;">// Offscreen bitmap</span>
	CBitmap<span style="color: #000040;">*</span>	m_oldBitmap<span style="color: #008080;">;</span>	<span style="color: #666666;">// bitmap originally found in CMemDC</span>
	CDC<span style="color: #000040;">*</span>		m_pDC<span style="color: #008080;">;</span>			<span style="color: #666666;">// Saves CDC passed in constructor</span>
	CRect		m_rect_dest<span style="color: #008080;">;</span>	<span style="color: #666666;">// Rectangle of drawing area.</span>
	CRect		m_rect_source<span style="color: #008080;">;</span>  <span style="color: #666666;">// Rectangle of source area.</span>
	BOOL		m_bMemDC<span style="color: #008080;">;</span>		<span style="color: #666666;">// TRUE if CDC really is a Memory DC.</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
&nbsp;
	CMemDC<span style="color: #008000;">&#40;</span>CDC<span style="color: #000040;">*</span> pDC, <span style="color: #0000ff;">const</span> CRect<span style="color: #000040;">*</span> pSourceRect <span style="color: #000080;">=</span> <span style="color: #0000ff;">NULL</span>, <span style="color: #0000ff;">const</span> CRect<span style="color: #000040;">*</span> pDestRect <span style="color: #000080;">=</span> <span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span> <span style="color: #008080;">:</span> CDC<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		ASSERT<span style="color: #008000;">&#40;</span>pDC <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> 
&nbsp;
		<span style="color: #666666;">// Some initialization</span>
		m_pDC <span style="color: #000080;">=</span> pDC<span style="color: #008080;">;</span>
		m_oldBitmap <span style="color: #000080;">=</span> <span style="color: #0000ff;">NULL</span><span style="color: #008080;">;</span>
		m_bMemDC <span style="color: #000080;">=</span> <span style="color: #000040;">!</span>pDC<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>IsPrinting<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
		<span style="color: #666666;">// Get the rectangle to draw</span>
		<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>pDestRect <span style="color: #000080;">==</span> <span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
			pDC<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>GetClipBox<span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>m_rect_dest<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span>
			m_rect_dest <span style="color: #000080;">=</span> <span style="color: #000040;">*</span>pDestRect<span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span>
		<span style="color: #666666;">// Get the rectangle to draw</span>
		<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>pSourceRect <span style="color: #000080;">==</span> <span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
			pDC<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>GetClipBox<span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>m_rect_source<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span>
			m_rect_source <span style="color: #000080;">=</span> <span style="color: #000040;">*</span>pSourceRect<span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span>
&nbsp;
		<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>m_bMemDC<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
			<span style="color: #666666;">// Create a Memory DC</span>
			CreateCompatibleDC<span style="color: #008000;">&#40;</span>pDC<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			pDC<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>LPtoDP<span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>m_rect_dest<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
			m_bitmap.<span style="color: #007788;">CreateCompatibleBitmap</span><span style="color: #008000;">&#40;</span>pDC, m_rect_dest.<span style="color: #007788;">Width</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, m_rect_dest.<span style="color: #007788;">Height</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			m_oldBitmap <span style="color: #000080;">=</span> SelectObject<span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>m_bitmap<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
			SetMapMode<span style="color: #008000;">&#40;</span>pDC<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>GetMapMode<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
			SetWindowExt<span style="color: #008000;">&#40;</span>pDC<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>GetWindowExt<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			SetViewportExt<span style="color: #008000;">&#40;</span>pDC<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>GetViewportExt<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
			pDC<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>DPtoLP<span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>m_rect_dest<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			SetWindowOrg<span style="color: #008000;">&#40;</span>m_rect_dest.<span style="color: #007788;">left</span>, m_rect_dest.<span style="color: #007788;">top</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span>
			<span style="color: #666666;">// Make a copy of the relevent parts of the current DC for printing</span>
			m_bPrinting <span style="color: #000080;">=</span> pDC<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>m_bPrinting<span style="color: #008080;">;</span>
			m_hDC       <span style="color: #000080;">=</span> pDC<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>m_hDC<span style="color: #008080;">;</span>
			m_hAttribDC <span style="color: #000080;">=</span> pDC<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>m_hAttribDC<span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span>
&nbsp;
		<span style="color: #666666;">// Fill background </span>
		FillSolidRect<span style="color: #008000;">&#40;</span>m_rect_dest, pDC<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>GetBkColor<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	~CMemDC<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>	
	<span style="color: #008000;">&#123;</span>		
		<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>m_bMemDC<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
&nbsp;
			m_pDC<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>StretchBlt<span style="color: #008000;">&#40;</span>
				m_rect_dest.<span style="color: #007788;">left</span>, <span style="color: #666666;">// x-coord of destination upper-left corner</span>
				m_rect_dest.<span style="color: #007788;">top</span>,  <span style="color: #666666;">// y-coord of destination upper-left corner</span>
				m_rect_dest.<span style="color: #007788;">Width</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, <span style="color: #666666;">// width of destination rectangle</span>
				m_rect_dest.<span style="color: #007788;">Height</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, <span style="color: #666666;">// height of destination rectangle</span>
				<span style="color: #0000dd;">this</span>, <span style="color: #666666;">// handle to source DC</span>
				<span style="color: #0000dd;">0</span>, <span style="color: #666666;">// x-coord of source upper-left corner</span>
				<span style="color: #0000dd;">0</span>, <span style="color: #666666;">// y-coord of source upper-left corner</span>
				m_rect_source.<span style="color: #007788;">Width</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, <span style="color: #666666;">// width of source rectangle</span>
				m_rect_source.<span style="color: #007788;">Height</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>,  <span style="color: #666666;">// height of source rectangle</span>
				SRCCOPY<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
			<span style="color: #666666;">//Swap back the original bitmap.</span>
			SelectObject<span style="color: #008000;">&#40;</span>m_oldBitmap<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>		
		<span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span>
			<span style="color: #666666;">// All we need to do is replace the DC with an illegal value,</span>
			<span style="color: #666666;">// this keeps us from accidently deleting the handles associated with</span>
			<span style="color: #666666;">// the CDC that was passed to the constructor.			</span>
			m_hDC <span style="color: #000080;">=</span> m_hAttribDC <span style="color: #000080;">=</span> <span style="color: #0000ff;">NULL</span><span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span>	
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #666666;">// Allow usage as a pointer	</span>
	CMemDC<span style="color: #000040;">*</span> operator<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> 
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">this</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>	
&nbsp;
	<span style="color: #666666;">// Allow usage as a pointer	</span>
	operator CMemDC<span style="color: #000040;">*</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> 
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">this</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #339900;">#endif</span></pre></div></div>




<p>It is used the same way as the original, just with an additional parameter for the size of the source.</p>

<p>Related posts:<ol><li><a href='http://www.matthiaspospiech.de/blog/2007/04/27/mfc-flicker-free-example-with-cstatic-control/' rel='bookmark' title='Permanent Link: Flicker Free example with CStatic Control'>Flicker Free example with CStatic Control</a></li>
<li><a href='http://www.matthiaspospiech.de/blog/2007/04/26/mfc-flicker-free-example-with-dialog/' rel='bookmark' title='Permanent Link: Flicker Free example with dialog'>Flicker Free example with dialog</a></li>
<li><a href='http://www.matthiaspospiech.de/blog/2007/04/27/mfc-show-dialog-on-second-screen/' rel='bookmark' title='Permanent Link: Show Dialog on Second Screen'>Show Dialog on Second Screen</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.matthiaspospiech.de/blog/2007/04/26/mfc-enhanced-cmemdc/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Convert Cstring to int</title>
		<link>http://www.matthiaspospiech.de/blog/2007/04/17/mfc-convert-cstring-to-int/</link>
		<comments>http://www.matthiaspospiech.de/blog/2007/04/17/mfc-convert-cstring-to-int/#comments</comments>
		<pubDate>Tue, 17 Apr 2007 13:49:52 +0000</pubDate>
		<dc:creator>pospiech</dc:creator>
				<category><![CDATA[MFC]]></category>

		<guid isPermaLink="false">http://www.pospiech.eu/blog/2007/04/17/mfc-convert-cstring-to-int/</guid>
		<description><![CDATA[The solution is the following


CString samplecstring;	
int i = _ttoi&#40;samplecstring&#41;;




further conversion examples are listed at http://www.codeproject.com/cpp/data_conversions.asp.

Converting backwards from int to CString can be done via


int i  = 255;
Cstring text;
text.Format&#40;_T&#40;&#34;%d&#34;&#41;, i&#41;;




Related posts:convert Images in batch process



Related posts:<ol><li><a href='http://www.matthiaspospiech.de/blog/2008/08/23/convert-images-in-batch-process/' rel='bookmark' title='Permanent Link: convert Images in batch process'>convert Images in batch process</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>The solution is the following</p>


<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">CString samplecstring<span style="color: #008080;">;</span>	
<span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> _ttoi<span style="color: #008000;">&#40;</span>samplecstring<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>




<p>further conversion examples are listed at <a href="http://www.codeproject.com/cpp/data_conversions.asp" class="liexternal">http://www.codeproject.com/cpp/data_conversions.asp</a>.</p>

<p>Converting backwards from int to CString can be done via</p>


<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> i  <span style="color: #000080;">=</span> <span style="color: #0000dd;">255</span><span style="color: #008080;">;</span>
Cstring text<span style="color: #008080;">;</span>
text.<span style="color: #007788;">Format</span><span style="color: #008000;">&#40;</span>_T<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;%d&quot;</span><span style="color: #008000;">&#41;</span>, i<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>




<p>Related posts:<ol><li><a href='http://www.matthiaspospiech.de/blog/2008/08/23/convert-images-in-batch-process/' rel='bookmark' title='Permanent Link: convert Images in batch process'>convert Images in batch process</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.matthiaspospiech.de/blog/2007/04/17/mfc-convert-cstring-to-int/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
