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, none of the examples I've read seem to define the dialog programmatically.
Anyone know how to do this? A simple example is fine, I'm not doing anything complicated with it yet.
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.
This isn't the same as a dialog, but it might be easier than creating a dialog programmatically.
First, a few notes about how this is done:
Instead of designing the dialog in the rc file, you could manually use
CreateWindow
(orCreateWindowEx
) to create child windows of a main window. (for .NET Winforms programmers, these windows are likeControl
s).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.
There are some disadvantages to not using a real dialog, namely that tab will not work when switching between controls.
About the example:
TextBox
), and a check box.It has been tested under the following conditions:
UNICODE
and_UNICODE
defined)UNICODE
and_UNICODE
not defined)Now for the code:
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.
Further reading
The builtin set of window classes are rather limited, so you might be curious as to how you can define your own window classes ("
Control
s") using winapi, see the articles below:I think Raymond Chen has a pretty good example here: http://blogs.msdn.com/oldnewthing/archive/2005/04/29/412577.aspx
Salam, Hi, here you can find how to use Windows API Dialogs without using resource files. The Winapi (C Win32 API, No MFC) tutorial: http://zetcode.com/gui/winapi/
Try to search MSDN for "dialog templates in memory"
See this for example: http://msdn.microsoft.com/en-us/library/ms632588(VS.85).aspx
Raymond Chen wrote few posts about dialog manager:
Take a look at this toolkit that describes how to create dialogs without resource files. It's in WTL, however I'm sure you can pick apart the internals to achieve the same thing using win32 API directly.