Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

DrawText vs GetCellRect

Posted on 2013-12-13
1
Medium Priority
?
292 Views
Last Modified: 2013-12-16
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.

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;
}

Open in new window


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;

Open in new window


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!
0
Comment
Question by:Knut Hunstad
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 8

Accepted Solution

by:
Knut Hunstad earned 0 total points
ID: 39721356
I found the problem. I was using this with a Stingray CGXEditControl, in it's GetCellRect. What happened was that GetDC within GetCellRect did not return the same DC as the on in Draw!

So now I calculate the necessary width in Draw() and use it in GetCellRect.
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question