[Solved]Getting mad showing a template!
This time I definitely need your help, I tried every way my brain found for three days and now I'm out of ideas.
The problem: I need to show a dialog template. Dialog template measures are in Dialog Units, which depend on the dialog font's height'n'width.
Premise: I already read a lot and a lot of times the articles:
http://support.microsoft.com/default...b;EN-US;145994
and
http://support.microsoft.com/kb/125681/EN-US/#top
My idea was this:
I get system font width and system font height, then I get dialog custom font's width and height and I can perform the conversion with the formula
horz pixels == 2 * horz dialog units * (average char width of dialog font
/ average char width of system font)
vert pixels == 2 * vert dialog units * (average char height of dialog font
/ average char height of system font)
Easy to say...
The code I used to get system font size and dialog font's size are:
//TO GET SYSTEM FONT AVG VALUES
CRect rc( 0, 0, 4, 8 );
MapDialogRect( &rc );
int baseUnitY = rc.bottom;
int baseUnitX = rc.right;
TRACE("baseUnitX = %d\n", baseUnitX);
TRACE("baseUnitY = %d\n", baseUnitY);
//TO GET DIALOG'S FONT AVG VALUES
CPaintDC dc(this); // contesto di periferica per il disegno
SelectObject(dc,&m_font);
TEXTMETRIC tm;
CSize size;
GetTextMetrics(dc,&tm);
GetTextExtentPoint32(dc,"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",52,&size);
int avgWidth = (size.cx/26+1)/2;
int avgHeight = (WORD)tm.tmHeight;
TRACE("baseUnitX = %d\n", avgWidth);
TRACE("baseUnitY = %d\n", avgHeight);
SecondPremise: m_font is a CFont variable loaded this way:
LOGFONT lf;
memset(&lf, 0, sizeof(LOGFONT)); // Clear out structure.
lf.lfHeight = 8; // Request a tot-pixel-high font
strcpy(lf.lfFaceName, "Tw Cen MT Condensed"); // with face name we read
//lf.lfHeight = 15; // Request a tot-pixel-high font
//strcpy(lf.lfFaceName, "MS Sans Serif"); // with face name we read
m_font.CreateFontIndirect(&lf); // Create the font.
Note that m_font is correctly initialized and is NOT destroyed, it is a public variable in the mfc dialog's class. And i used it to set the font to some control, so.. no errors here.
Why coordinates I get are totally WRONG?
Am I getting wrong in something? I also thought about creating a dialog with CreateIndirect, but couldn't find good examples. If you can, help me understanding why I am getting wrong, imho it is all right, but results are wrong :(
Re: Getting mad showing a template!
[QUOTE=NasterMain]The problem: I need to show a dialog template./QUOTE]Can you be more specific? What you mean by that? Why would you want to display dialog template?
Are you trying to change dialog's font?
Re: Getting mad showing a template!
I am trying to get real size (in pixels) of a dialog template (or extended template)
The template (or extended template) has its own font (typeface unicode string) and its own size (pointsize) and its own rect (x,y,cx,cy) expressed in dialog units.
I need to calculate them in REAL coordinates (pixel), and because the template uses a different font from my mfc dialog, i cannot use MapDialogRect.
The method i used is shown above, but cannot get it to work!
Thank you very much for taking care of my problem
Re: Getting mad showing a template!
Problem solved by myself, if anyone has the same problem, contact me.
Re: Getting mad showing a template!
Quote:
Originally Posted by NasterMain
Problem solved by myself, if anyone has the same problem, contact me.
Please be courteous and tell us how you solved the problem.
Boards such as CodeGuru are designed to be searched by others who may have the same problem and expect to find the solutions here -- not by sending an email to someone.
Regards,
Paul McKenzie
Re: Getting mad showing a template!
Quote:
Originally Posted by Paul McKenzie
Please be courteous and tell us how you solved the problem.
Boards such as CodeGuru are designed to be searched by others who may have the same problem and expect to find the solutions here -- not by sending an email to someone.
Regards,
Paul McKenzie
I didn't post the solution because imho is an horrible solution and sincerely I think someone more skilled could do better.
But if someone wanna get it work without care of the method, you're right.
I created a DLGTEMPLATEEX structure and i filled it with my data (font, styles, etc..) then i called the
dialog.CreateIndirect(&structpointer, NULL, NULL);
(notice dialog is a CDialog var)
function to create a window (sorry, could not get something better). I didn't specify the WS_VISIBLE flag so the window did not get shown and then I could use MapDialogRect.
Then destroy the window :/
Pretty messy huh?
Re: Getting mad showing a template!
Quote:
I need to calculate them in REAL coordinates (pixel)
Then why don't you read MSDN? :confused:
Quote:
LOGFONT
lfHeight
For the MM_TEXT mapping mode, you can use the following formula to specify a height for a font with a specified point size:
lfHeight = -MulDiv(PointSize, GetDeviceCaps(hDC, LOGPIXELSY), 72);
Re: Getting mad showing a template!
That's the font height. Not the average height. And i need the avg width too. And I need then to call a MapDialogRect to size a rect properly.
Re: Getting mad showing a template!
Yep, that's the font height. :) You have to set correct font before doing GetTextExtent, and that's why you need the font height.
Quote:
Why coordinates I get are totally WRONG?
I find this description impressive, but... Would you give us some definition in technical terms?