Marc Posted August 23, 2016 Share Posted August 23, 2016 (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 August 23, 2016 by Marc solved Any of my own codes posted on the forum are free for use by others without any restriction of any kind. (WTFPL) Link to comment Share on other sites More sharing options...
AutoBert Posted August 23, 2016 Share Posted August 23, 2016 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 Link to comment Share on other sites More sharing options...
Marc Posted August 23, 2016 Author Share Posted August 23, 2016 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... Any of my own codes posted on the forum are free for use by others without any restriction of any kind. (WTFPL) Link to comment Share on other sites More sharing options...
funkey Posted August 23, 2016 Share Posted August 23, 2016 #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 Marc 1 Programming today is a race between software engineers striving tobuild bigger and better idiot-proof programs, and the Universetrying to produce bigger and better idiots.So far, the Universe is winning. Link to comment Share on other sites More sharing options...
Marc Posted August 23, 2016 Author Share Posted August 23, 2016 wow... cool Many thanks! Any of my own codes posted on the forum are free for use by others without any restriction of any kind. (WTFPL) Link to comment Share on other sites More sharing options...
funkey Posted August 23, 2016 Share Posted August 23, 2016 Font weight 700 is thinner than weight 600. And the function always gives me 700. Can anybody tell me why this is so? expandcollapse popup#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 tobuild bigger and better idiot-proof programs, and the Universetrying to produce bigger and better idiots.So far, the Universe is winning. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now