tags:

views:

168

answers:

3

Hey folks,

I am currently writing a program in c++ (no MFC) and want to update a label (win32 static control) using the win32 DrawText function. However when i call the function nothing is written to the label. I use the following code:

    HDC devCon = ::GetDC(GetDlgItem(IDC_TITLE).m_hWnd);
    RECT rect = {10, 10, 100, 15};
    ::DrawText(devCon, _T("TEST DC TEXT!!!"), -1, &rect, DT_NOCLIP);
    ::ReleaseDC(GetDlgItem(IDC_TITLE).m_hWnd, devCon);

As you see with the GetDlgItem(...) I am using ATL but that should not be a problem in my opinion. When I specify NULL in the GetDC method the text is drawn in the upper left corner of the screen as it is supossed to be since the method return the DC to the entire screen. Why doesn't this work with the DC of the label?

Hope you folks can help me.

A: 

I guess that the text is drawn but at the next window message is set to the default text.

Try to set the text with SendMessage(..,WM_SETTEXT,...);

Karsten
WM_SETTEXT does not work for me because for some reason the label does not show ellipsis if the text is to long like it is supposed to do.
Russo
Forget what I just said, it works perfectly!I just have the problem that I want to display two line text with end ellipsis and MSDN says you have to use DrawText for that.
Russo
A: 

Use SetDlgItemText() to set the text for the control.

You are trying to paint directly onto the static control's device context. This is not going to be so simple, because:

  • the control will repaint itself whenever it's update region is invalidated

  • usually the controls share the device context with the parent window, so what you are getting in GetDC(...) is actually your dialog's device context.

So, use SetDlgItemText, or SetWindowText to set the text of the window.

To use a custom font (or set the text/background color), handle the WM_CTLCOLORSTATIC message in your WindowProc.

StrawberryInChampagne
A: 

If you want to draw the text manually because setting the control text doesn't do what you want, then you need to tell Windows that you're doing that. Otherwise the control will draw itself over whatever you do whenever it needs to be redrawn.

To draw it yourself, mark your control as owner draw by setting the SS_OWNERDRAW style, and then handle the WM_DRAWITEM message to draw it in the window procedure of the parent window, or subclass the window and handle the WM_PAINT message in your new window procedure.

Anthony Williams