Solved

DrawText vs GetCellRect

Posted on 2013-12-13
1
281 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
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

856 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