Knut Hunstad
asked on
DrawText vs GetCellRect
Hi!
I have subclassed CEdit (or actually CGXEditControl from Stingray, but I'm not sure that's the problem here) and override GetCellRect. The purpose is to align numbers with different number of decimals at the decimal point. The font is fixed width. The member variable m_nrOfSpacesAfter is the number of "blank" spaces behind the number. If I display 3 decimals at most, a number written with only 1 decimal will have m_nrOfSpacesAfter set to 2. m_rightMargin is a global offset for all controls of this type.
The problem is that this doesn't work precisely. The width is always 9/char. But the resulting output is a few pixels too far to the left (2/char). If I put in:
it seems to be precise. But I don't want to put in such a fixed number in my code, since there is no guarantee it will be right on other screens, other system fonts etc.
I have tried with GetTextExtent and GetTextMetrics instead of DrawText/DT_CALCRECT, with the same result. They all claim that 9 is the char width.
I'm probably overlooking something obvious...
Thanks for any help!
I have subclassed CEdit (or actually CGXEditControl from Stingray, but I'm not sure that's the problem here) and override GetCellRect. The purpose is to align numbers with different number of decimals at the decimal point. The font is fixed width. The member variable m_nrOfSpacesAfter is the number of "blank" spaces behind the number. If I display 3 decimals at most, a number written with only 1 decimal will have m_nrOfSpacesAfter set to 2. m_rightMargin is a global offset for all controls of this type.
CRect MyEditControl::GetCellRect(ROWCOL nRow, ROWCOL nCol, LPRECT rectItem /* = NULL */, const CGXStyle* pStyle /* = NULL */)
{
CRect returnRect = CGXEditControl::GetCellRect(nRow, nCol, rectItem, pStyle);
UINT fillAfter = 0;
CDC* pDc = GetDC();
if (m_nrOfSpacesAfter > 0)
{
CRect spaceRect(0, 0, 0, 0);
pDc->DrawText(CString(_T('9'), m_nrOfSpacesAfter), spaceRect, DT_CALCRECT);
fillAfter = spaceRect.Width();
}
returnRect.right -= fillAfter;
return returnRect;
}
The problem is that this doesn't work precisely. The width is always 9/char. But the resulting output is a few pixels too far to the left (2/char). If I put in:
fillAfter -= m_nrOfSpacesAfter*2;
it seems to be precise. But I don't want to put in such a fixed number in my code, since there is no guarantee it will be right on other screens, other system fonts etc.
I have tried with GetTextExtent and GetTextMetrics instead of DrawText/DT_CALCRECT, with the same result. They all claim that 9 is the char width.
I'm probably overlooking something obvious...
Thanks for any help!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.