<?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>Code Library &#187; Win32/MFC</title>
	<atom:link href="http://www.ucosoft.com/tag/win32mfc/feed" rel="self" type="application/rss+xml" />
	<link>http://www.ucosoft.com</link>
	<description>Free Source Code and Program Tips</description>
	<lastBuildDate>Thu, 22 Jul 2010 05:17:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>How to get ClistCtrl item text of another process</title>
		<link>http://www.ucosoft.com/get-clistctrl-item-text-of-another-process.html</link>
		<comments>http://www.ucosoft.com/get-clistctrl-item-text-of-another-process.html#comments</comments>
		<pubDate>Wed, 24 Sep 2008 08:19:30 +0000</pubDate>
		<dc:creator>support</dc:creator>
				<category><![CDATA[Win32/MFC]]></category>
		<category><![CDATA[CListCtrl]]></category>
		<category><![CDATA[How-to]]></category>
		<category><![CDATA[Injection]]></category>
		<category><![CDATA[Process]]></category>

		<guid isPermaLink="false">http://www.ucosoft.com/2008/09/24/get-clistctrl-item-text-of-another-process.html</guid>
		<description><![CDATA[Question: In some app like WinSpy/Spy++ to make adjustments to list controls. It can correctly set an app&#8217;s list control&#8217;s modes, alignments, sorting, styles, and extended styles. It can also get the widths of the columns and count of items. The problem is that you cannot seem to get the columns&#8217; names or the item [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Question:</strong></p>
<p>In some app like WinSpy/Spy++ to make adjustments to list controls. It can correctly set an app&#8217;s list control&#8217;s modes, alignments,    <br />sorting, styles, and extended styles. It can also get the widths of the columns and count of items. </p>
<p>The problem is that you cannot seem to get the columns&#8217; names or the item text. The app uses code like this, where lc is a pointer to the target app&#8217;s list control: </p>
<p>&#160; lc-&gt;GetColumnWidth();    <br />&#160; lc-&gt;GetItemCount();     <br />&#160; lc-&gt;ModifyStyle();     <br />&#160; lc-&gt;SetExtendedStyle();     <br />&#160; lc-&gt;GetHeaderCtrl()-&gt;GetItemCount(); </p>
<p>That all works fine, but when you try something like this, it doesn&#8217;t work: </p>
<p>&#160; TCHAR colname[256]=_T(&quot;&quot;);    <br />&#160; LVCOLUMN lvcol;     <br />&#160; ZeroMemory(&amp;lvcol, sizeof(lvcol));     <br />&#160; lvcol.mask=LVCF_TEXT;     <br />&#160; lvcol.cchTextMax=sizeof(colname)-1;     <br />&#160; lvcol.pszText=colname;     <br />&#160; lc-&gt;GetColumn(col, &amp;lvcol); </p>
<p>GetColumn may return TRUE or FALSE, but the LVCOLUMN structure remains unchanged. If you tried using that same code in a test app to get the name of a column from that app&#8217;s own list control and it worked. It only seems to fail when use it from a different app.</p>
<p><strong><u>Solution:</u></strong></p>
<p>Well, there is no surprise here; in fact, if there is any surprise, it is that you did not manage to totally crash the target app (I presume that lc is a CListCtrl::FromHandle of an HWND in another process). </p>
<p>Think about this case:</p>
<p>you are passing the address of a data structure in YOUR process as a LPARAM-sized value to some totally different process, where that LPARAM value is completely and utterly meaningless. </p>
<p>David Ching did a remote-sendmessage DLL some time ago.&#160; This essentially does DLL injection of the code so it is running in the target process; you can google for this by looking for SendMessageRemote.</p>
<p><a href="http://www.dcsoft.com/private/sendmessageremote.cpp">http://www.dcsoft.com/private/sendmessageremote.cpp</a></p>

	Tags: <strong><a href="http://www.ucosoft.com/tag/clistctrl" title="CListCtrl" rel="tag">CListCtrl</a>, <a href="http://www.ucosoft.com/tag/how-to" title="How-to" rel="tag">How-to</a>, <a href="http://www.ucosoft.com/tag/injection" title="Injection" rel="tag">Injection</a>, <a href="http://www.ucosoft.com/tag/process" title="Process" rel="tag">Process</a>, <a href="http://www.ucosoft.com/tag/win32mfc" title="Win32/MFC" rel="tag">Win32/MFC</a></strong><br />
]]></content:encoded>
			<wfw:commentRss>http://www.ucosoft.com/get-clistctrl-item-text-of-another-process.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple CEdit Validation</title>
		<link>http://www.ucosoft.com/simple-cedit-validation.html</link>
		<comments>http://www.ucosoft.com/simple-cedit-validation.html#comments</comments>
		<pubDate>Wed, 27 Jun 2007 09:41:55 +0000</pubDate>
		<dc:creator>support</dc:creator>
				<category><![CDATA[Win32/MFC]]></category>
		<category><![CDATA[CEdit]]></category>
		<category><![CDATA[Cpp]]></category>

		<guid isPermaLink="false">http://www.ucosoft.com/2007/06/27/simple-cedit-validation.html</guid>
		<description><![CDATA[Sometime you need some CEdit Validation, for example, restricting the data number only. The simple implemetion is to handle the EN_UPDATE notification message. The EN_UPDATE message is useful to handle as it caters for both normal entry and clipboard paste. You can do that in the Dialog or subclass the CEdit class. Here is an [...]]]></description>
			<content:encoded><![CDATA[<p>Sometime you need some CEdit Validation, for example, restricting the data number only.</p>
<p>The simple implemetion is to handle the <strong>EN_UPDATE</strong> notification message. The <strong>EN_UPDATE</strong> message is useful to handle as it caters for both normal entry and clipboard paste. You can do that in the Dialog or subclass the CEdit class.</p>
<p>Here is an example of a derived CEdit class.</p>
<p><coolcode lang="cpp" linenum="no"><br />
class CNumEdit : public CEdit<br />
{<br />
//â€¦.<br />
    afx_msg void OnUpdate();<br />
    //}}AFX_MSG </p>
<p>    DECLARE_MESSAGE_MAP()<br />
private:<br />
    CString m_strLastGood;<br />
}; </p>
<p></coolcode></p>
<p>In the cpp file:</p>
<p> <span id="more-86"></span><br />
<coolcode lang="cpp" linenum="no"><br />
BEGIN_MESSAGE_MAP(CNumEdit, CEdit)<br />
    //{{AFX_MSG_MAP(CNumEdit)<br />
    ON_CONTROL_REFLECT(EN_UPDATE, OnUpdate)<br />
    //}}AFX_MSG_MAP<br />
END_MESSAGE_MAP() </p>
<p>void CNumEdit::OnUpdate()<br />
{<br />
    CString str;<br />
    GetWindowText( str );<br />
    bool bProblem = false;<br />
    for ( int indx = 0; indx < str.GetLength(); indx++ )<br />
    {<br />
        if (( str[indx] < â€˜0â€² ) || ( str[indx] > â€˜9â€²) )<br />
        {<br />
            bProblem = true;<br />
            break;<br />
        }<br />
    }<br />
    if ( bProblem )<br />
    {<br />
        int start, end;<br />
        /* Find the current caret position */<br />
        GetSel( start, end );<br />
        /* Restore the last good text that was entered */<br />
        SetWindowText( m_strLastGood );<br />
        /* Restore the caret */<br />
        SetSel( start-1, end-1, true );<br />
        /* Let the user know */<br />
        MessageBeep( MB_OK );<br />
    }<br />
    else<br />
    {<br />
    /* Store the last good entry string in a member variable of the Hex edit class*/<br />
        m_strLastGood = str;<br />
    }<br />
} </p>
<p></coolcode></p>
<p>At last, you may find one of these meets your needs:</p>
<ul>
<li>http://www.codeproject.com/editctrl/maskededit.asp</li>
<li>http://www.codeproject.com/editctrl/validatingedit.asp</li>
</ul>

	Tags: <strong><a href="http://www.ucosoft.com/tag/cedit" title="CEdit" rel="tag">CEdit</a>, <a href="http://www.ucosoft.com/tag/cpp" title="Cpp" rel="tag">Cpp</a>, <a href="http://www.ucosoft.com/tag/win32mfc" title="Win32/MFC" rel="tag">Win32/MFC</a></strong><br />
]]></content:encoded>
			<wfw:commentRss>http://www.ucosoft.com/simple-cedit-validation.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to translate among CString,string and char array</title>
		<link>http://www.ucosoft.com/how-to-translate-among-cstringstring-and-char-array.html</link>
		<comments>http://www.ucosoft.com/how-to-translate-among-cstringstring-and-char-array.html#comments</comments>
		<pubDate>Fri, 22 Jun 2007 03:56:19 +0000</pubDate>
		<dc:creator>support</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[How-to]]></category>
		<category><![CDATA[STL]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[Win32/MFC]]></category>

		<guid isPermaLink="false">http://www.ucosoft.com/2007/06/22/how-to-translate-among-cstringstring-and-char-array.html</guid>
		<description><![CDATA[In MFC program, you&#8217;d better try to avoid using std::string. There is no compelling reason to use it in MFC, and it leads to confusion. There are some standard method to translate among Cstring ,string and char[]. // Suppose CString str; std::string s; char buf[SIZE]; // from CString to std::string s = str; // from [...]]]></description>
			<content:encoded><![CDATA[<p>In MFC program, you&#8217;d better try to avoid using std::string.  There is no compelling reason to use it in MFC, and it leads to confusion.</p>
<p>There are some standard method to translate among Cstring ,string and char[].<br />
<coolcode lang="cpp" linenum="no"><br />
// Suppose<br />
CString str;<br />
std::string s;<br />
char buf[SIZE];</p>
<p>// from CString to std::string<br />
s = str;</p>
<p>// from std::string to CString<br />
str = s.c_str();</p>
<p>// to char[]<br />
_tcscpy(buf, str);<br />
_tcscpy(buf, s.c_str());</p>
<p>// from char[]<br />
str = buf;<br />
s = buf;<br />
</coolcode><br />
Also, beware of unicode. If UNICODE is defined, you should convert between unicode and ansi versions. Better to use TCHAR instead of char and std::basic_string<TCHAR> instead of std::string. </p>

	Tags: <strong><a href="http://www.ucosoft.com/tag/how-to" title="How-to" rel="tag">How-to</a>, <a href="http://www.ucosoft.com/tag/stl" title="STL" rel="tag">STL</a>, <a href="http://www.ucosoft.com/tag/string" title="string" rel="tag">string</a>, <a href="http://www.ucosoft.com/tag/win32mfc" title="Win32/MFC" rel="tag">Win32/MFC</a></strong><br />
]]></content:encoded>
			<wfw:commentRss>http://www.ucosoft.com/how-to-translate-among-cstringstring-and-char-array.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to check whether the window is in minimise or Maximise mode</title>
		<link>http://www.ucosoft.com/how-to-check-whether-the-window-is-in-minimise-or-maximise-mode.html</link>
		<comments>http://www.ucosoft.com/how-to-check-whether-the-window-is-in-minimise-or-maximise-mode.html#comments</comments>
		<pubDate>Thu, 22 Mar 2007 00:16:53 +0000</pubDate>
		<dc:creator>support</dc:creator>
				<category><![CDATA[Win32/MFC]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[CWnd]]></category>

		<guid isPermaLink="false">http://www.ucosoft.com/archives/77.html</guid>
		<description><![CDATA[CWnd::IsIconic BOOL IsIconic( ) const; Return Value Nonzero if CWnd is minimized; otherwise 0. Remarks Specifies whether CWnd is minimized (iconic). Example // This code, normally emitted by the AppWizard for a dialog-based // project for the dialog&#8217;s WM_PAINT handler, runs only if the // window is iconic. The window erase the icon&#8217;s area, then [...]]]></description>
			<content:encoded><![CDATA[</p>
<h3><a></a>CWnd::IsIconic</h3>
<p><b>BOOL</b> <b>IsIconic(</b> <b>)</b> <b>const;</b>
<p><b>Return Value</b>
<p>Nonzero if <b>CWnd</b> is minimized; otherwise 0.
<p><b>Remarks</b>
<p>Specifies whether <b>CWnd</b> is minimized (iconic).
<p><b>Example</b><br />
<coolcode lang="cpp" linenum="no"><br />
// This code, normally emitted by the AppWizard for a dialog-based<br />
// project for the dialog&#8217;s WM_PAINT handler, runs only if the<br />
// window is iconic. The window erase the icon&#8217;s area, then<br />
// paints the icon referenced by m_hIcon.</p>
<p>if (IsIconic())<br />
{<br />
   CPaintDC dc(this); // device context for painting</p>
<p>   IconEraseBkgnd(dc);</p>
<p>   // Center icon in client rectangle<br />
   int cxIcon = GetSystemMetrics(SM_CXICON);<br />
   int cyIcon = GetSystemMetrics(SM_CYICON);<br />
   CRect rect;<br />
   GetClientRect(&amp;rect);<br />
   int x = (rect.Width() &#8211; cxIcon + 1) / 2;<br />
   int y = (rect.Height() &#8211; cyIcon + 1) / 2;</p>
<p>   // Draw the icon<br />
   dc.DrawIcon(x, y, m_hIcon);<br />
}<br />
</coolcode></p>

	Tags: <strong><a href="http://www.ucosoft.com/tag/api" title="API" rel="tag">API</a>, <a href="http://www.ucosoft.com/tag/cwnd" title="CWnd" rel="tag">CWnd</a>, <a href="http://www.ucosoft.com/tag/win32mfc" title="Win32/MFC" rel="tag">Win32/MFC</a></strong><br />
]]></content:encoded>
			<wfw:commentRss>http://www.ucosoft.com/how-to-check-whether-the-window-is-in-minimise-or-maximise-mode.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>sscanf with CString</title>
		<link>http://www.ucosoft.com/sscanf-with-cstring.html</link>
		<comments>http://www.ucosoft.com/sscanf-with-cstring.html#comments</comments>
		<pubDate>Tue, 20 Mar 2007 09:01:46 +0000</pubDate>
		<dc:creator>support</dc:creator>
				<category><![CDATA[Win32/MFC]]></category>
		<category><![CDATA[CString]]></category>
		<category><![CDATA[sscanf]]></category>

		<guid isPermaLink="false">http://www.ucosoft.com/archives/76.html</guid>
		<description><![CDATA[Problem: In the C time, we used to use sscanf to read data from string as following: char strUserName[20], strPassword[20]; // if the content of m_recvBuff is &#8220;Microsoft Bill&#8221;. sscanf((const char *)m_recvBuff, &#8220;%s %s&#8221;, strUserName, strPassword); And when you first get CString in sight, you may write the&#160;similar code: CString strUserName, strPassword; // if the [...]]]></description>
			<content:encoded><![CDATA[<p><strong><u>Problem:</u></strong></p>
<p>In the C time, we used to use sscanf to read data from string as following:</p>
<p><coolcode lang="cpp" linenum="no"><br />
char strUserName[20], strPassword[20];<br />
// if the content of m_recvBuff is &#8220;Microsoft Bill&#8221;.<br />
sscanf((const char *)m_recvBuff, &#8220;%s %s&#8221;, strUserName, strPassword);<br />
</coolcode></p>
<p>And when you first get CString in sight, you may write the&nbsp;similar code:</p>
<p><coolcode lang="cpp" linenum="no"><br />
CString strUserName, strPassword;<br />
// if the content of m_recvBuff is &#8220;Microsoft Bill&#8221;.<br />
sscanf((const char *)m_recvBuff, &#8220;%s %s&#8221; , strUserName, strPassword);<br />
</coolcode></p>
<p>Unfortunately, it can not work as you expected and, in most cases, the process will crash.</p>
<p><strong><u>Solution:</u></strong><span id="more-76"></span></p>
<p>First, you should not be using sccanf, since this is not Unicode-aware. You should use _stscanf which allows for Unicode. &nbsp;In addition, it is not a &#8220;safe&#8221; methodology because it can generate buffer overruns.</p>
<p>Second, the problem here is that you are doing something profoundly illegal: you are passing a CString pointer to a call that expects an LPTSTR. &nbsp;You can&#8217;t do this. &nbsp;It won&#8217;t work, and&nbsp;it surprised me that it merely produces an erroneous result instead of crashing with an access fault, heap corruption assert, or some similar fatal situation.</p>
<p>After all, you can use scanf safely, but you should try to use the new _s versions to make them more &#8220;trustworthy&#8221;. &nbsp;Here&#8217;s some info on it: </p>
<p><a href="http://msdn2.microsoft.com/en-us/library/w40768et%28VS.80%29.aspx">http://msdn2.microsoft.com/en-us/library/w40768et(VS.80).aspx</a>  </p>
<p>The problem you&#8217;re having is that you are not actually passing the buffers <br />of the CStrings to the scanf function. Take a look at the GetBuffer() <br />function in CString. &nbsp;Also, this function might be useful to you:
<p><a href="http://msdn2.microsoft.com/en-us/library/k4ftfkd2(VS.80).aspx">http://msdn2.microsoft.com/en-us/library/k4ftfkd2(VS.80).aspx</a>
<p>In additional, You can cast a CString to type LPCTSTR to get read-only access to the contents of the string. &nbsp;However, <br />(1) &nbsp;You can&#8217;t cast a CString to type LPTSTR to get writeable access. &nbsp;You have to do something like call GetBufferSetLength and make sure you set the buffer large enough. <br />(2) &nbsp;In variable parameters to sscanf, you can&#8217;t expect any casting to be done automatically, you have to code the casts.
<p>But if you take enouth care of your code, sscanf may be not necessarily dangerous. You should check the length of the string and make sure you&#8217;ve allocated large enough sizes for your other variables then you won&#8217;t have buffer overruns.  </p>
<p>At last, in MFC, the following code may be the general solution:</p>
<p><coolcode lang="cpp" linenum="no"><br />
CString strUserName;<br />
CString strPassword;<br />
m_recBuff.Trim();  // remove leading and trailing space<br />
int n = m_recvBuff.Find(_T(&#8221; &#8220;));<br />
if(n>0) /* has space */<br />
{<br />
    strUserName = m_recvBuff.Left(n);<br />
    strPassword = m_recvBuff.Mid(n + 1);<br />
    strPassword.Trim();<br />
 }<br />
</coolcode></p>
<p>Unlike sscanf, which should no longer be considered a viable mechanism for any purpose dealing with string outputs, this actually works and is safe. &nbsp;It will not cause a buffer overrun. &nbsp;You should make it a practice to avoid mechanisms that can cause buffer overrun.
<p>The next paragraph&nbsp;is&nbsp;writen by <strong>Joseph M. Newcomer</strong>, as reference.</p>
<p><em>There is no economy in using obsolete and unsafe mechanisms. &nbsp;sscanf is overused and I consider it one of the design errors of the C language. &nbsp;I have never yet found a situation in which I would trust it to do the right thing in the presence of bad input, and the best I can say for it is that it is only suitable for use in the most naively trusting situations possible. &nbsp;I never use it, and I have never used it since I first learned of it. &nbsp;I had, as a beginner, used and trusted such mechanisms in languages I learned in the 1960s, and I quickly learned that such mechanisms are simply not to be trusted. &nbsp;I doubt if I have used such a mechanism since about 1967 or so. &nbsp;It was a bad idea then and it remains a bad idea today. &nbsp;The passage of 40 years does not make a bad idea good. </p>
<p></em></p>

	Tags: <strong><a href="http://www.ucosoft.com/tag/cstring" title="CString" rel="tag">CString</a>, <a href="http://www.ucosoft.com/tag/sscanf" title="sscanf" rel="tag">sscanf</a>, <a href="http://www.ucosoft.com/tag/win32mfc" title="Win32/MFC" rel="tag">Win32/MFC</a></strong><br />
]]></content:encoded>
			<wfw:commentRss>http://www.ucosoft.com/sscanf-with-cstring.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>How to Draw Bitmap from a bmp File?</title>
		<link>http://www.ucosoft.com/how-to-draw-bitmap-from-a-bmp-file.html</link>
		<comments>http://www.ucosoft.com/how-to-draw-bitmap-from-a-bmp-file.html#comments</comments>
		<pubDate>Fri, 05 Jan 2007 07:06:52 +0000</pubDate>
		<dc:creator>support</dc:creator>
				<category><![CDATA[Win32/MFC]]></category>
		<category><![CDATA[Bitmap]]></category>
		<category><![CDATA[Cpp]]></category>
		<category><![CDATA[How-to]]></category>

		<guid isPermaLink="false">http://www.ucosoft.com/archives/72.html</guid>
		<description><![CDATA[How to load a bitmap from bmp file and draw it in DC? There are two main steps: Load the bitmap from the file to a memory DC. Paint the content of the memory DC to the one which display the bitmap. In MFC, there is no API to load a bitmap directly from a [...]]]></description>
			<content:encoded><![CDATA[<p>How to load a bitmap from bmp file and draw it in DC?</p>
<p>There are two main steps:</p>
<ul>
<li>Load the bitmap from the file to a memory DC.</li>
<li>Paint the content of the memory DC to the one which display the bitmap.</li>
</ul>
<p>In MFC, there is no API to load a bitmap directly from a file, but you can use a Win32 API: <strong>LoadImage</strong>.<br />
Here is the delaration of LoadImage.</p>
<p><coolcode lang="cpp" linenum="no"><br />
HANDLE LoadImage(<br />
HINSTANCE hinst, //handle to instance, if load from file, it should be NULL<br />
LPCTSTR lpszName, //image resource name or the image file path<br />
UINT uType, //image type<br />
//1. IMAGE_BITMAP<br />
//2. IMAGE_CURSOR<br />
//3. IMAGE_ICON<br />
int cxDesired, //desired width<br />
int cyDesired, //desired height<br />
UINT fuLoad //load options<br />
);<br />
</coolcode></p>
<p>After this,&nbsp; the following steps are similar with loading a bitmap from resource. Here is a short example.</p>
<p><span id="more-72"></span><br />
<coolcode lang="cpp" linenum="no" download="ShowBmp_demo.cpp"><br />
CClientDC dc(this);<br />
CDC *mdc=new CDC;<br />
mdc->CreateCompatibleDC(&#038;dc);</p>
<p>CBitmap bitmap;<br />
//CBitmap is devived from CGdiObject<br />
//It has a handle:m_hObject, which can obtained with LoadImage<br />
bitmap.m_hObject=(HBITMAP)::LoadImage(<br />
   NULL,<br />
   &#8220;b1.bmp&#8221;,<br />
   IMAGE_BITMAP,<br />
   500,<br />
   400,<br />
   LR_LOADFROMFILE);</p>
<p>mdc->SelectObject(bitmap);<br />
CRect rect;<br />
GetClientRect(&#038;rect);<br />
//BitBlt()<br />
dc.BitBlt(0,0,rect.right,rect.bottom,mdc,0,0,SRCCOPY);</p>
<p>//release mdc<br />
delete mdc;<br />
</coolcode></p>

	Tags: <strong><a href="http://www.ucosoft.com/tag/bitmap" title="Bitmap" rel="tag">Bitmap</a>, <a href="http://www.ucosoft.com/tag/cpp" title="Cpp" rel="tag">Cpp</a>, <a href="http://www.ucosoft.com/tag/how-to" title="How-to" rel="tag">How-to</a>, <a href="http://www.ucosoft.com/tag/win32mfc" title="Win32/MFC" rel="tag">Win32/MFC</a></strong><br />
]]></content:encoded>
			<wfw:commentRss>http://www.ucosoft.com/how-to-draw-bitmap-from-a-bmp-file.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple code to create an EMF or Bitmap file from existed draw code</title>
		<link>http://www.ucosoft.com/simple-code-to-create-an-emf-or-bitmap-file-from-existed-draw-code.html</link>
		<comments>http://www.ucosoft.com/simple-code-to-create-an-emf-or-bitmap-file-from-existed-draw-code.html#comments</comments>
		<pubDate>Sun, 17 Dec 2006 02:04:30 +0000</pubDate>
		<dc:creator>hamo</dc:creator>
				<category><![CDATA[Win32/MFC]]></category>
		<category><![CDATA[Bitmap]]></category>
		<category><![CDATA[emf]]></category>

		<guid isPermaLink="false">http://www.ucosoft.com/archives/58.html</guid>
		<description><![CDATA[We often need to create an Enhanced Meta File(EMF) or Bitmap(bmp) from an existed DC draw code. It&#8217;s not very difficult but it&#8217;s boring to code again and again. The following is a simple solution, what you need to do is only to implement an IPicDrawer and call DrawEnhMeta or DrawBitmap function. struct IPicDrawer { [...]]]></description>
			<content:encoded><![CDATA[<p>We often need to create an Enhanced Meta File(EMF) or Bitmap(bmp) from an existed DC draw code. It&#8217;s not very difficult but it&#8217;s boring to code again and again.<br />
The following is a simple solution, what you need to do is only to implement an <strong>IPicDrawer</strong> and call <strong>DrawEnhMeta</strong> or <strong> DrawBitmap</strong> function.</p>
<p><span id="more-58"></span><br />
<!--adsense--><br />
<coolcode lang="cpp" linenum="no" download="demo_src.cpp"><br />
struct IPicDrawer<br />
{<br />
  virtual void GetSize(CSize&#038;) = 0; // It&#8217;s the smallest size for EMF<br />
  virtual BOOL Draw(CDC*) = 0;<br />
};</p>
<p>BOOL DrawEnhMeta(LPCTSTR lpszFileName, IPicDrawer *pIDrawer)<br />
{<br />
  // Draw EMF file, the limitation of DC function refer to MSDN<br />
  HDC hDC = ::CreateEnhMetaFile(NULL, lpszFileName, NULL,<br />
      _T(&#8220;FQ EMF Func\0No Title\0&#8243;));<br />
  CDC *pDC = CDC::FromHandle(hDC);<br />
  if(pDC)<br />
  {<br />
    CSize sz;<br />
    pIDrawer->GetSize(sz);<br />
    pDC->FillRect(CRect(0,0,sz.cx,sz.cy), &#038;CBrush(RGB(255,255,255)));<br />
    if(pIDrawer->Draw(pDC))<br />
    {<br />
      HENHMETAFILE hEmf = ::CloseEnhMetaFile(hDC);<br />
      if(hEmf)<br />
      {<br />
        ::DeleteEnhMetaFile(hEmf);<br />
        return TRUE;<br />
      }<br />
      else<br />
        ::DeleteFile(lpszFileName);<br />
    }<br />
  }<br />
  return FALSE;<br />
}</p>
<p>BOOL DrawBitmap(LPCTSTR lpszFileName, IPicDrawer *pIDrawer)<br />
{<br />
  // Draw 24 bit true color bitmap file<br />
  HANDLE hFile = ::CreateFile(lpszFileName, GENERIC_READ | GENERIC_WRITE,<br />
    0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);<br />
  BOOL bRet = FALSE;<br />
  if(hFile)<br />
  {<br />
    CSize sz;<br />
    pIDrawer->GetSize(sz);<br />
    BITMAPINFO BmpInfo = {0, }; //<br />
    BmpInfo.bmiHeader.biWidth = sz.cx;<br />
    BmpInfo.bmiHeader.biHeight = sz.cy;<br />
    BmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);<br />
    BmpInfo.bmiHeader.biPlanes = 1;<br />
    BmpInfo.bmiHeader.biBitCount = 24; // 24bit<br />
    BmpInfo.bmiHeader.biCompression = BI_RGB;<br />
    BmpInfo.bmiHeader.biSizeImage = BmpInfo.bmiHeader.biBitCount / 8 *<br />
      BmpInfo.bmiHeader.biWidth * BmpInfo.bmiHeader.biHeight;</p>
<p>    BITMAPFILEHEADER bmpFileHeader = {0, }; //<br />
    bmpFileHeader.bfType = *(WORD*)_T(&#8220;BM&#8221;);<br />
    bmpFileHeader.bfOffBits =<br />
      sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);<br />
    bmpFileHeader.bfOffBits +=<br />
      sizeof(DWORD) &#8211; (bmpFileHeader.bfOffBits % sizeof(DWORD));<br />
    bmpFileHeader.bfSize = bmpFileHeader.bfOffBits + BmpInfo.bmiHeader.biSizeImage;<br />
    //write file header and image information<br />
    DWORD dwNum = 0;<br />
    bRet = ::WriteFile(hFile, &#038;bmpFileHeader, sizeof(BITMAPFILEHEADER),<br />
      &#038;dwNum, NULL);<br />
    bRet = bRet &#038;&#038; ::WriteFile(hFile, &#038;(BmpInfo.bmiHeader), sizeof(BITMAPINFOHEADER),<br />
      &#038;dwNum, NULL);<br />
    if(bRet)<br />
    {<br />
      bRet = FALSE;<br />
      //create file mapping<br />
      HANDLE hMap = ::CreateFileMapping(hFile, NULL, PAGE_READWRITE,<br />
        0, bmpFileHeader.bfSize, NULL);<br />
      if(hMap)<br />
      {<br />
        // create bitmap object<br />
        void *pBits = NULL;<br />
        HBITMAP hBMP = ::CreateDIBSection(NULL, &#038;BmpInfo, DIB_RGB_COLORS,<br />
          &#038;pBits, hMap, bmpFileHeader.bfOffBits);<br />
        if(hBMP)<br />
        {<br />
          // draw the specified figure to file<br />
          CDC memTmpDC;<br />
          memTmpDC.CreateCompatibleDC(NULL);<br />
          if(memTmpDC.GetSafeHdc())<br />
          {<br />
            CBitmap *pbmp;<br />
            pbmp = CBitmap::FromHandle(hBMP);<br />
            if(pbmp)<br />
            {<br />
              memTmpDC.SelectObject(pbmp);<br />
              memTmpDC.FillRect(CRect(0,0,sz.cx,sz.cy), &#038;CBrush(RGB(255,255,255)));<br />
              bRet = pIDrawer->Draw(&#038;memTmpDC);<br />
            }<br />
          }<br />
          ::DeleteObject(hBMP);<br />
        }<br />
        ::CloseHandle(hMap);<br />
      }<br />
    }<br />
    ::CloseHandle(hFile);<br />
  }<br />
  if(!bRet)<br />
    ::DeleteFile(lpszFileName);<br />
  return bRet;<br />
}<br />
</coolcode></p>

	Tags: <strong><a href="http://www.ucosoft.com/tag/bitmap" title="Bitmap" rel="tag">Bitmap</a>, <a href="http://www.ucosoft.com/tag/emf" title="emf" rel="tag">emf</a>, <a href="http://www.ucosoft.com/tag/win32mfc" title="Win32/MFC" rel="tag">Win32/MFC</a></strong><br />
]]></content:encoded>
			<wfw:commentRss>http://www.ucosoft.com/simple-code-to-create-an-emf-or-bitmap-file-from-existed-draw-code.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to change the row height of CListCtrl</title>
		<link>http://www.ucosoft.com/how-to-change-the-row-height-of-clistctrl.html</link>
		<comments>http://www.ucosoft.com/how-to-change-the-row-height-of-clistctrl.html#comments</comments>
		<pubDate>Wed, 06 Dec 2006 02:34:32 +0000</pubDate>
		<dc:creator>hamo</dc:creator>
				<category><![CDATA[Win32/MFC]]></category>
		<category><![CDATA[CListCtrl]]></category>
		<category><![CDATA[Cpp]]></category>

		<guid isPermaLink="false">http://www.ucosoft.com/archives/54.html</guid>
		<description><![CDATA[When you&#160;want to change the height of CListCtrl, you need to use an owner-draw version of CListCtrl and change the font of the control. But when&#160;you change the font of&#160;the CListCtrl, the control or its parent window does not get a chance to respecify the height of the rows and the effect will not be [...]]]></description>
			<content:encoded><![CDATA[<p>When you&nbsp;want to change the height of CListCtrl, you need to use an owner-draw version of CListCtrl and change the font of the control. But when&nbsp;you change the font of&nbsp;the CListCtrl, the control or its parent window does not get a chance to respecify the height of the rows and the effect will not be taken. The reason is that No WM_MEASUREITEM message is sent to the controls parent window. </p>
<p>The following article from <a href="http://www.codeproject.com" rel="nofollow">CodeProject</a>&nbsp;provided a good solution.</p>
<p><!--adsensex-->
<ul>
<li><a href="http://www.codeproject.com/listctrl/changerowheight.asp">Changing Row Height in an owner drawn Control</a></li>
</ul>
<p>And there is a similar&nbsp;article on CodeGuru.</p>
<ul>
<li><a href="http://www.codeguru.com/Cpp/controls/listview/advanced/article.php/c1013" rel="nofollow">Changing row height in owner drawn control</a></li>
</ul>

	Tags: <strong><a href="http://www.ucosoft.com/tag/clistctrl" title="CListCtrl" rel="tag">CListCtrl</a>, <a href="http://www.ucosoft.com/tag/cpp" title="Cpp" rel="tag">Cpp</a>, <a href="http://www.ucosoft.com/tag/win32mfc" title="Win32/MFC" rel="tag">Win32/MFC</a></strong><br />
]]></content:encoded>
			<wfw:commentRss>http://www.ucosoft.com/how-to-change-the-row-height-of-clistctrl.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Moving selection of CListCtrl</title>
		<link>http://www.ucosoft.com/moving-selection-of-clistctrl.html</link>
		<comments>http://www.ucosoft.com/moving-selection-of-clistctrl.html#comments</comments>
		<pubDate>Mon, 04 Dec 2006 00:00:00 +0000</pubDate>
		<dc:creator>hamo</dc:creator>
				<category><![CDATA[Win32/MFC]]></category>
		<category><![CDATA[CListCtrl]]></category>

		<guid isPermaLink="false">http://www.ucosoft.com/archives/52.html</guid>
		<description><![CDATA[tag: CListCtrl, SetItemState, SetSelectionMark When you try to move the item selection of a CListCtrl object, you should remember to call SetSelectionMark. If not, the program will not work as you think. It will only work once and won&#8217;t work any longer. The reason is that even you have called SetItemState and the selection was [...]]]></description>
			<content:encoded><![CDATA[<p>tag: <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_mfc_clistctrl.asp" rel="nofollow">CListCtrl</a>, <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_mfc_clistctrl.3a3a.getitemstate.asp" rel="nofollow">SetItemState</a>, <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_mfc_clistctrl.3a3a.setselectionmark.asp" rel="nofollow">SetSelectionMark</a></p>
<p>When you try to move the item selection of a <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_mfc_clistctrl.asp">CListCtrl</a> object, you should remember to call <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_mfc_clistctrl.3a3a.setselectionmark.asp" rel="nofollow">SetSelectionMark</a>. If not, the program will not work as you think. It will only work once and won&#8217;t work any longer.</p>
<p>The reason is that even you have called <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_mfc_clistctrl.3a3a.getitemstate.asp" rel="nofollow">SetItemState</a> and the selection was changed in visiable, but the selection mark still keeps the old value.</p>
<p>The solution is to call the <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_mfc_clistctrl.3a3a.setselectionmark.asp" rel="nofollow">SetSelectionMark</a> function after <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_mfc_clistctrl.3a3a.getitemstate.asp" rel="nofollow">SetItemState</a>.</p>
<p>Here is a short example.</p>
<p> <span id="more-52"></span><br />
<!--adsense--><br />
<coolcode lang="cpp" linenum="no"><br />
void CTestListDlg::OnBtnMoveNextItem()<br />
{<br />
int curSel = m_list.GetSelectionMark();<br />
int nCount = m_list.GetItemCount();<br />
m_list.SetItemState(curSel, 0, LVIS_SELECTED);<br />
curSel ++;<br />
if (curSel>= nCount) curSel=0;<br />
m_list.SetItemState(curSel, LVIS_SELECTED,LVIS_SELECTED);<br />
m_list.SetSelectionMark(curSel);<br />
m_list.SetFocus();<br />
}<br />
  </coolcode></p>

	Tags: <strong><a href="http://www.ucosoft.com/tag/clistctrl" title="CListCtrl" rel="tag">CListCtrl</a>, <a href="http://www.ucosoft.com/tag/win32mfc" title="Win32/MFC" rel="tag">Win32/MFC</a></strong><br />
]]></content:encoded>
			<wfw:commentRss>http://www.ucosoft.com/moving-selection-of-clistctrl.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Solution: Crash in Release build with self-defined message</title>
		<link>http://www.ucosoft.com/solution-crash-in-release-build-with-self-defined-message.html</link>
		<comments>http://www.ucosoft.com/solution-crash-in-release-build-with-self-defined-message.html#comments</comments>
		<pubDate>Tue, 28 Nov 2006 15:05:02 +0000</pubDate>
		<dc:creator>support</dc:creator>
				<category><![CDATA[Win32/MFC]]></category>
		<category><![CDATA[Debug]]></category>
		<category><![CDATA[message]]></category>
		<category><![CDATA[Release]]></category>

		<guid isPermaLink="false">http://www.ucosoft.com/archives/42.html</guid>
		<description><![CDATA[We often meet such case: I have an application with a modeless dialog. The dialog comes up perfectly in DEBUG build, But in RELEASE build, it crashed. Such problem often occures because of incorrect signature of message handle functions, that included into MFC message map for your CWnd based class.We should very carefully check signatures [...]]]></description>
			<content:encoded><![CDATA[<p>We often meet such case: I have an application with a modeless dialog. The dialog comes up perfectly in DEBUG build, But in RELEASE build, it crashed.</p>
<p><!--adsense--></p>
<p>Such problem often occures because of incorrect signature of message handle functions, that included into MFC message map for your CWnd based class.<br />We should very carefully check signatures such functions in comparision with expected one by MFC library. For example, if you put follow handler into message map</p>
<p><coolcode lang="cpp" linenum="no"><br />
ON_MESSAGE(WM_SOME_USER_MESSAGE, OnMyMessage)<br />
</coolcode></p>
<p>and define OnMyMessage as:</p>
<p><coolcode lang="cpp" linenum="no"><br />
void CMyWnd::OnMyMessage()<br />
{<br />
 //do something<br />
}<br />
</coolcode></p>
<p>this code will be compiled without errors. DEBUG build can work without any problems, but RELEASE will crash besause ON_MESSAGE macro expect follow<br />signature:</p>
<p><coolcode lang="cpp" linenum="no"><br />
LRESULT CMyWnd::OnMyMessage(WPARAM /*wParam*/, LPARAM /*lParam*/)</coolcode></p>
<p>DEBUG build works because stack processing has differences for debug and release builds.</p>
<p>so, the solution is simple:</p>
<p><span id="more-42"></span></p>
<p>Solution 1:&nbsp; modify the&nbsp; message handle functions with the following style and it will be ok.</p>
<p><coolcode lang="cpp" linenum="no"><br />
void CMyWnd::OnMyMessage(WPARAM wParam, LPARAM lParam)<br />
</coolcode></p>
<p>Solution 2:&nbsp; don&#8217;t modify the message handle functios, and change the <font color="#0000ff"><strong>ON_MESSAGE macro </strong><font color="#000000">to</font><strong> </strong></font><font color="#0000ff"><strong>ON_MESSAGE_VOID</strong>.</font><br />
<coolcode lang="cpp" linenum="no"><br />
ON_MESSAGE_VOID(WM_SOME_USER_MESSAGE, OnMyMessage)<br />
</coolcode><br />
<br />This macro is in AfxPriv.h, but is largely undocumented.</p>

	Tags: <strong><a href="http://www.ucosoft.com/tag/debug" title="Debug" rel="tag">Debug</a>, <a href="http://www.ucosoft.com/tag/message" title="message" rel="tag">message</a>, <a href="http://www.ucosoft.com/tag/release" title="Release" rel="tag">Release</a>, <a href="http://www.ucosoft.com/tag/win32mfc" title="Win32/MFC" rel="tag">Win32/MFC</a></strong><br />
]]></content:encoded>
			<wfw:commentRss>http://www.ucosoft.com/solution-crash-in-release-build-with-self-defined-message.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

