CDC::DrawText Not doing as it's told
-
Guys, I have a problem with CDC::DrawText() and I have been sitting here wasting away my Sunday trying to figure it out. The problem is that it does not draw the text in the position I expect it to be. The following code is supposed to draw a rectangle, then draw the text in the box, using the style (DT_LEFT, DT_CENTER or DT_RIGHT).
CBrush brush(m\_background\[nIndex\]); CBrush\* pOldBrush = pDC->SelectObject(&brush); pDC->SetBkColor(m\_background\[nIndex\]); pDC->SetBkMode(OPAQUE); pDC->SelectStockObject(NULL\_PEN); pDC->Rectangle(m\_hitRect); pDC->SelectObject(pOldBrush); pDC->SetBkMode(TRANSPARENT); pDC->DrawText(txt,m\_hitRect, m\_nStyle | DT\_SINGLELINE | DT\_VCENTER);
The text appears in the box (but not in the correct position) when I use DT_CENTER or DT_RIGHT but is not drwn at all with DT_LEFT (I think its outside the rect). Please help, I really should be cutting the grass! :-D Tony
-
Guys, I have a problem with CDC::DrawText() and I have been sitting here wasting away my Sunday trying to figure it out. The problem is that it does not draw the text in the position I expect it to be. The following code is supposed to draw a rectangle, then draw the text in the box, using the style (DT_LEFT, DT_CENTER or DT_RIGHT).
CBrush brush(m\_background\[nIndex\]); CBrush\* pOldBrush = pDC->SelectObject(&brush); pDC->SetBkColor(m\_background\[nIndex\]); pDC->SetBkMode(OPAQUE); pDC->SelectStockObject(NULL\_PEN); pDC->Rectangle(m\_hitRect); pDC->SelectObject(pOldBrush); pDC->SetBkMode(TRANSPARENT); pDC->DrawText(txt,m\_hitRect, m\_nStyle | DT\_SINGLELINE | DT\_VCENTER);
The text appears in the box (but not in the correct position) when I use DT_CENTER or DT_RIGHT but is not drwn at all with DT_LEFT (I think its outside the rect). Please help, I really should be cutting the grass! :-D Tony
-
Guys, I have a problem with CDC::DrawText() and I have been sitting here wasting away my Sunday trying to figure it out. The problem is that it does not draw the text in the position I expect it to be. The following code is supposed to draw a rectangle, then draw the text in the box, using the style (DT_LEFT, DT_CENTER or DT_RIGHT).
CBrush brush(m\_background\[nIndex\]); CBrush\* pOldBrush = pDC->SelectObject(&brush); pDC->SetBkColor(m\_background\[nIndex\]); pDC->SetBkMode(OPAQUE); pDC->SelectStockObject(NULL\_PEN); pDC->Rectangle(m\_hitRect); pDC->SelectObject(pOldBrush); pDC->SetBkMode(TRANSPARENT); pDC->DrawText(txt,m\_hitRect, m\_nStyle | DT\_SINGLELINE | DT\_VCENTER);
The text appears in the box (but not in the correct position) when I use DT_CENTER or DT_RIGHT but is not drwn at all with DT_LEFT (I think its outside the rect). Please help, I really should be cutting the grass! :-D Tony
You don't show the values of variables so it's hard to guess. If you strip it down and hardcode values it works fine...
CString txt = \_T("This is the text!"); CRect m\_hitRect(10,10,200,40); //CBrush brush(RGB(0xFF,0x00,0x00));//(m\_background\[nIndex\]); //CBrush\* pOldBrush = dc.SelectObject(&brush); //dc.SetBkColor(RGB(0x80,0x80,0x80)); //dc.SetBkMode(OPAQUE); //dc.SelectStockObject(NULL\_PEN); dc.Rectangle(m\_hitRect); //dc.SelectObject(pOldBrush); //dc.SetBkMode(TRANSPARENT); dc.DrawText(txt,m\_hitRect, DT\_LEFT | DT\_SINGLELINE | DT\_VCENTER);
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
You don't show the values of variables so it's hard to guess. If you strip it down and hardcode values it works fine...
CString txt = \_T("This is the text!"); CRect m\_hitRect(10,10,200,40); //CBrush brush(RGB(0xFF,0x00,0x00));//(m\_background\[nIndex\]); //CBrush\* pOldBrush = dc.SelectObject(&brush); //dc.SetBkColor(RGB(0x80,0x80,0x80)); //dc.SetBkMode(OPAQUE); //dc.SelectStockObject(NULL\_PEN); dc.Rectangle(m\_hitRect); //dc.SelectObject(pOldBrush); //dc.SetBkMode(TRANSPARENT); dc.DrawText(txt,m\_hitRect, DT\_LEFT | DT\_SINGLELINE | DT\_VCENTER);
Mark Salsbery Microsoft MVP - Visual C++ :java:
Hi thanks for the reply. I figured out what it was, and as usual its something dumb and not in the area I was looking in. A long, long time ago I had padded out the right hand side of the string with spaces (not sure why I did this). Anyway, as you can imagine, this would totally screw up the justification. Oh well, that grass wont mow itself :laugh: Thanks