Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't display text in a modeless dialog box when initially displayed

Status
Not open for further replies.

jtrox

Technical User
Nov 22, 2002
8
US
I have a modeless dialog box with a listbox control and a static control. When the dialog box is shown initially, I have a handler in place for the WM_SHOWWINDOW message to select the first item in the listbox, and then display the appropiate text corresponding to that item in the static control.

Because the text going to the static control is of variable length and I want it centered vertically, I went ahead and get a CClientDC object for the static box and use the DrawText function to draw the text in the client area.

The problem that I have is that when the modeless window is initially displayed, the first item in the listbox is selected correctly, but no text appears in the static control. After the window is drawn, when an item is selected in the listbox drawn the text is displayed correctly.

Does anyone know what I need to do to have the text appear when the window is initially displayed?

Thanks,

Jason

 
What you are wanting to do sounds straightforward. What I don't understand is why you are having to involve WM_SHOWWINDOW & CClientDC. I would be interested in seeing your code.
 
If you wish to center the text in a static, you can specify that as a parameter of the object itself.
 
PencilBiter - It does seem convoluted. This modeless dialog is created at the start of the app and exists the entire time. When the user closes it, I hide the dialog with the (SW_HIDE) flag, and when they launch it I use the (SW_SHOW) flag. That's why I'm using the WM_SHOWWINDOW event.

I tried assigning a variable to the static box, but couldn't get the formatting that I wanted using the standard MFC options. Namely - to get the wordwrap I couldn't use the vertical center format. So instead I process the text myself and then use the DrawText function. The code looks like this:

void CDefinitionsDlg::OnShowWindow(BOOL bShow, UINT nStatus)
{
CDialog::OnShowWindow(bShow, nStatus);
if(bShow)
{
m_ctlDefList.SetCurSel(0);
m_ctlDefList.GetText(0, m_currentTerm);

UpdateData(FALSE);

DisplayText(m_currentTerm);

}
return;
}

void CDefinitionsDlg::DisplayText(CString defText)
{
CSize fullSize;
Crect clientRect;
int numberLines;

CWnd* pWnd = GetDlgItem(IDC_DEF_TEXT);
CClientDC aDC(pWnd);

pWnd->GetDClientRect(clientRect);

// Create margins in rect
clientRect.DeflateRect(9,7,5,5);

CString formattedString;

// The FormatText function adds /r/n for word-wrapping
formattedString = FormatText(defText);

CFont* oldFont = aDC.SelectObject(&systemFont);

CRect textRect;
textRect = GetBoundingRect(aDC, formattedString,
clientRect)
aDC.SetBkMode(TRANSPARENT);

aDC.DrawText(formattedString, textRect, NULL);
aDC.SelectObject(oldFont);

return;
}


 
Sorry - forgot to mention that textRect is calculated to be centered vertically within the clientRect area.
 
I may be wrong; however, can you use the UpdateWindow() function within the OnInitDialog function?

I often enter a value in a dialog box's control with the control's SetWindowText function and then call the UpdateWindow function to display the updated value.
 
The dialog is not yet visible in OnShowWindow.Hence, the text you write to it will be erased. Have you tried calling settext in the OnPaint method?
Greetings,
Rick
 
Thanks everyone. LazyMe is right - nothing I add to the OnShowWindow function is displayed. So I went ahead and added the following code to my OnPaint function:

void CDefDlg::OnPaint()
{
// Get a pointer to the static text dialog
CWnd* pWnd = GetDlgItem(IDC_TEXT)

CString blkspace = " ";
pWnd->SetWindowText(blkspace);

UpdateWindow();

// This is the function that I wrote to make the text
// wrap correctly and be centered vertically
DisplayText(m_definition);
}

Inspite of how ugly this is, it works correctly. If I don't output something to the static box with the SetWindowText function, nothing is printed when the dialog is displayed. The UpdateWindow() function too must be called within the OnPaint() or nothing is displayed initially. Thanks for the help everyone. If there is a better way of doing this, I'd love to hear about it.
 
I don't think the updatewindow should be called in the OnPaint function. You see; UpdateWindow itself is responsible for sending WM_PAINT messages to the window if its update rectangle is not empty. This WM_PAINT message will result in the call to OnPaint....
Greetings,
Rick
 
I understand what you are saying LazyMe - I had the same concerns when I put it there. For some reason though, it doesn't end up creating an endless loop. And I haven't been able to find another place that I could put the function call that works....
 
>> it doesn't end up creating an endless loop.

perhaps not if it "posts" the message rather than "sending" it as this makes it an asynchronous operation. However it would still produce a very wasteful (hog) application in terms of processor usage.

-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top