I work with WinCE / MFC.
I am trying to create a modal dialog (errorDlg) used for displaying
error messages. It is based on CDialog.
I try to display it using the following code :
errorDlg *dlg = new errorDlg();
dlg->ShowMessage(CST_OMR_TOO_MANY_TOB);
The code of the ShowMessage() function of errorDlg is :
void errorDlg::ShowMessage(int code)
{
CStatic *edit;
char str[] = "THIS IS A MESSAGE";
char *pStr1 = str;
LPCTSTR string = (LPCTSTR)str;
edit = (CStatic *)GetDlgItem(IDC_MYEDIT);
edit->SetWindowTextW(string);
DoModal();
}
but the line :
edit->SetWindowTextW(string);
make the program hang. It think that it may be because the Edit control
that is in the window is not available until the DoModal() is
completed, but I can't understand how I can display the message before
the DoModal() is called ?
Thank you for any help ...
Yes thats because the control is available at the time you are trying
to access it. What you are trying to do should be done in OnInitDialog
instead.
--
Ajay
But how can I provide the message string to be displayed before the
OnInitDialog() is executed ?
Sorry, I believe I made a confusion between the OnInitDialog() function
and the errDlg constructor, which are two different things !
Yes. You can pass it in the constructor or simply expose a method
which accepts the string you want. Then you can use it like this:
CErrorDialog errDlg;
dlg.SetString("SomeString"); // this will set the string to a member
variable which you can use in OnInitDialog
dlg.DoModal();
--
Ajay
And why are you using pointer so much when you don't have to?
errorDlg Dlg("This is the error message",this);
Dlg.DoModal();
errorDlg::errorDlg(const CString &ErrorMessage,CWnd *pParent)
:CDialog(errorDlg::IDD,pParent)
, m_ErrorMsg(ErrorMessage)
{
}
void errorDlg::DoDataExchange(...)
{
CDialog::DoDataExchange();
DDX_Control(pDX,IDC_MYEDIT,m_MsgCtrl);
}
BOOL errorDlg::OnInitDialog()
{
CDialog::OnInitDialog();
m_MsgCtrl.SetWindowText(m_ErrorMsg);
}
AliR.
"No_Name" <no_mail@no_mail.com> wrote in message
news:gribhj$ncf$1...@aioe.org...
> Dans son message pr�c�dent, No_Name a �crit :
>> Ajay a pr�sent� l'�nonc� suivant :
>>> On Apr 8, 10:00 am, No_Name <no_mail@no_mail.com> wrote:
>>>> errorDlg *dlg = new errorDlg();
>>>> dlg->ShowMessage(CST_OMR_TOO_MANY_TOB);
>>>>
>>>> The code of the ShowMessage() function of errorDlg is :
>>>>
>>>> void errorDlg::ShowMessage(int code)
>>>> {
>>>> CStatic *edit;
>>>>
>>>> char str[] = "THIS IS A MESSAGE";
>>>> char *pStr1 = str;
>>>> LPCTSTR string = (LPCTSTR)str;
>>>> edit = (CStatic *)GetDlgItem(IDC_MYEDIT);
>>>> edit->SetWindowTextW(string);
>>>>
2. Display the message in the dialog's OnInitDialog function. (m_static is a
CStatic added to the dialog class using 'Add Variable')
m_static.SetWindowText(m_strMessage);
BTW, you are using 'char' but calling SetWindowTextW, which needs wchar_t.
Don't mix the two types!
"No_Name" <no_mail@no_mail.com> wrote in message
news:griaqp$mi4$1...@aioe.org...
--
Scott McPhillips [VC++ MVP]
I am using SetWindowTextW() because in WinCE I work with Unicode.
Thank you for all your answers. Now I set the message in the
OnInitDialog(), but the :
edit->SetWindowTextW(string);
still makes my program hang...
BTW, the SetWindowTextW() requests not a wchar_t pointer but a LPCTSTR
pointer.
Show your code of OnInitDialog. Make sure that you call the base
class first.
Also, use _T macro in SetWindowText.
--
Ajay
But the code that you posted has this...
>> char str[] = "THIS IS A MESSAGE";
>> char *pStr1 = str;
A unicode app does not use 'char'
In Unicode build LPCTSTR is the same as const wchar_t*.
You really have to make sure that you understand how strings are implemented in VC.
--
David Wilkinson
Visual C++ MVP
Tom
"Scott McPhillips [MVP]" <org-dot-mvps-at-scottmcp> wrote in message
news:%23s9E4wF...@TK2MSFTNGP02.phx.gbl...
See below...
On Wed, 08 Apr 2009 16:00:29 +0200, No_Name <no_mail@no_mail.com> wrote:
>Hello,
>
>
>I work with WinCE / MFC.
>I am trying to create a modal dialog (errorDlg) used for displaying
>error messages. It is based on CDialog.
>
>
>I try to display it using the following code :
>
>errorDlg *dlg = new errorDlg();
****
If this is a modal dialog, there is no reason to create a pointer to it, or use new.
Eliminate these.
****
>dlg->ShowMessage(CST_OMR_TOO_MANY_TOB);
>
>The code of the ShowMessage() function of errorDlg is :
>
>void errorDlg::ShowMessage(int code)
>{
> CStatic *edit;
>
> char str[] = "THIS IS A MESSAGE";
****
Since this is CE, why are you using 8-bit character strings?
****
> char *pStr1 = str;
****
This doesn't even make sense! What conceivable good does introducing this variable do?
ANswer: None whatsoever. It is an aberration. Eliminate it. Its presence is complete
and utter nonsense.
****
> LPCTSTR string = (LPCTSTR)str;
****
This doesn't make sense. You are taking an 8-bit pointer (by using a gratuitously useless
variable to get it) and telling the compiler to interpret it as a Unicode string, which is
complete and utter nonsense. I have no idea how you could make sense of this
****
> edit = (CStatic *)GetDlgItem(IDC_MYEDIT);
****
As others have pointed out, the dialog does not exist, so this statement is nonsense. It
is *always* nonsense to attempt to manipulate a control from outside a dialog, but in
thise case it is serious nonsense because the control does not exist
****
> edit->SetWindowTextW(string);
****
And, of course, this cannot work.
****
>
> DoModal();
****
Completely wrong. Why do that here?
****
>
>}
>
>but the line :
>edit->SetWindowTextW(string);
>make the program hang. It think that it may be because the Edit control
>that is in the window is not available until the DoModal() is
>completed, but I can't understand how I can display the message before
>the DoModal() is called ?
****
You can't. Don't expect to. Other than a complete rewrite because every single line is
wrong, there's nothing you can do to salvage this code.
Create a value string for the message in your dialog. You can either expose the CString
variable directly or you can use a setter method to set it.
MyDlg dlg;
dlg.Message = L"This is a message";
dlg.DoModal();
or
MyDlg dlg;
dlg.SetMyText(L"This is a message");
dlg.DoModal();
see how easy this was? No 'new', no pointers, no casts that lie about data types, no
attempt to manipulate controls from outside the dialog, no use of the ugly and antiquated
GetDlgItem (which is really useful only in very rare and exotic circumstances, which this
is not even *near*)
joe
****
>
>Thank you for any help ...
>
Joseph M. Newcomer [MVP]
email: newc...@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm