Re: GetTextExtent and Window Placement
Hi, Dr. Newcomer:
First, the pDC comes from the OnPaint handler.
void CGraphWnd::OnPaint()
{
CPaintDC dc(this);
this->DrawAndMove(&dc);
}
All the previous code pasted belongs to the DrawAndMove() function.
Next, see comments and questions below...
"Joseph M. Newcomer" wrote:
See below...
On Fri, 16 Feb 2007 08:35:28 -0800, Trecius <Trecius@discussions.microsoft.com> wrote:
Hello, Newsgroupians:
I've yet another question that's been taxing my brain.
Here's my scenario. I've two windows: a parent window and a child window.
In the parent window, I'm creating a font that's at a ninety degree angle to
the horizontal. In the WM_PAINT handler, with the font I created, I get the
extents of a specific string. Once I have the extents of the string, I
reposition the child window to be the entire height of the parent window
subtract the extent of the text. Finally, I write text below the child
window.
The GUI representation is as follows...
|-----------------------------------|
| |--------------------------------| |
| | | |
| | (CHILD) | |
| | | |
| | | |
| |--------------------------------| |
| X |
| X |
|-----------------------------------|
(Note: The two X's are a string, but it's written vertically)
Here's my code.
// These 6 lines change the extents and moves the origin to the bottom left
and makes
// the coordinates a cartesian-style [+x = right, +y = up]
CRect rect;
this->GetClientRect(&rect);
pDC->SetMapMode(MM_ANISOTROPIC);
pDC->SetWindowExt(32767, 32767);
pDC->SetViewportExt(rect.right, -rect.bottom);
pDC-> SetViewportOrg(0, rect.bottom);
*****
This seems a bit complex. Is it really necessary?
*****
I do this, so I can use standard cartesian coordinate system, with the
origin at the bottom left. I do this for later I need to place graphing
coordinates in my window.
// Create the font
CFont font;
font.CreateFont(32767 / 75, 32767 / 100, 900, 900, FW_DONTCARE, FALSE,
FALSE, FALSE,
ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE,
_T("Arial"));
****
900? Not FW_HEAVY?
When I did 900, 900, it is the angle of escapement and orientation. The
next argument is FW_DONTCARE.
What effect are you attempting to achieve by using values like 32767 and 75? Perhaps you
meant something like CreatePointFontIndirect?
****
32767 is the window's extent, correct? Well, 32767 / 75 is just a ratio I
created that is used as the font size. I suppose it can be anything; the
same holds true for 32767 / 100.
CFont *pOrigFont = pDC->SelectObject(&font);
****
We have here a CDC *. Where did it come from? You have not given the context of where
this code is executed.
****
See above. The CDC * comes from OnPaint(), where OnPaint calls all this
code in another function.
// Get the extent
CSize ext;
::GetTextExtentPoint32(pDC->m_hAttribDC, _T("Canoe"), 5, &ext);
****
And what is wrong with
ext = pDC->GetTextExtent(_T("Canoe"));
?
****
I tried using pDC->GetTextExtent(_T("Canoe")), but it would fail when the
parent window were shrunk too small.
// Convert the extent to DP, so I can move the CHILD window
pDC->LPtoDP(&ext);
// Move the CHILD window
// Notice I use ext.cx and not .cy
this->m_wndChild.MoveWindow(0, 0, rect.right, rect.bottom - ext.cx);
****
Wouldn't it have been easier to write
SetWindowPos(NULL,
0,0, // ignored
rect.Width(),
rect.Height() - ext.cx,
SWP_NOMOVE | SWP_NOZORDER);
What's the advantage of using SetWindowPos versus MoveWindow?
In the worst case, the use of this-> seems redundant
****
// Finally, draw the text below the child window
pDC->DPtoLP(&ext);
pDC->TextOut(32767 / 2, ext.cx, _T("Canoe"));
Hopefully it wasn't too big or hard to understand.
****
Since this is in the OnPaint handler, I don't see how you get a CDC * for your work,
because the CPaintDC is not a CDC *. You need to show all the code, such as where this
mysterious CDC * is coming from.
Note that in OnPaint handler, the CPaintDC already has a clipping rectangle, so you can't
use the CPaintDC after you've resized the control, because the clipping region is already
set for the original position, which is why you would see the clipping of the text.
*****
My problem is that the word Canoe is always clipped by the parent Window.
Depending on the dimensions of the parent window, my text can be seen in its
entirety or the text is clipped. I can read "Can" and a half an 'o' at
times, or the entire word. It's very strange.
Another anomaly I experience is if I place the text ABOVE the child window,
it is always "correct." That is, the text is never clipped.
this->m_wndChild.MoveWindow(0, ext.cx, rect.right, rect.bottom - ext.cx);
...
pDC->TextOut(32767 / 2, ext.cx, _T("Canoe"));
Why is does it act correctly -- no clipping -- when I place the text atop
the child window as opposed to placing it below the child window?
Now, I've read ::GetTextExtentPoint32(...) and it states "that the angle of
escapement is always assumed to be 0... for both vertical and horizontal
fonts." Was it incorrect for me to assume that the .cx is the height? Do I
need to do a conversion? This is the only thing I can think of.
Thank you, Newgroupians, for your continued support.
Trecius
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
Thank you once again.