Re: Win32Lib: any way to GET the font size?

new topic     » goto parent     » topic index » view thread      » older message » newer message

CChris wrote:
> 
> Dan Moyer wrote:
> > 
> > don cole wrote:
> > > 
> > > Dan Moyer wrote:
> > > > 
> > > > don cole wrote:
> > > > > 
> > > > > 
> > > > > Dan Moyer wrote:
> > > > > > 
> > > > > > Is there some way to get the font SIZE?  > > 

<<<<snip>>>>

> > Dan
> 
> The following appears to work. It reverses what convPointsToLogical() does.
> 
> }}}
<eucode>
> include win32lib.ew
> constant
>    Err_ECFSAVEDCFAIL = {"ezCreateFont:SaveDC failed.", 491},
>    Err_ECFRESTOREDC = {"ezCreateFont:RestoreDC failed.", 486},
>   
>    xGetCurrentObject=registerw32Function(gdi32,"GetCurrentObject",{C_POINTER,C_LONG},C_POINTER),
>   
>    xLPtoDP=registerw32Function(gdi32,"LPtoDP",{C_POINTER,C_POINTER,C_LONG},C_LONG)
> 
> global function getFontPointSize(object pDC)
>     integer pointSize
>     atom lDC
>     atom lSavedDC,currentFont
>     atom lDpiY
>     atom lPoint,logFont
> 
>     -- save the current device context because I'm about to
>     -- possibly mess it up.
> 
>     if atom(pDC) then
>         -- Then a control ID was passed
>         lDC = getDC(pDC)
>     elsif length(pDC)>1 then
>         -- Then a raw window handle is at position 1
>         lDC=w32Func(xGetDC,{pDC[1]})
>     else
>         -- Else a real dc was passed
>         lDC = pDC[1]
>     end if
> 
>     lSavedDC = w32Func( xSaveDC, {lDC} )
>     if lSavedDC = 0 then
>         abortErr( Err_ECFSAVEDCFAIL )
>     end if
> 
>     -- these are NT specific. they should have no effect in Win95
>     VOID = w32Func( xSetGraphicsMode, {lDC, GM_ADVANCED} )
>     VOID = w32Func( xModifyWorldTransform, {lDC, NULL, MWT_IDENTITY} )
> 
>     VOID = w32Func( xSetViewportOrgEx, {lDC, 0, 0, NULL} )
>     VOID = w32Func( xSetWindowOrgEx,   {lDC, 0, 0, NULL} )
> 
>     lDpiY = w32Func( xGetDeviceCaps, {lDC, LOGPIXELSY} )
> 
>     -- w32allot
>     lPoint = w32acquire_mem( 0, SIZEOF_POINT )
>     currentFont = w32Func(xGetCurrentObject,{lDC,6}) -- OBJ_FONT
>     logFont = w32acquire_mem(0,SIZEOF_LOGFONT)
>     VOID = w32Func(xGetObject,{currentFont,SIZEOF_LOGFONT,logFont})
>     w32store( lPoint,ptY, peek4u(logFont))
>     VOID = w32Func( xLPtoDP, {lDC, lPoint, 1} )
>     pointSize = w32fetch( lPoint,ptY )
>     w32release_mem(lPoint)
>     w32release_mem(logFont)
>     pointSize = floor(pointSize*(72.0/lDpiY)+0.5)
> 
>     -- restore the device context
>     if not w32Func( xRestoreDC, {lDC, lSavedDC} ) then
>         abortErr( Err_ECFRESTOREDC )
>     end if
> 
>     if atom(pDC) then
>         releaseDC( pDC )
>     end if
> 
>     return pointSize
> 
> end function
> 
> constant w=create(Window,"taaaa",0,50,50,50,50,0)
> procedure p(integer id,integer event,sequence data)
> ?getFontPointSize(w)
> end procedure -- prints 12 here, and the floor() isn't really needed
> 
> setHandler(w,w32HActivate,routine_id("p"))
> 
> WinMain(w,Normal)
> </eucode>
{{{

> 
> CChris
> 
> PS: you can avoid calling GetCurrentObject() by sending the control the
>  WM_GETFONT message. However, if you only have a DC, you'll have to retrieve
>  the control handle using WindowFromDC().

Thanks CChris!  Looks like something that might be good to add to Win32Lib?
If only for symmetry?

Right now I'm working on keeping better track of what the font size is
(and changes to) during app use, so I can just use that value as needed,
but if that becomes too tedious, I'll just use your routine instead! :)

Dan

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu