The MASM Forum

General => The Campus => Topic started by: xandaz on November 04, 2021, 08:01:35 AM

Title: Correct font size from LOGFONT
Post by: xandaz on November 04, 2021, 08:01:35 AM
    hey guys. I'm using ChooseFont Common-Dialog and can't get the size to match the size shown in the dialog. I found this in documentation:lfHeight = -MulDiv(PointSize, GetDeviceCaps(hDC, LOGPIXELSY), 72)
...and coded: invoke GetDC,hWnd
mov hDC,eax
invoke GetDeviceCaps,eax,LOGPIXELSY
mov ebx,chFont.iPointSize
xor edx,edx
mul ebx
mov ebx,72
xor edx,edx
div ebx

...but still the sizes don't match the size shown in the Common Dialog. How to get the correct size for the font? Has it got something to do with the remainder members of LOGFONT? Cause i'm absolutelly certain that if i use CreateFontIndirect and put 16 for the height the font will come out ina regular size. Thanks in advance.
Title: Re: Correct font size from LOGFONT
Post by: fearless on November 04, 2021, 09:32:47 AM
Might need to negate the value

https://github.com/mrfearless/ModernUI/blob/master/ModernUI/MUIPointSizeToLogicalUnit.asm

Also windows might interfere with fonts and dialog sizes when scaling - if your have a high dpi resolution monitor
Title: Re: Correct font size from LOGFONT
Post by: xandaz on November 04, 2021, 06:39:04 PM
    Nice tip fearless but it didn't help much. It's better to find some number rather than 72.
Title: Re: Correct font size from LOGFONT
Post by: daydreamer on November 04, 2021, 09:11:43 PM
Quote from: xandaz on November 04, 2021, 06:39:04 PM
    Nice tip fearless but it didn't help much. It's better to find some number rather than 72.
Put it as variable instead of immediate 72
And loop thru several sizes and drawtext to test?
For example if the output is size 32 instead of size 16, modify div constant accordingly
Title: Re: Correct font size from LOGFONT
Post by: xandaz on November 05, 2021, 12:17:31 AM
    Thanks DayDreamer. I'll do just that.
Title: Re: Correct font size from LOGFONT
Post by: Greenhorn on November 05, 2021, 09:38:57 AM
Hi xandaz,

try this (logical to points):
      invoke   GetDC,hWnd
      mov   hDC,eax
      invoke   GetDeviceCaps,eax,LOGPIXELSY
      mov   ebx,72
      xor   edx,edx
      mul   ebx
      mov   ebx,chFont.iPointSize
      xor   edx,edx
      div   ebx


Edit: This is mistaken.

Kind regards
Greenhorn
Title: Re: Correct font size from LOGFONT
Post by: Greenhorn on November 06, 2021, 11:50:09 AM
QuoteiPointSize

Type: INT

The size of the selected font, in units of 1/10 of a point. The ChooseFont function sets this value after the user closes the dialog box.


      mov   eax,chFont.iPointSize
      xor   edx,edx
      div, 10
      mov ebx, eax

      invoke   GetDC,hWnd
      mov   hDC,eax
      invoke   GetDeviceCaps,eax,LOGPIXELSY
      xor   edx,edx
      mul   ebx
      mov   ebx,72
      xor   edx,edx
      div   ebx
Title: Re: Correct font size from LOGFONT
Post by: xandaz on November 07, 2021, 03:20:27 AM
   Thanks Greenhorn. I'll look into it.
Title: Re: Correct font size from LOGFONT
Post by: xandaz on November 07, 2021, 05:42:17 AM
    Greenhorn. Your code doesnt work. The characters get too small. Thanks anyway.
Title: Re: Correct font size from LOGFONT
Post by: Greenhorn on November 07, 2021, 12:59:00 PM
It's not clear to me what you want to achieve.

Typically you use the ChooseFont dialog to create a font from the chFont.lpLogFont member just by using CreateFontIndirect.
So for what do you need chFont.iPointSize ?
Title: Re: Correct font size from LOGFONT
Post by: xandaz on November 07, 2021, 09:25:21 PM
   i was trying to match the output font size to match the font size in the ChooseFont common dialog. Thanks
Title: Re: Correct font size from LOGFONT
Post by: xandaz on November 09, 2021, 08:54:56 AM
    I'm using CHARFORMAT. Not WM_SETFONT. So i wanted to match CHARFORMAT.yHeight with size chosen in ChooseFont Dialog box. Still in trouble.
Thanks.
Title: Re: Correct font size from LOGFONT
Post by: xandaz on November 10, 2021, 08:00:48 AM
    For furher notice:            invoke  GetDeviceCaps,chFont.hDC,LOGPIXELSY
            invoke   MulDiv,chFont.iPointSize,eax,72
     neg eax
     shl eax,1

    Works just fine.