mfc >> Can you call SetWindowText on a modal form?

by Ben Taylor » Wed, 26 Nov 2003 17:22:36 GMT

Hi
I have a DLL that is called by Visual Basic and which
displays a modal form. I have compiled this DLL and it
works perfectly, apart from the fact that I want to be
able to pass it a string to display in the title bar, but
the SetWindowText function doesn't seem to do anything.
My code is shown below. Any help would be much appreciated.
Thanks

void __stdcall GetLogin(BSTR appname, BSTR caption, BSTR*
username, BSTR* password, bool reshow)
{
//read the current username from the registry for
this application
CString currentuser = theApp.GetProfileString
((LPSTR)appname, USERNAME, NULLSTRING);

//read the current password from the registry for
this application and decrypt it
CString currentpassword = Encrypt(
theApp.GetProfileString((LPSTR)appname,
PASSWORD, NULLSTRING));

//variables to represent new password
CString retuser, retpass;
if(reshow || (currentuser.GetLength() == 0))
{ //if we need to show the form because either
there is no existing username or because we are told to...
Login flogin;

//update the caption of the form

flogin.SetWindowText((LPSTR)caption);
//THE ABOVE LINE DOESN'T DO ANYTHING...

//show the form modally
flogin.DoModal();

//retrieve the values entered
retuser = flogin.m_Username;
retpass = flogin.m_Password;

//update the registry settings for this
application, after encrypting the password
theApp.WriteProfileString((LPSTR)appname,
USERNAME, retuser.operator LPCTSTR());
theApp.WriteProfileString((LPSTR)appname,
PASSWORD,
Encrypt(retpass).operator LPCTSTR
());
}
else
{
//if values already exist in registry,
just set return values to these
retuser = currentuser;
retpass = currentpassword;
}

//copy return values to byref parameters passed in
LPCSTR luser = retuser.operator LPCTSTR();
LPCSTR lpass = retpass.operator LPCTSTR();
*username = SysAllocStringByteLen(luser,
retuser.GetLength());
*password = SysAllocStringByteLen(lpass,
retpass.GetLength());

}




mfc >> Can you call SetWindowText on a modal form?

by Kim Youngjin » Wed, 26 Nov 2003 17:34:57 GMT


Till you call Create or DoMoal function of Dialog, dialog windows doesn't
be created.
So when you call SetWindowTest(...), there is no window to display the
text.

If you wanna control the modal dialog box test from outside of the dialog,
add function SetWindowTitle(LPCSTR pstr) and contain the name.
And then call SetWindowText(..) in overrided OnInitDialog().

class CTestDialog : public CDialog
{
public:
void SetWindowTitle(LPCSTR pstr);
private:
CString m_strTitle;
};

void CTestDialog::SetWindowTitle(LPCSTR pstr)
{
m_strTitle=pstr;
}

void CTestDialog:;OnInitDialog()
{
SetWindowText(m_strTitle);
}





Similar Threads

1. Can you call SetWindowText on a modal form?

Hi
I have a DLL that is called by Visual Basic and which 
displays a modal form. I have compiled this DLL and it 
works perfectly, apart from the fact that I want to be 
able to pass it a string to display in the title bar, but 
the SetWindowText function doesn't seem to do anything.
My code is shown below. Any help would be much appreciated.
Thanks

void __stdcall GetLogin(BSTR appname, BSTR caption, BSTR* 
username, BSTR* password, bool reshow)
{
	//read the current username from the registry for 
this application
	CString currentuser = theApp.GetProfileString
((LPSTR)appname, USERNAME, NULLSTRING);

	//read the current password from the registry for 
this application and decrypt it
	CString currentpassword = Encrypt(
		theApp.GetProfileString((LPSTR)appname, 
PASSWORD, NULLSTRING));

	//variables to represent new password
	CString retuser, retpass;
	if(reshow || (currentuser.GetLength() == 0))
	{ //if we need to show the form because either 
there is no existing username or because we are told to...
		Login flogin;
		
		//update the caption of the form
	
		flogin.SetWindowText((LPSTR)caption);
//THE ABOVE LINE DOESN'T DO ANYTHING...

		//show the form modally
		flogin.DoModal();

		//retrieve the values entered
		retuser = flogin.m_Username;
		retpass = flogin.m_Password;

		//update the registry settings for this 
application, after encrypting the password
		theApp.WriteProfileString((LPSTR)appname, 
USERNAME, retuser.operator LPCTSTR());
		theApp.WriteProfileString((LPSTR)appname, 
PASSWORD, 
			Encrypt(retpass).operator LPCTSTR
());
	}
	else
	{
		//if values already exist in registry, 
just set return values to these
		retuser = currentuser;
		retpass = currentpassword;
	}

	//copy return values to byref parameters passed in
	LPCSTR luser = retuser.operator LPCTSTR();
	LPCSTR lpass = retpass.operator LPCTSTR();
	*username = SysAllocStringByteLen(luser, 
retuser.GetLength());
	*password = SysAllocStringByteLen(lpass, 
retpass.GetLength());

}


2. WinForms- Modal forms that call functions which change original fo - CSharp/C#

3. Closing a modal form from the calling code

I am calling the ShowModal() function for a form to display error messages and I would like to be able to close this form from the calling code (instead of clicking an Ok button) under certain circumstances. I've tried eveything I can think of and have had no luck. Doses anyone have any ideas?

4. Make non modal form modal - CSharp/C#

5. SetWindowText call in CDialogImpl class in Japanese

I have a modeless dialog window in an ActiveX control derived from the CDialogImpl class which displays progress information.  A secondary thread is making calls to update the text in the progress dialog which is running in the main process thread.  The progress text is being displayed in static controls on the dialog

I have localized this dialog and text in English, French, German, Japanese, Korean, Simplified Chinese, and Traditional Chinese using individual resource files for each language.  The text shows up fine in all languages except Japanese.  In Japanese, the window title bar text shows up with the correct multibyte characters, but the window text shows up with ASCII characters (i.e. punctuation) fine but all the multibyte characters are replaced by either spaces (native Japanese) or black rectangles (MUI).  I have dumped the text in question to a log file and it displays fine, so the text is being retrieved from the resource file and formatted correctly.  I can even write that text to the window title bar and it displays correctly; it is only when I write it to the static controls that it gets messed up.  I am thus thinking that I am running into an ATL bug, since I have written a test program using an MFC dialog reading from the same localized resource file and it works fine on Japanese (native and MUI).  I am using Visual C++ 6 SP4.

Is there any report of such an issue, and if so, is there any patch available
Thanks



6. Getting owner draw child control to paint when calling SetWindowText

7. closing Modal Form inside form

I have a modal form (ShowDialog()) that I may want to close when a user 
clicks a button.  I already have an OK and Cancel button, so I can't use 
those options, but I want it to function just like an OK button press.  See 
Example:

    private void add_Click(object sender, EventArgs e)
    {
      // Do some work...

      if (itemcnt == 0)
      {
         this.DialogResult = DialogResult.Cancel;
         // Close current form and return.
      }
    }

8. Modal Form from a child form - CSharp/C#