Jump to content

ComboBox with Icon and other font


Go to solution Solved by HurleyShanabarger,

Recommended Posts

Hello,

I want to use a ComboBox with icons and set the font for the whole combobox, but I it is not working as expected. I created the function _GUICtrlSetFont and added the same parameters as for the standard GUICtrlSetFont function. When the script is run, the size differs - but the same "size" is taken for the function. Can someone explain this?

 

#NoTrayIcon
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
    #Tidy_Parameters=/reel /sf /ri
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

#Region Includes
    #include <GuiComboBoxEx.au3>
    #include <GuiImageList.au3>
    #include <WinAPIGdi.au3>
    #include <FontConstants.au3>
    #include <GUIConstantsEx.au3>
    #include <WindowsConstants.au3>
#EndRegion Includes

#Region Variables/Opt
    Opt("MustDeclareVars", 1)
#EndRegion Variables/Opt

#Region Main
    _Main()

    Func _Main()
        ; GUI
        Local $hGUI = GUICreate("Test", 600, 120)
        Local $hCombo_Udf = _GUICtrlComboBox_Create($hGUI, "", 5, 5, 150, 150, $CBS_DROPDOWNLIST)
        Local $hCombo_Ex = _GUICtrlComboBoxEx_Create($hGUI, "", 160, 5, 150, 150, $CBS_DROPDOWNLIST)
        Local $hCombo_Std = GUICtrlCreateCombo("", 315, 5, 150, 150, $CBS_DROPDOWNLIST)
        GUICtrlCreateButton("Dummy", 5, 50, 120, 35)

        ; Font
        Local $iSize = 16
        Local $iWeight = 0
        Local $sgFont = "Consolas"
        Local $iAttrib = 0
        _GUICtrlSetFont($hCombo_Udf, $iSize, $iWeight, $iAttrib, $sgFont)
        _GUICtrlSetFont($hCombo_Ex, $iSize, $iWeight, $iAttrib, $sgFont)
        GUICtrlSetFont($hCombo_Std, $iSize, $iWeight, $iAttrib, $sgFont, $ANTIALIASED_QUALITY)

        ; ImageList
        Local $hImage = _GUIImageList_Create(15, 15, 5, 3)
        _GUIImageList_AddIcon($hImage, @SystemDir & "\shell32.dll", 110)
        _GUIImageList_AddIcon($hImage, @SystemDir & "\shell32.dll", 131)
        _GUIImageList_AddIcon($hImage, @SystemDir & "\shell32.dll", 165)
        _GUIImageList_AddIcon($hImage, @SystemDir & "\shell32.dll", 168)
        _GUIImageList_AddIcon($hImage, @SystemDir & "\shell32.dll", 137)
        _GUIImageList_AddIcon($hImage, @SystemDir & "\shell32.dll", 146)

        ; ComboBoxUdf
        _GUICtrlComboBox_BeginUpdate($hCombo_Udf)
        _GUICtrlComboBox_AddString($hCombo_Udf, "Item 001")
        _GUICtrlComboBox_AddString($hCombo_Udf, "Item 002")
        _GUICtrlComboBox_AddString($hCombo_Udf, "Item 003")
        _GUICtrlComboBox_AddString($hCombo_Udf, "Item 004")
        _GUICtrlComboBox_AddString($hCombo_Udf, "Item 005")
        _GUICtrlComboBox_AddString($hCombo_Udf, "Item 006")
        _GUICtrlComboBox_SetCurSel($hCombo_Udf, 1)
        _GUICtrlComboBox_EndUpdate($hCombo_Udf)

        ; ComboBoxEx
        _GUICtrlComboBoxEx_BeginUpdate($hCombo_Ex)
        _GUICtrlComboBoxEx_SetImageList($hCombo_Ex, $hImage)
        _GUICtrlComboBoxEx_AddString($hCombo_Ex, "Item 001", 0, 0)
        _GUICtrlComboBoxEx_AddString($hCombo_Ex, "Item 002", 1, 1)
        _GUICtrlComboBoxEx_AddString($hCombo_Ex, "Item 003", 2, 2)
        _GUICtrlComboBoxEx_AddString($hCombo_Ex, "Item 004", 3, 3)
        _GUICtrlComboBoxEx_AddString($hCombo_Ex, "Item 005", 4, 4)
        _GUICtrlComboBoxEx_AddString($hCombo_Ex, "Item 006", 5, 5)
        _GUICtrlComboBoxEx_SetCurSel($hCombo_Ex, 1)
        _GUICtrlComboBoxEx_EndUpdate($hCombo_Ex)

        ; ComboBox
        GUICtrlSetData($hCombo_Std, "Item 001|Item 002|Item 003|Item 004|Item 005|Item 006", "Item 002")

        GUISetState()
        Do
        Until GUIGetMsg() = $GUI_EVENT_CLOSE
    EndFunc   ;==>_Main
#EndRegion Main

#Region Functions
    Func _GUICtrlSetFont($p_hControl, $p_iHeight = Default, $p_iWeight = Default, $p_iAttribute = Default, $p_sgFont = "Arial")
        ; Set values
        If $p_iHeight = Default Then $p_iHeight = 8.5
        If $p_iWeight = Default Then $p_iWeight = 400
        If $p_iAttribute = Default Then $p_iAttribute = 0
        Local $boItalic = BitAND($p_iAttribute, 2) <> 0
        Local $boUnder = BitAND($p_iAttribute, 4) <> 0
        Local $boStrike = BitAND($p_iAttribute, 8) <> 0

        ; Assign font
        Local $hFnt = _WinAPI_CreateFont($p_iHeight, 0, 0, 0, 400, $boItalic, $boUnder, $boStrike, $DEFAULT_CHARSET, $OUT_DEFAULT_PRECIS, $CLIP_DEFAULT_PRECIS, $ANTIALIASED_QUALITY, 0, $p_sgFont)
        _WinAPI_SetFont($p_hControl, $hFnt, True)
;~      _WinAPI_DeleteObject($hFnt)
    EndFunc   ;==>_GUICtrlSetFont
#EndRegion Functions

 

Link to comment
Share on other sites

  • Solution

So, after searching around the forum and looking at MSDN I realized, that GUICtrlSetFont takes the font in points, whereas _WinAPI_CreateFont takes the font in logical units. However I found two possible approaches:

  • Either using GDI+ to calculate/measure the heigth of the fonts from given points
  • or using the formula given on the MSDN entry
    • nHeight = -MulDiv(PointSize, GetDeviceCaps(hDC, LOGPIXELSY), 72);

As the solution with DC is slight more accurate (or similar) to GuiCtrlSetFont I am using this as solution for now:

Func _GUICtrlSetFont($p_hControl = Default, $p_iHeight = Default, $p_iWeight = Default, $p_iAttribute = Default, $p_sgFont = "Tahoma")
        ; Set values
        If $p_iHeight = Default Then $p_iHeight = 8.5
        If $p_iWeight = Default Then $p_iWeight = 400
        If $p_iAttribute = Default Then $p_iAttribute = 0
        Local $boItalic = (BitAND($p_iAttribute, 2) <> 0)
        Local $boUnder = (BitAND($p_iAttribute, 4) <> 0)
        Local $boStrike = (BitAND($p_iAttribute, 8) <> 0)
        Local $iHeight = $p_iHeight

        ; Convert point height to logical unit
        Local $hDC = _WinAPI_GetDC(0)
        Local $_iCap = _WinAPI_GetDeviceCaps($hDC, 90)
        _WinAPI_ReleaseDC(0, $hDC)
        $iHeight = -1 * _WinAPI_MulDiv($p_iHeight, $_iCap, 72)

        ; Assign font
        Local $hFnt = _WinAPI_CreateFont($iHeight, 0, 0, 0, $p_iWeight, $boItalic, $boUnder, $boStrike, $DEFAULT_CHARSET, $OUT_DEFAULT_PRECIS, $CLIP_DEFAULT_PRECIS, $PROOF_QUALITY, 0, $p_sgFont)
        _WinAPI_SetFont($p_hControl, $hFnt, True)
    EndFunc   ;==>_GUICtrlSetFont_Dc

It was also imported to not use  

$ANTIALIASED_QUALITY

but  

$PROOF_QUALITY

.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...