<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="FeedCreator 1.8" -->
<?xml-stylesheet href="http://wiki.nathancampos.me/lib/exe/css.php?s=feed" type="text/css"?>
<rdf:RDF
    xmlns="http://purl.org/rss/1.0/"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
    xmlns:dc="http://purl.org/dc/elements/1.1/">
    <channel rdf:about="http://wiki.nathancampos.me/feed.php">
        <title>Nathan&#039;s KB - snippets</title>
        <description>Hello</description>
        <link>http://wiki.nathancampos.me/</link>
        <image rdf:resource="http://wiki.nathancampos.me/lib/exe/fetch.php?media=wiki:dokuwiki.svg" />
       <dc:date>2026-05-13T09:02:38+00:00</dc:date>
        <items>
            <rdf:Seq>
                <rdf:li rdf:resource="http://wiki.nathancampos.me/doku.php?id=snippets:win32"/>
            </rdf:Seq>
        </items>
    </channel>
    <image rdf:about="http://wiki.nathancampos.me/lib/exe/fetch.php?media=wiki:dokuwiki.svg">
        <title>Nathan's KB</title>
        <link>http://wiki.nathancampos.me/</link>
        <url>http://wiki.nathancampos.me/lib/exe/fetch.php?media=wiki:dokuwiki.svg</url>
    </image>
    <item rdf:about="http://wiki.nathancampos.me/doku.php?id=snippets:win32">
        <dc:format>text/html</dc:format>
        <dc:date>2023-05-28T17:23:50+00:00</dc:date>
        <dc:creator>Anonymous (anonymous@undisclosed.example.com)</dc:creator>
        <title>win32</title>
        <link>http://wiki.nathancampos.me/doku.php?id=snippets:win32</link>
        <description>
&lt;h1 class=&quot;sectionedit1&quot; id=&quot;windows_programming_snippets&quot;&gt;Windows Programming Snippets&lt;/h1&gt;
&lt;div class=&quot;level1&quot;&gt;

&lt;p&gt;
This is a collection of useful Windows programming snippets we&amp;#039;ve used throughout the years on various projects. There are also some useful snippets in the &lt;a href=&quot;http://wiki.nathancampos.me/doku.php?id=devnotes:msvc-porting&quot; class=&quot;wikilink1&quot; title=&quot;devnotes:msvc-porting&quot; data-wiki-id=&quot;devnotes:msvc-porting&quot;&gt;Porting to Microsoft Visual C++&lt;/a&gt; page, although those will be related to cross-platform development.
&lt;/p&gt;

&lt;/div&gt;
&lt;!-- EDIT{&amp;quot;target&amp;quot;:&amp;quot;section&amp;quot;,&amp;quot;name&amp;quot;:&amp;quot;Windows Programming Snippets&amp;quot;,&amp;quot;hid&amp;quot;:&amp;quot;windows_programming_snippets&amp;quot;,&amp;quot;codeblockOffset&amp;quot;:0,&amp;quot;secid&amp;quot;:1,&amp;quot;range&amp;quot;:&amp;quot;1-327&amp;quot;} --&gt;
&lt;h2 class=&quot;sectionedit2&quot; id=&quot;windows_and_controls&quot;&gt;Windows and Controls&lt;/h2&gt;
&lt;div class=&quot;level2&quot;&gt;

&lt;p&gt;
Windows are everything in Windows, even controls, some of the Win32 &lt;abbr title=&quot;Application Programming Interface&quot;&gt;API&lt;/abbr&gt; functions to deal with this core component of the operating system are sometimes laughable, so we need to create our own wrappers to make them useful.
&lt;/p&gt;

&lt;/div&gt;
&lt;!-- EDIT{&amp;quot;target&amp;quot;:&amp;quot;section&amp;quot;,&amp;quot;name&amp;quot;:&amp;quot;Windows and Controls&amp;quot;,&amp;quot;hid&amp;quot;:&amp;quot;windows_and_controls&amp;quot;,&amp;quot;codeblockOffset&amp;quot;:0,&amp;quot;secid&amp;quot;:2,&amp;quot;range&amp;quot;:&amp;quot;328-585&amp;quot;} --&gt;
&lt;h3 class=&quot;sectionedit3&quot; id=&quot;getting_a_window_s_text&quot;&gt;Getting a Window&amp;#039;s Text&lt;/h3&gt;
&lt;div class=&quot;level3&quot;&gt;

&lt;p&gt;
Such a simple operation should&amp;#039;ve been so simple and straightforward, after all it&amp;#039;s something that you&amp;#039;ll be doing a lot in Windows, but the people that designed the &lt;abbr title=&quot;Application Programming Interface&quot;&gt;API&lt;/abbr&gt; didn&amp;#039;t think about including a little helper to make this task easier.
&lt;/p&gt;
&lt;pre class=&quot;code c&quot;&gt;&lt;span class=&quot;coMULTI&quot;&gt;/**
 * Gets a window&#039;s text property into a newly allocated buffer. No need for
 * pointer wrangling.
 *
 * @warning This function allocates memory that must be free&#039;d by you.
 * 
 * @param hWnd Handle of the window we want the text from.
 * 
 * @return Newly allocated buffer with the window&#039;s text.
 */&lt;/span&gt;
LPTSTR GetWindowTextAlloc&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;HWND hWnd&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
	&lt;span class=&quot;kw4&quot;&gt;int&lt;/span&gt; iLen&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
	LPTSTR szText&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
	&lt;span class=&quot;coMULTI&quot;&gt;/* Get the length of the text. */&lt;/span&gt;
	iLen &lt;span class=&quot;sy0&quot;&gt;=&lt;/span&gt; GetWindowTextLength&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;hWnd&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span class=&quot;sy0&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nu0&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
	&lt;span class=&quot;coMULTI&quot;&gt;/* Allocate the memory to receive the window&#039;s text. */&lt;/span&gt;
	szText &lt;span class=&quot;sy0&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;LPTSTR&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;a href=&quot;http://www.opengroup.org/onlinepubs/009695399/functions/malloc.html&quot;&gt;&lt;span class=&quot;kw3&quot;&gt;malloc&lt;/span&gt;&lt;/a&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;iLen &lt;span class=&quot;sy0&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;kw4&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;TCHAR&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;kw1&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;szText &lt;span class=&quot;sy0&quot;&gt;==&lt;/span&gt; NULL&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;
		&lt;span class=&quot;kw1&quot;&gt;return&lt;/span&gt; NULL&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
	&lt;span class=&quot;coMULTI&quot;&gt;/* Get the text from the window. */&lt;/span&gt;
	GetWindowText&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;hWnd&lt;span class=&quot;sy0&quot;&gt;,&lt;/span&gt; szText&lt;span class=&quot;sy0&quot;&gt;,&lt;/span&gt; iLen&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
	&lt;span class=&quot;kw1&quot;&gt;return&lt;/span&gt; szText&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;!-- EDIT{&amp;quot;target&amp;quot;:&amp;quot;section&amp;quot;,&amp;quot;name&amp;quot;:&amp;quot;Getting a Window&amp;#039;s Text&amp;quot;,&amp;quot;hid&amp;quot;:&amp;quot;getting_a_window_s_text&amp;quot;,&amp;quot;codeblockOffset&amp;quot;:0,&amp;quot;secid&amp;quot;:3,&amp;quot;range&amp;quot;:&amp;quot;586-1562&amp;quot;} --&gt;
&lt;h2 class=&quot;sectionedit4&quot; id=&quot;dialogs&quot;&gt;Dialogs&lt;/h2&gt;
&lt;div class=&quot;level2&quot;&gt;

&lt;p&gt;
Dialogs are almost the heart of every Windows application, so there are a lot of repetitive things that we usually want to do with them.
&lt;/p&gt;

&lt;/div&gt;
&lt;!-- EDIT{&amp;quot;target&amp;quot;:&amp;quot;section&amp;quot;,&amp;quot;name&amp;quot;:&amp;quot;Dialogs&amp;quot;,&amp;quot;hid&amp;quot;:&amp;quot;dialogs&amp;quot;,&amp;quot;codeblockOffset&amp;quot;:1,&amp;quot;secid&amp;quot;:4,&amp;quot;range&amp;quot;:&amp;quot;1563-1721&amp;quot;} --&gt;
&lt;h3 class=&quot;sectionedit5&quot; id=&quot;centering_on_a_parent_window&quot;&gt;Centering on a Parent Window&lt;/h3&gt;
&lt;div class=&quot;level3&quot;&gt;

&lt;p&gt;
Whenever we are opening a new dialog, commonly, we want it to open in the center of the dialog&amp;#039;s parent window. This is so common that &lt;a href=&quot;https://learn.microsoft.com/en-us/windows/win32/dlgbox/using-dialog-boxes#initializing-a-dialog-box&quot; class=&quot;urlextern&quot; title=&quot;https://learn.microsoft.com/en-us/windows/win32/dlgbox/using-dialog-boxes#initializing-a-dialog-box&quot; rel=&quot;ugc nofollow&quot;&gt;Microsoft has even documented it&lt;/a&gt;.
&lt;/p&gt;
&lt;pre class=&quot;code c&quot;&gt;HWND hwndOwner&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt; 
RECT rc&lt;span class=&quot;sy0&quot;&gt;,&lt;/span&gt; rcDlg&lt;span class=&quot;sy0&quot;&gt;,&lt;/span&gt; rcOwner&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt; 
&amp;nbsp;
....
&amp;nbsp;
&lt;span class=&quot;kw1&quot;&gt;case&lt;/span&gt; WM_INITDIALOG&lt;span class=&quot;sy0&quot;&gt;:&lt;/span&gt; 
&amp;nbsp;
    &lt;span class=&quot;co1&quot;&gt;// Get the owner window and dialog box rectangles. &lt;/span&gt;
    &lt;span class=&quot;kw1&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;hwndOwner &lt;span class=&quot;sy0&quot;&gt;=&lt;/span&gt; GetParent&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;hwndDlg&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span class=&quot;sy0&quot;&gt;==&lt;/span&gt; NULL&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt; 
    &lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
        hwndOwner &lt;span class=&quot;sy0&quot;&gt;=&lt;/span&gt; GetDesktopWindow&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt; 
    &lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
    GetWindowRect&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;hwndOwner&lt;span class=&quot;sy0&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sy0&quot;&gt;&amp;amp;&lt;/span&gt;rcOwner&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt; 
    GetWindowRect&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;hwndDlg&lt;span class=&quot;sy0&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sy0&quot;&gt;&amp;amp;&lt;/span&gt;rcDlg&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt; 
    CopyRect&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;&amp;amp;&lt;/span&gt;rc&lt;span class=&quot;sy0&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sy0&quot;&gt;&amp;amp;&lt;/span&gt;rcOwner&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt; 
&amp;nbsp;
    &lt;span class=&quot;co1&quot;&gt;// Offset the owner and dialog box rectangles so that right and bottom &lt;/span&gt;
    &lt;span class=&quot;co1&quot;&gt;// values represent the width and height, and then offset the owner again &lt;/span&gt;
    &lt;span class=&quot;co1&quot;&gt;// to discard space taken up by the dialog box. &lt;/span&gt;
    OffsetRect&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;&amp;amp;&lt;/span&gt;rcDlg&lt;span class=&quot;sy0&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sy0&quot;&gt;-&lt;/span&gt;rcDlg.&lt;span class=&quot;me1&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sy0&quot;&gt;-&lt;/span&gt;rcDlg.&lt;span class=&quot;me1&quot;&gt;top&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt; 
    OffsetRect&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;&amp;amp;&lt;/span&gt;rc&lt;span class=&quot;sy0&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sy0&quot;&gt;-&lt;/span&gt;rc.&lt;span class=&quot;me1&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sy0&quot;&gt;-&lt;/span&gt;rc.&lt;span class=&quot;me1&quot;&gt;top&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt; 
    OffsetRect&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;&amp;amp;&lt;/span&gt;rc&lt;span class=&quot;sy0&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sy0&quot;&gt;-&lt;/span&gt;rcDlg.&lt;span class=&quot;me1&quot;&gt;right&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sy0&quot;&gt;-&lt;/span&gt;rcDlg.&lt;span class=&quot;me1&quot;&gt;bottom&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt; 
&amp;nbsp;
    &lt;span class=&quot;co1&quot;&gt;// The new position is the sum of half the remaining space and the owner&#039;s &lt;/span&gt;
    &lt;span class=&quot;co1&quot;&gt;// original position. &lt;/span&gt;
    SetWindowPos&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;hwndDlg&lt;span class=&quot;sy0&quot;&gt;,&lt;/span&gt; 
                 HWND_TOP&lt;span class=&quot;sy0&quot;&gt;,&lt;/span&gt; 
                 rcOwner.&lt;span class=&quot;me1&quot;&gt;left&lt;/span&gt; &lt;span class=&quot;sy0&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;rc.&lt;span class=&quot;me1&quot;&gt;right&lt;/span&gt; &lt;span class=&quot;sy0&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;nu0&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;,&lt;/span&gt; 
                 rcOwner.&lt;span class=&quot;me1&quot;&gt;top&lt;/span&gt; &lt;span class=&quot;sy0&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;rc.&lt;span class=&quot;me1&quot;&gt;bottom&lt;/span&gt; &lt;span class=&quot;sy0&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;nu0&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;,&lt;/span&gt; 
                 &lt;span class=&quot;nu0&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nu0&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;,&lt;/span&gt;          &lt;span class=&quot;co1&quot;&gt;// Ignores size arguments. &lt;/span&gt;
                 SWP_NOSIZE&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt; 
&amp;nbsp;
    &lt;span class=&quot;kw1&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;GetDlgCtrlID&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;HWND&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt; wParam&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span class=&quot;sy0&quot;&gt;!=&lt;/span&gt; ID_ITEMNAME&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt; 
    &lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt; 
        SetFocus&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;GetDlgItem&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;hwndDlg&lt;span class=&quot;sy0&quot;&gt;,&lt;/span&gt; ID_ITEMNAME&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt; 
        &lt;span class=&quot;kw1&quot;&gt;return&lt;/span&gt; FALSE&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt; 
    &lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt; 
    &lt;span class=&quot;kw1&quot;&gt;return&lt;/span&gt; TRUE&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;!-- EDIT{&amp;quot;target&amp;quot;:&amp;quot;section&amp;quot;,&amp;quot;name&amp;quot;:&amp;quot;Centering on a Parent Window&amp;quot;,&amp;quot;hid&amp;quot;:&amp;quot;centering_on_a_parent_window&amp;quot;,&amp;quot;codeblockOffset&amp;quot;:1,&amp;quot;secid&amp;quot;:5,&amp;quot;range&amp;quot;:&amp;quot;1722-&amp;quot;} --&gt;</description>
    </item>
</rdf:RDF>
