Solved

DrawText vs GetCellRect

Posted on 2013-12-13
1
273 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:khun
1 Comment
 
LVL 8

Accepted Solution

by:
khun 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Cannot debug web form sthrough Visual Studio 2013 2 60
Changing the installation path of this MSI 5 94
ClickOnce Install - Shortcut Question 3 73
Dialogbox API leak? 18 81
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

895 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now