<p>I'm trying to create a dialog box using C++ and the windows API, but I don't want the dialog defined in a resource file. I can't find anything good on this on the web, and none of the examples I've read seem to define the dialog programmatically.</p>

<p>How can I do this?</p>

<p>A simple example is fine. I'm not doing anything complicated with it yet.</p>

[{'answer_id': 61652, 'author': 'Alan', 'author_id': 2958, 'author_profile': 'https://Stackoverflow.com/users/2958', 'pm_score': 2, 'selected': False, 'text': '<p>Take a look at <a href="http://www.codeproject.com/KB/wtl/resourcelessdialogtoolkit.aspx" rel="nofollow noreferrer">this toolkit</a> that describes how to create dialogs without resource files.</p>\n\n<p>It\'s in WTL. However, I\'m sure you can pick apart the internals to achieve the same thing using the Win32 API directly.</p>\n'}, {'answer_id': 61671, 'author': 'Mladen Janković', 'author_id': 6300, 'author_profile': 'https://Stackoverflow.com/users/6300', 'pm_score': 4, 'selected': False, 'text': '<p>Raymond Chen wrote a few posts about the dialog manager:</p>\n\n<ul>\n<li><a href="https://devblogs.microsoft.com/oldnewthing/20050329-00/?p=36043" rel="nofollow noreferrer">The dialog manager, part 1: Warm-ups</a></li>\n<li><a href="https://devblogs.microsoft.com/oldnewthing/20050330-00/?p=36023" rel="nofollow noreferrer">The dialog manager, part 2: Creating the frame window</a></li>\n<li><a href="https://devblogs.microsoft.com/oldnewthing/20050331-00/?p=36003" rel="nofollow noreferrer">The dialog manager, part 3: Creating the controls</a></li>\n<li><a href="https://devblogs.microsoft.com/oldnewthing/20050401-00/?p=35993" rel="nofollow noreferrer">The dialog manager, part 4: The dialog loop</a></li>\n<li><a href="https://devblogs.microsoft.com/oldnewthing/20050404-48/?p=35983" rel="nofollow noreferrer">The dialog manager, part 5: Converting a non-modal dialog box to modal</a></li>\n<li><a href="https://devblogs.microsoft.com/oldnewthing/20050405-46/?p=35973" rel="nofollow noreferrer">The dialog manager, part 6: Subtleties in message loops</a></li>\n<li><a href="https://devblogs.microsoft.com/oldnewthing/20050406-57/?p=35963" rel="nofollow noreferrer">The dialog manager, part 7: More subtleties in message loops</a></li>\n<li><a href="https://devblogs.microsoft.com/oldnewthing/20050407-00/?p=35953" rel="nofollow noreferrer">The dialog manager, part 8: Custom navigation in dialog boxes</a></li>\n<li><a href="https://devblogs.microsoft.com/oldnewthing/20050408-41/?p=35943" rel="nofollow noreferrer">The dialog manager, part 9: Custom accelerators in dialog boxes</a></li>\n</ul>\n'}, {'answer_id': 62851, 'author': 'realsugar', 'author_id': 6885, 'author_profile': 'https://Stackoverflow.com/users/6885', 'pm_score': 2, 'selected': False, 'text': '<p>Try to search <a href="http://msdn.microsoft.com" rel="nofollow noreferrer">MSDN</a> for "dialog templates in memory".</p>\n\n<p>See this for example: <em><a href="https://learn.microsoft.com/en-us/windows/win32/dlgbox/dialog-boxes" rel="nofollow noreferrer">Dialog Boxes</a></em></p>\n'}, {'answer_id': 1251680, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>Here you can find how to use Windows API dialogs without using resource files.</p>\n\n<p>The Windows API (only the C Win32 API, no <a href="http://en.wikipedia.org/wiki/Microsoft_Foundation_Class_Library" rel="nofollow noreferrer">MFC</a>) tutorial: </p>\n\n<p><em><a href="http://zetcode.com/gui/winapi/" rel="nofollow noreferrer">Windows API tutorial</a></em></p>\n'}, {'answer_id': 48009861, 'author': 'jrh', 'author_id': 4975230, 'author_profile': 'https://Stackoverflow.com/users/4975230', 'pm_score': 3, 'selected': False, 'text': '<p>If all you want to do is show a window with controls, it\'s possible to create a window without using resource (.rc) files / scripts.</p>\n<p>This isn\'t the same as a dialog, but it might be easier than creating a dialog programmatically.</p>\n<h2>First, a few notes about how this is done:</h2>\n<ul>\n<li><p>Instead of designing the dialog in the rc file, you could manually use <code>CreateWindow</code> (or <code>CreateWindowEx</code>) to create child windows of a main window. (for .NET <a href="http://en.wikipedia.org/wiki/Windows_Forms" rel="nofollow noreferrer">Windows Forms</a> programmers, these windows are like <code>Control</code>s).</p>\n</li>\n<li><p>This process will not be graphical at all (you will need to manually type in the location and size of each window), but I think this can be a great way to understand how dialogs are created under the hood.</p>\n</li>\n<li><p>There are some disadvantages to not using a real dialog, namely that tab will not work when switching between controls.</p>\n</li>\n</ul>\n<hr />\n<h2>About the example:</h2>\n<ul>\n<li>This example features a dialog box with two buttons, an edit box (.NET Windows Forms programmers would think of it as a <code>TextBox</code>), and a check box.</li>\n</ul>\n<p><a href="https://i.stack.imgur.com/lb3fh.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lb3fh.png" alt="Screenshot of window" /></a></p>\n<p>It has been tested under the following conditions:</p>\n<ul>\n<li>x86 build</li>\n<li>x64 build</li>\n<li>Unicode build (<code>UNICODE</code> and <code>_UNICODE</code> defined)</li>\n<li>Non-Unicode build (<code>UNICODE</code> and <code>_UNICODE</code> not defined)</li>\n<li>Built with Visual Studio\'s C compiler</li>\n<li>Built with Visual Studio\'s C++ compiler</li>\n<li>OS: Windows 10 64 bit</li>\n</ul>\n<h3>Note: UNICODE</h3>\n<ul>\n<li>As of the time of writing, UTF-8 is still in beta for Windows 10\n<ul>\n<li>If you have not enabled this setting, you should assume that any <code>char*</code> is ACP, not UTF-8, this applies to standard library functions too</li>\n<li>Even though in Linux, that same standard library function would be UTF-8.</li>\n<li>Sadly, some C++ standard library features only work with <code>char*</code> (e.g., exception messages).</li>\n<li>You can still use UTF-8 in Windows without the option set, you will just have to encode it back to UTF-16 before calling winapi functions.</li>\n<li>Here is a <a href="https://www.reddit.com/r/cpp/comments/g7tkd7/state_of_utf8_support_on_windows/" rel="nofollow noreferrer">reddit thread</a> with a reply from somebody who claims to have worked on UTF-8 on Windows, it has some good information.</li>\n</ul>\n</li>\n<li>UNICODE in Windows means &quot;UTF-16&quot;, not &quot;UTF-8&quot;.</li>\n<li>Using Unicode of some kind is <strong>strongly recommended</strong> for any version of Windows that is not <a href="https://en.wikipedia.org/wiki/Unicode_in_Microsoft_Windows" rel="nofollow noreferrer">very old</a>.\n<ul>\n<li>Be aware that if you don\'t use Unicode, your program may be utterly unable to open file names containing Unicode characters, handle directories (e.g., usernames) with non-ACP characters, etc.</li>\n<li>Using ACP functions (<code>SendMessageA</code>,etc) without somehow verifying that UTF-8 is enabled (it\'s disabled by default) is probably a bug.</li>\n<li>For max portability/flexibility, I would recommend using UTF-16 and the W version of all API functions, <a href="https://learn.microsoft.com/en-us/windows/win32/api/stringapiset/nf-stringapiset-multibytetowidechar" rel="nofollow noreferrer">translating from UTF-8 to UTF-16 at the last minute</a>. Read this page very carefully.</li>\n</ul>\n</li>\n</ul>\n<hr />\n<h2>Now for the code:</h2>\n<p>Note that a large amount of comments have been added to try to document the windows functions, I recommend copy/pasting this into a text editor, for best results.</p>\n<pre><code>// This sample will work either with or without UNICODE, it looks like\n// it\'s recommended now to use UNICODE for all new code, but I left\n// the ANSI option in there just to get the absolute maximum amount\n// of compatibility.\n//\n// Note that UNICODE and _UNICODE go together, unfortunately part\n// of the Windows API uses _UNICODE, and part of it uses UNICODE.\n//\n// tchar.h, for example, makes heavy use of _UNICODE, and windows.h\n// makes heavy use of UNICODE.\n#define UNICODE\n#define _UNICODE\n//#undef UNICODE\n//#undef _UNICODE\n\n#include &lt;windows.h&gt;\n#include &lt;tchar.h&gt;\n\n// I made this struct to more conveniently store the\n// positions / size of each window in the dialog\ntypedef struct SizeAndPos_s\n{\n    int x, y, width, height;\n} SizeAndPos_t;\n\n// Typically these would be #defines, but there\n// is no reason to not make them constants\nconst WORD ID_btnHELLO = 1;\nconst WORD ID_btnQUIT = 2;\nconst WORD ID_CheckBox = 3;\nconst WORD ID_txtEdit = 4;\nconst WORD ID_btnShow = 5;\n\n//                                    x,      y,      width,  height\nconst SizeAndPos_t mainWindow   =   { 150,    150,    300,    300 };\n\nconst SizeAndPos_t btnHello     =   { 20,     50,     80,     25 };\nconst SizeAndPos_t btnQuit      =   { 120,    50,     80,     25 };\nconst SizeAndPos_t chkCheck     =   { 20,     90,     185,    35 };\n\nconst SizeAndPos_t txtEdit      =   { 20,     150,    150,    20 };\nconst SizeAndPos_t btnShow      =   { 180,    150,    80,     25 };\n\nHWND txtEditHandle = NULL;\n\n// hwnd:    All window processes are passed the handle of the window\n//         that they belong to in hwnd.\n// msg:     Current message (e.g., WM_*) from the OS.\n// wParam:  First message parameter, note that these are more or less\n//          integers, but they are really just &quot;data chunks&quot; that\n//          you are expected to memcpy as raw data to float, etc.\n// lParam:  Second message parameter, same deal as above.\nLRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)\n{\n\n    switch (msg)\n    {\n\n    case WM_CREATE:\n        // Create the buttons\n        //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n\n        // Note that the &quot;parent window&quot; is the dialog itself. Since we are\n        // in the dialog\'s WndProc, the dialog\'s handle is passed into hwnd.\n        //\n        //CreateWindow( lpClassName,        lpWindowName,       dwStyle,                                x,          y,          nWidth,         nHeight,            hWndParent,     hMenu,              hInstance,      lpParam\n        //CreateWindow( windowClassName,    initial text,       style (flags),                          xPos,       yPos,       width,          height,             parentHandle,   menuHandle,         instanceHandle, param);\n        CreateWindow(   TEXT(&quot;Button&quot;),     TEXT(&quot;Hello&quot;),      WS_VISIBLE | WS_CHILD,                  btnHello.x, btnHello.y, btnHello.width, btnHello.height,    hwnd,           (HMENU)ID_btnHELLO, NULL,           NULL);\n\n        CreateWindow(   TEXT(&quot;Button&quot;),     TEXT(&quot;Quit&quot;),       WS_VISIBLE | WS_CHILD,                  btnQuit.x,  btnQuit.y,  btnQuit.width,  btnQuit.height,     hwnd,           (HMENU)ID_btnQUIT,  NULL,           NULL);\n\n        // Create a checkbox\n        //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n        CreateWindow(  TEXT(&quot;button&quot;),      TEXT(&quot;CheckBox&quot;),   WS_VISIBLE | WS_CHILD | BS_CHECKBOX,    chkCheck.x, chkCheck.y, chkCheck.width, chkCheck.height,    hwnd,           (HMENU)ID_CheckBox, NULL,           NULL);\n\n        // Create an edit box (single line text editing), and a button to show the text\n        //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n        //Handle        = CreateWindow(windowClassName,    windowName,           style,                              xPos,       yPos,       width,          height,             parentHandle,   menuHandle,         instanceHandle, param);\n        txtEditHandle   = CreateWindow(TEXT(&quot;Edit&quot;),       TEXT(&quot;Initial Text&quot;), WS_CHILD | WS_VISIBLE | WS_BORDER,  txtEdit.x,  txtEdit.y,  txtEdit.width,  txtEdit.height,     hwnd,           (HMENU)ID_txtEdit,  NULL,           NULL);\n\n        //CreateWindow( windowClassName,    windowName,         style,                                  xPos,      yPos,      width,          height,           parentHandle,   menuHandle,         instanceHandle, param);\n        CreateWindow(   TEXT(&quot;Button&quot;),     TEXT(&quot;Show&quot;),       WS_VISIBLE | WS_CHILD,                  btnShow.x, btnShow.y, btnShow.width, btnShow.height,    hwnd,           (HMENU)ID_btnShow,  NULL,           NULL);\n\n        // Create an Updown control. Note that this control will allow you to type in non-number characters, but it will not affect the state of the control\n\n        break;\n\n    // For more information about WM_COMMAND, see\n    // https://msdn.microsoft.com/en-us/library/windows/desktop/ms647591(v=vs.85).aspx\n    case WM_COMMAND:\n\n        // The LOWORD of wParam identifies which control sent\n        // the WM_COMMAND message. The WM_COMMAND message is\n        // sent when the button has been clicked.\n        if (LOWORD(wParam) == ID_btnHELLO)\n        {\n            MessageBox(hwnd, TEXT(&quot;Hello!&quot;), TEXT(&quot;Hello&quot;), MB_OK);\n        }\n        else if (LOWORD(wParam) == ID_btnQUIT)\n        {\n            PostQuitMessage(0);\n        }\n        else if (LOWORD(wParam) == ID_CheckBox)\n        {\n            UINT checked = IsDlgButtonChecked(hwnd, ID_CheckBox);\n\n            if (checked)\n            {\n                CheckDlgButton(hwnd, ID_CheckBox, BST_UNCHECKED);\n                MessageBox(hwnd, TEXT(&quot;The checkbox has been unchecked.&quot;), TEXT(&quot;CheckBox Event&quot;), MB_OK);\n            }\n            else\n            {\n                CheckDlgButton(hwnd, ID_CheckBox, BST_CHECKED);\n                MessageBox(hwnd, TEXT(&quot;The checkbox has been checked.&quot;), TEXT(&quot;CheckBox Event&quot;), MB_OK);\n            }\n        }\n        else if (LOWORD(wParam) == ID_btnShow)\n        {\n               int textLength_WithNUL = GetWindowTextLength(txtEditHandle) + 1;\n               // WARNING: If you are compiling this for C, please remember to remove the (TCHAR*) cast.\n               TCHAR* textBoxText = (TCHAR*) malloc(sizeof(TCHAR) * textLength_WithNUL);\n\n               GetWindowText(txtEditHandle, textBoxText, textLength_WithNUL);\n\n               MessageBox(hwnd, textBoxText, TEXT(&quot;Here\'s what you typed&quot;), MB_OK);\n\n               free(textBoxText);\n        }\n        break;\n\n    case WM_DESTROY:\n\n        PostQuitMessage(0);\n        break;\n    }\n\n    return DefWindowProc(hwnd, msg, wParam, lParam);\n}\n\n\n// hInstance: This handle refers to the running executable\n// hPrevInstance: Not used. See https://blogs.msdn.microsoft.com/oldnewthing/20040615-00/?p=38873\n// lpCmdLine: Command line arguments.\n// nCmdShow: a flag that says whether the main application window\n//           will be minimized, maximized, or shown normally.\n//\n// Note that it\'s necessary to use _tWinMain to make it\n// so that command line arguments will work, both\n// with and without UNICODE / _UNICODE defined.\nint APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)\n{\n    MSG  msg;\n    WNDCLASS mainWindowClass = { 0 };\n\n    // You can set the main window name to anything, but\n    // typically you should prefix custom window classes\n    // with something that makes it unique.\n    mainWindowClass.lpszClassName = TEXT(&quot;JRH.MainWindow&quot;);\n\n    mainWindowClass.hInstance = hInstance;\n    mainWindowClass.hbrBackground = GetSysColorBrush(COLOR_3DFACE);\n    mainWindowClass.lpfnWndProc = WndProc;\n    mainWindowClass.hCursor = LoadCursor(0, IDC_ARROW);\n\n    RegisterClass(&amp;mainWindowClass);\n\n    // Notes:\n    // - The classname identifies the TYPE of the window. Not a C type.\n    //   This is a (TCHAR*) ID that Windows uses internally.\n    // - The window name is really just the window text, this is\n    //   commonly used for captions, including the title\n    //   bar of the window itself.\n    // - parentHandle is considered the &quot;owner&quot; of this\n    //   window. MessageBoxes can use HWND_MESSAGE to\n    //   free them of any window.\n    // - menuHandle: hMenu specifies the child-window identifier,\n    //               an integer value used by a dialog box\n    //               control to notify its parent about events.\n    //               The application determines the child-window\n    //               identifier; it must be unique for all\n    //               child windows with the same parent window.\n\n    //CreateWindow( windowClassName,                windowName,             style,                            xPos,         yPos,       width,              height,            parentHandle,   menuHandle,  instanceHandle, param);\n    CreateWindow(   mainWindowClass.lpszClassName,  TEXT(&quot;Main Window&quot;),    WS_OVERLAPPEDWINDOW | WS_VISIBLE, mainWindow.x, mainWindow.y, mainWindow.width, mainWindow.height, NULL,           0,           hInstance,      NULL);\n\n    while (GetMessage(&amp;msg, NULL, 0, 0))\n    {\n        TranslateMessage(&amp;msg);\n        DispatchMessage(&amp;msg);\n    }\n\n    return (int)msg.wParam;\n}\n\n// This code is based roughly on tutorial code present at  http://zetcode.com/gui/winapi/\n</code></pre>\n<hr />\n<h2>Further reading</h2>\n<p>The builtin set of window classes are rather limited, so you might be curious as to how you can define your own window classes (&quot;<code>Control</code>s&quot;) using the Windows API, see the articles below:</p>\n<ul>\n<li><a href="https://www.codeproject.com/Articles/559385/Custom-Controls-in-Win-API-The-Basics" rel="nofollow noreferrer">Custom Controls in Win32 API: The Basics (Code Project)</a></li>\n<li><a href="https://www.winehq.org/" rel="nofollow noreferrer">The WINE emulator source</a> serves as a good example of how the Windows API could be implemented, and how you can make your own window classes that imitate the behavior of builtin classes.</li>\n<li><a href="http://zetcode.com/gui/winapi/" rel="nofollow noreferrer">Zetcode.com\'s tutorials</a></li>\n</ul>\n<blockquote>\n<p>NOTE: I originally intended this post to cover the creation of dialogs programmatically. Due to a mistake on my part I didn\'t realize that you can\'t just &quot;show&quot; a window as a dialog. Unfortunately I wasn\'t able to get the setup mentioned by Raymond Chen working. Even looking at WINE\'s source, it\'s not super clear.</p>\n</blockquote>\n'}]
['https://Stackoverflow.com/questions/61634', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1467/']