Solved

DrawText vs GetCellRect

Posted on 2013-12-13
1
285 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

679 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