Sign in to follow this  
Followers 0
Marc

get font weight of a label [solved]

6 posts in this topic

#1 ·  Posted (edited)

Hi all,

is there any way to get the font weight of a label? Setting the font weight is easy going (GUICtrlSetFont), but reading it is not so much fun.

Background (what I want to achieve): in my tools, there are lots of labels. The User shall be able to copy the label-text by clicking on it. But I want to differ between the static labels (the description-labels) and the labels containing the useful informations. The latter ones are bold (font weight 800), the other ones are not.

Example:

#include <GUIConstants.au3>

$Form1 = GUICreate("Test", 250, 50, -1, -1)
GUICtrlCreateLabel("Username:", 10, 10, 90, 17)
$lbl_name = GUICtrlCreateLabel("Jane Doe", 100, 10, 90, 17)
GUICtrlSetFont(-1, -1, 800)
GUISetState(@SW_SHOW, $Form1)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case -100 To 0
            ; ignore
        Case Else
            $tmp = GUICtrlRead($nMsg)
            ClipPut($tmp)
            MsgBox(0, "Text has been copied", $tmp)
    EndSwitch
    Sleep(50)
WEnd

In this form, I want the text of $lbl_name to get copied when the label is clicked on, but when clicking the label without a variable, nothing shall happen.

Of course, I could throw each and every $lbl-variable into an separate Case-segment in the switch-statement. But I am looking for a more lazy generic solution, what brought me to the idea of "if _labelfontwidth($nMsg)<800 then Continueloop".

Any ideas? :)

Edited by Marc
solved

It's my job to comfort the disturbed and to disturb the comfortable.

Share this post


Link to post
Share on other sites



Delete the sleep in your loop, it isn't neccesary. Have a look at:

Quote

_WinAPI_GetFontName

Retrieves the unique name of the font based on its typeface name, character set, and style

 

Share this post


Link to post
Share on other sites

Hi Autobert,

sorry, but I don't get your point here?

I don't want to get the unique font name, I want to know whether a specific label is bold or not.

Perhaps I am just missing the trick...


It's my job to comfort the disturbed and to disturb the comfortable.

Share this post


Link to post
Share on other sites
#include <GUIConstants.au3>
#include <SendMessage.au3>
#include <WindowsConstants.au3>
#include <WinAPI.au3>


$Form1 = GUICreate("Test", 250, 50, -1, -1)
GUICtrlCreateLabel("Username:", 10, 10, 90, 17)
$lbl_name = GUICtrlCreateLabel("Jane Doe", 100, 10, 90, 17)
GUICtrlSetFont(-1, -1, 800)
GUISetState(@SW_SHOW, $Form1)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case -100 To 0
            ; ignore
        Case Else
            ConsoleWrite(_LabelGetFontWeight($nMsg) & @LF)
    EndSwitch
    Sleep(50)
WEnd

Func _LabelGetFontWeight($nID)
    Local $hCtrl = GUICtrlGetHandle($nID)
    Local $hDC = _WinAPI_GetDC($hCtrl) ; Get the device context handle of the current window.
    Local $hFont = _SendMessage($hCtrl, $WM_GETFONT) ; Retrieve the font with which the control is currently drawing its text.
    Local $hSelectObject = _WinAPI_SelectObject($hDC, $hFont) ; Select the object of the context device.

    Local $tTextMetric = _WinAPI_GetTextMetrics($hDC)
    _WinAPI_SelectObject($hDC, $hSelectObject)
    _WinAPI_ReleaseDC($hCtrl, $hDC) ; Release the device context.
    Return DllStructGetData($tTextMetric, "tmWeight")
EndFunc

 

1 person likes this

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Share this post


Link to post
Share on other sites

wow... cool :drool:

Many thanks!


It's my job to comfort the disturbed and to disturb the comfortable.

Share this post


Link to post
Share on other sites

Font weight 700 is thinner than weight 600. And the function always gives me 700.

Can anybody tell me why this is so?

 

#include <GUIConstants.au3>
#include <SendMessage.au3>
#include <WindowsConstants.au3>
#include <WinAPI.au3>


$Form1 = GUICreate("Test", 250, 150, -1, -1)
GUICtrlCreateLabel("Weight 400", 10, 10, 90, 17)
GUICtrlCreateLabel("Weight 800", 100, 10, 90, 17)
GUICtrlSetFont(-1, -1, 800)

$lbl_name = GUICtrlCreateLabel("Weight 600", 100, 30, 90, 17)
GUICtrlSetFont(-1, -1, 600)

$lbl_name = GUICtrlCreateLabel("Weight 700", 100, 50, 90, 17)
GUICtrlSetFont(-1, -1, 700)

GUISetState(@SW_SHOW, $Form1)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case -100 To 0
            ; ignore
        Case Else
            ConsoleWrite( "ID: " & $nMsg & " - Weight: " &_LabelGetFontWeight($nMsg) & @LF)
    EndSwitch
WEnd

Func _LabelGetFontWeight($nID)
    Local $hCtrl = GUICtrlGetHandle($nID)
    Local $hDC = _WinAPI_GetDC($hCtrl) ; Get the device context handle of the current window.
    Local $hFont = _SendMessage($hCtrl, $WM_GETFONT) ; Retrieve the font with which the control is currently drawing its text.
    Local $hSelectObject = _WinAPI_SelectObject($hDC, $hFont) ; Select the object of the context device.

    Local $tTextMetric = _WinAPI_GetTextMetrics($hDC)
    _WinAPI_SelectObject($hDC, $hSelectObject)
    _WinAPI_ReleaseDC($hCtrl, $hDC) ; Release the device context.
    Return DllStructGetData($tTextMetric, "tmWeight")
EndFunc

 


Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0