Sign in to follow this  

[W32] Controls

This topic is 3874 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

EDIT: Maybe I'm misunderstanding... do controls HAVE to be in some kind of dialog box? I've been reading "theForger's Win32 API Tutorial", and I got to the point of controls. In the controls tutorial, he shows you how to add controls to a dialog box using resources. In the next lesson, he shows you how to add controls to the window at runtime. Maybe I missed something, but I didn't see anything about making controls on the window (not dialogs) with resources. Maybe you can't do that, but it makes sense to me that if you can add controls to dialog boxes with resources, you should be able to do the same with a window. For example, I want to add an EDIT control to my window, for a simple text editor. It explains how to do that in the code during runtime, but not using resources. I would assume that's possible but I could be wrong. So, how do I add controls to a window with resources, like you do dialog boxes? Or can you only do controls for windows at runtime? I hope this made sense, maybe I just have no idea what I'm talking about. [Edited by - Uphoreum on May 9, 2007 9:34:05 PM]

Share this post


Link to post
Share on other sites
You can use a dialog box as the primary window for your program.

Here's a quick run through


// in the resource file

MYDLG DIALOGEX DISCARDABLE 0, 0, 200,200
STYLE DS_SHELLFONT | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
CLASS "WLDialog"
CAPTION "sample"
FONT 8, "MS Shell Dlg"
BEGIN
DEFPUSHBUTTON "&Bye", IDCANCEL, 7,4,50,14, WS_TABSTOP
END

// in the source file

int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hinstPrev,
LPSTR lpCmdLine, int nShowCmd)
{

WNDCLASS wc;

wc.style = CS_DBLCLKS | CS_SAVEBITS | CS_BYTEALIGNWINDOW;
wc.lpfnWndProc = WLWndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = DLGWINDOWEXTRA;
wc.hInstance = hinst;
wc.hIcon = NULL;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = NULL;
wc.lpszMenuName = NULL;
wc.lpszClassName = TEXT("WLDialog");

if (!RegisterClass(&wc)) return FALSE;

return DialogBox(hinst, MAKEINTRESOURCE(MYDLG), NULL, (DLGPROC)WLWndProc);

}




The important things to remember are that you need to include the CLASS specification in the dialog definition in the resource file. In the above example that would be CLASS "WLDialog" - and the name of the class has to be the same classname assigned to the wndclass structure to be registered - wc.lpszClassName = TEXT("WLDialog"); - and the wndclass structure WndExtra member has to be set to DLGWINDOWEXTRA - wc.cbWndExtra = DLGWINDOWEXTRA;


You can find a more involved example here: Another different type of dialog procedure (that's where I took the basics from above).

You can also find more information here: Windows Programming/Dialog Boxes

And here: Tutorial 10: Dialog Box as Main Window

And here: Using Dialog Boxes

And if you're feeling really ambitious and want to try it in memory: Windows API: In memory dialog box templates...


Share this post


Link to post
Share on other sites
A dialog box and a window are the same thing. The difference between them is that a dialog or more like a pre-made window. You can still create controls during runtime and place them on a window, it's just a lot more work.

Share this post


Link to post
Share on other sites
Okay, so I just wanna clear up:

You CAN create controls directly on a normal window, like your main window, but you CAN'T do it with resources, only at runtime?

Share this post


Link to post
Share on other sites
Quote:
You CAN create controls directly on a normal window, like your main window, but you CAN'T do it with resources, only at runtime?


Correct, because there is no such thing as a Win32 window template resource. A window is a very generic item, it may even be a control. A dialog is a more specialized window designed expressly for containing other controls. Hence the reason dialog template resources do exist.

Share this post


Link to post
Share on other sites

This topic is 3874 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this