Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to add an hWnd(handle) to a modal CDialog so SetWindowText() works

174 views
Skip to first unread message

Joeri

unread,
Jan 6, 2001, 9:23:18 AM1/6/01
to
Hello, I am using MS Visual C++ and there's a nasty bug(?) i want to
> report on:
> whenever one opens a modal dialog(like the about dialog) and you try to
> use the function
> SetWindowText("Hello") on it, it gives a ASSERTION failure. It appears
> the dialog deosnt have a handle (hWnd) which it needs to perform this
> function. Can anyone please tell me how to overcome this problem. One
> way is to make the dialog modeless (using Create instead of just calling
> it, but i dont want to). So, basically how do i give my CDialog a hWnd
> handle so the problem works.
> To try it out yourselfs: just open a new project and use
> SetWindowText("blabla") on the CDialog:
> void CScribbleApp::OnAppAbout()
> {
> CAboutDlg aboutDlg;
> aboutDlg.SetWindowText("blabla"); // ---> this will create an error, try
> debugging it & you'll see its hWnd=0x00000000 (not good)...
> aboutDlg.DoModal();
> }
>
> thanks a million!!
> please reply to this email adress,
> sincerely,
> Joeri.

David Lowndes

unread,
Jan 6, 2001, 9:33:52 AM1/6/01
to
>> whenever one opens a modal dialog(like the about dialog) and you try to
>> use the function
>> SetWindowText("Hello") on it, it gives a ASSERTION failure. It appears
>> the dialog deosnt have a handle (hWnd) which it needs to perform this
>> function. Can anyone please tell me how to overcome this problem.

Joeri,

You can only use this once the underlying dialog has been created and
attached to the CDialog class - the usual place to do this would be in
the dialog's OnInitDialog handler.

Dave
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq
My address is altered to discourage junk mail.
Please post responses to the newsgroup thread,
there's no need for follow-up email copies.

joeri

unread,
Jan 6, 2001, 9:52:03 AM1/6/01
to

can you put a sample code of how to do it, ive been messing with GetSafeHandle()
etc a long time, so if you just could tell me the keywords to get a handle to the
dialog...
if it has to be done like you said in OnInitDialog...
BOOL CAboutDlg::OnInitDialog()
{
CDialog::OnInitDialog();

?????
return TRUE;
}

Dave, hope you're still here, cause im fucked up until i solve this problem, and
my work is due for monday morning(university task)

Joeri,

.

Joseph M. Newcomer

unread,
Jan 6, 2001, 3:02:34 PM1/6/01
to
Create a member variable for the control whose text you want to set.
For example, call it c_MyEditControl. Then in the OnInitDialog
handler, you can write

c_MyEditControl.SetWindowText("Whatever");

See my essays about Avoiding GetDlgItem and Avoiding UpdateData. You
should never have to touch GetSafeHandle or see an HWND. Note that you
cannot under any circumstances call SetWindowText either before, or
GetWindowText after, the DoModal, a common error.
joe
On Sat, 6 Jan 2001 06:52:03 -0800, "joeri" <jjur...@yahoo.com>
wrote:

>
>can you put a sample code of how to do it, ive been messing with GetSafeHandle()
>etc a long time, so if you just could tell me the keywords to get a handle to the
>dialog...
>if it has to be done like you said in OnInitDialog...
>BOOL CAboutDlg::OnInitDialog()
>{
> CDialog::OnInitDialog();
>
> ?????
> return TRUE;
>}
>
>Dave, hope you're still here, cause im fucked up until i solve this problem, and
>my work is due for monday morning(university task)
>
>-----Original Message-----
>>> whenever one opens a modal dialog(like the about dialog) and you try to
>>> use the function
>>> SetWindowText("Hello") on it, it gives a ASSERTION failure. It appears
>>> the dialog deosnt have a handle (hWnd) which it needs to perform this
>>> function. Can anyone please tell me how to overcome this problem.
>
>Joeri,
>
>You can only use this once the underlying dialog has been created and
>attached to the CDialog class - the usual place to do this would be in
>the dialog's OnInitDialog handler.
>
>Dave

Joseph M. Newcomer [MVP]
email: newc...@flounder.com
Web: http://www3.pgh.net/~newcomer
MVP Tips: http://www3.pgh.net/~newcomer/mvp_tips.htm

ter...@my-deja.com

unread,
Jan 24, 2001, 5:11:43 AM1/24/01
to
In article <0aue5t4e8e65tj7pm...@4ax.com>,

Joseph M. Newcomer <newc...@flounder.com> wrote:
> Create a member variable for the control whose text you want to set.
> For example, call it c_MyEditControl. Then in the OnInitDialog
> handler, you can write
>
> c_MyEditControl.SetWindowText("Whatever");
>

What if you have a CString variable 'str' that you want put in the
SetWindowText? F.ex.:

c_MyEditControl.SetWindowText(str);

The variable 'str' is not remembered by the program in OnInitDialog.
So my question is: how do you bring a long variables from one dialog to
another, to SetWindowText in the OnInitDialog??


Terje Karlsen


Sent via Deja.com
http://www.deja.com/

Jonno

unread,
Jan 24, 2001, 5:55:08 AM1/24/01
to

<ter...@my-deja.com> wrote in message news:94m9or$nsh$1...@nnrp1.deja.com...

Terje,
Add a public CString member to your dialog class, eg.
CString m_text;

Then set it before you call DoModal, or in the constructor.

MyDialg dlg;
dlg m_text = "Hello";
dlg.DoModal();

Then do the SetWindowText in OnInitDialog
Jonno


ter...@my-deja.com

unread,
Jan 24, 2001, 6:31:36 AM1/24/01
to
In article <uvRwGQfhAHA.464@tkmsftngp02>,

"Jonno" <flexiblejon at hotmail.com> wrote:

> Terje,
> Add a public CString member to your dialog class, eg.
> CString m_text;
>
> Then set it before you call DoModal, or in the constructor.
>
> MyDialg dlg;
> dlg m_text = "Hello";
> dlg.DoModal();
>
> Then do the SetWindowText in OnInitDialog
> Jonno
>

Thanks!!
It worked!

0 new messages