#### Archived

This topic is now archived and is closed to further replies.

# WM_INITDIALOG... *sigh*!

## Recommended Posts

quote:
The WM_INITDIALOG message is sent to the dialog box procedure immediately before a dialog box is displayed. Dialog box procedures typically use this message to initialize controls and carry out any other initialization tasks that affect the appearance of the dialog box.
quote:
The SetDlgItemText function sets the title or text of a control in a dialog box.
The above are quotes from MSDN... If the above is true , then why won''t my code work?

BOOL CALLBACK DlgProc(HWND hWnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
switch(iMsg)
{
case WM_INITDIALOG:
SetDlgItemText(hWnd, IDC_EDIT1, "Welcome to my program!");
return true;
}

return false;
}

IDC_EDIT1 is a constant used in the dialog template and is defined in my resource.h and included in the original source file. What am I doing wrong here?

##### Share on other sites
I''m going to assume that by the name IDC_EDIT1 you are using an edit text box and wish to fill in the value. Try the following instead:
SendDlgItemMessage( hWnd,                        IDC_EDIT1,                         WM_SETTEXT,                        0,                        ( LPARAM )"Welcome..." );

If my assumption is incorrect, please elaborate.

##### Share on other sites
What value does SetDlgItemText return?

##### Share on other sites
The code you''ve posted works. The problem must be somewhere else, specifically in the dialog template or in the code to create the dialog.

##### Share on other sites
PropellerBoy: You assumption was correct, but I don''t think I need to used SendDlgItemMessage. I have tried doing SetDlgItemText in response to a WM_COMMAND message (ie. a button was clicked) and it worked, so SetDlgItemText seems to be al I need.

AP: The only code that is in my WinMain is a call to DialogBox. This function requires a HWND to the owner window, but since this dialog is the parent window, I just pass NULL. As far as I know (I checked MSDN but they don''t say if passing NULL is ok or not...) this isn''t the problem...

Solo: Duh! I could''ve thought of that myself..! I''m gonna check as soon as Christmas is over, as I don''t feel like sitting alone in my bedroom these days.

Oh, that reminds me! Merry Christmas to y''all!

##### Share on other sites
"The only code that is in my WinMain is a call to DialogBox."

so you don''t have a message pump?

##### Share on other sites
I''ve used that method before, successfully. The dialog call looks like this (in WinMain):
DialogBox( hInstance,            MAKEINTRESOURCE( IDD_MY_DIALOG ),	    NULL,            DialogFunction );

It works for single dialog apps. However, I''m sure it''s not the "best" way.

##### Share on other sites
Calling DialogBox as PropellerBoy posted is correct. NULL is acceptable for hwndParent and no message loop is necessary because DialogBox has an internal message loop.

I threw together a tester that uses your DlgProc with a call to DialogBox in WinMain. It works fine. Here''s what the resource stuff looks like...

In resource.h:
// AppWizard stuff snipped out#define IDD_DIALOG1                     101#define IDC_EDIT1                       1000// AppWizard stuff snipped out

In app.rc:
// AppWizard stuff snipped outIDD_DIALOG1 DIALOG DISCARDABLE  0, 0, 186, 30STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENUCAPTION "Dialog"FONT 8, "MS Sans Serif"BEGIN    EDITTEXT        IDC_EDIT1,7,7,172,16,ES_AUTOHSCROLLEND// AppWizard stuff snipped out

##### Share on other sites
Well thanks AP for checking that for me, but I knew that that code would work (Or at least the dialog box pops up). The problem here is that for some reason I can''t initialize the dialogboxes edit control...

quote:
However, I''m sure it''s not the "best" way.

I know this probably isn''t the best way. I think the best way would be to supply my own messagepump by registering a WNDCLASSEX structure with it''s cbWndExtra field set to DLGWINDOWEXTRA, and using the CLASS directive in my resource template. This would instruct Windows not to use the standard dialogbox messagepump and let''s my program use it''s own. The problem with this is that the Windows messagepump allows the user to conveniently switch focus between my controls using the TAB-key, and I don''t feel like coding the ''TAB logic'' myself .
However, an added bonus to this approach is that I can use my own icon on the dialogbox by filling the hIcon field of the WNDCLASSEX struct. I have tried putting my own icon in there with the DialogBox approach (by using SetClassLong in response to the WM_INITDIALOG message) but this didn''t work. Anyone know how to do this?

So the questions now are :

1. How do I initialize my edit control, and
2. How can I let the dialogbox (and the .exe file itself) use my own kewl icon?

Thanks for the responses up until now, guys!

Regards, MainForze

##### Share on other sites
put this inside the funciton then use its handle instead of the hWnd you passed to the funciton:

HWND hWndEdit1 = GetDlgItem(hWnd,IDC_EDIT1);

• ### Forum Statistics

• Total Topics
628300
• Total Posts
2981894

• 9
• 9
• 11
• 10
• 10