CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jul 2007
    Posts
    52

    [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
    Last edited by NasterMain; March 29th, 2008 at 07:48 AM. Reason: Problem Solved

  2. #2
    Join Date
    May 1999
    Location
    ALABAMA, USA
    Posts
    9,917

    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?
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  3. #3
    Join Date
    Jul 2007
    Posts
    52

    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

  4. #4
    Join Date
    Jul 2007
    Posts
    52

    Re: Getting mad showing a template!

    Problem solved by myself, if anyone has the same problem, contact me.

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    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

  6. #6
    Join Date
    Jul 2007
    Posts
    52

    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?

  7. #7
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: Getting mad showing a template!

    I need to calculate them in REAL coordinates (pixel)
    Then why don't you read MSDN?
    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);
    Last edited by Igor Vartanov; March 30th, 2008 at 11:14 AM.
    Best regards,
    Igor

  8. #8
    Join Date
    Jul 2007
    Posts
    52

    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.

  9. #9
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    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.

    Why coordinates I get are totally WRONG?
    I find this description impressive, but... Would you give us some definition in technical terms?
    Best regards,
    Igor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured