如何给combo控件添加自定义图片?(已解决)
本帖最后由 nmgwddj 于 2011-11-30 13:56 编辑如下图,可以将自己的jpg或者bmp图片显示在combo控件上。我自己试了试_GUIImageList_AddBitmap还是添加不上,论坛搜索了以下貌似没有类似例子。
这个貌似需要加上 $CBS_OWNERDRAWVARIABLE 等样式,自绘 Combo~
比较麻烦 回复 2# afan
今天我自己做做试试 #include <GUIComboBox.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <FontConstants.au3>
#include <WinAPI.au3>
Global Const $ODT_MENU = 1
Global Const $ODT_LISTBOX = 2
Global Const $ODT_COMBOBOX = 3
Global Const $ODT_BUTTON = 4
Global Const $ODT_STATIC = 5
Global Const $ODT_HEADER = 100
Global Const $ODT_TAB = 101
Global Const $ODT_LISTVIEW = 102
Global Const $ODA_DRAWENTIRE = 1
Global Const $ODA_SELECT = 2
Global Const $ODA_FOCUS = 4
Global Const $ODS_SELECTED = 1
Global Const $ODS_GRAYED = 2
Global Const $ODS_DISABLED = 4
Global Const $ODS_CHECKED = 8
Global Const $ODS_FOCUS = 16
Global Const $ODS_DEFAULT = 32
Global Const $ODS_HOTLIGHT = 64
Global Const $ODS_INACTIVE = 128
Global Const $ODS_NOACCEL = 256
Global Const $ODS_NOFOCUSRECT = 512
Global Const $ODS_COMBOBOXEDIT = 4096
Global Const $clrWindowText = _WinAPI_GetSysColor($COLOR_WINDOWTEXT)
Global Const $clrHighlightText = _WinAPI_GetSysColor($COLOR_HIGHLIGHTTEXT)
Global Const $clrHighlight = _WinAPI_GetSysColor($COLOR_HIGHLIGHT)
Global Const $clrWindow = _WinAPI_GetSysColor($COLOR_WINDOW)
Global Const $tagDRAWITEMSTRUCT = _
'uint CtlType;' & _
'uint CtlID;' & _
'uint itemID;' & _
'uint itemAction;' & _
'uint itemState;' & _
'hwnd hwndItem;' & _
'hwnd hDC;' & _
$tagRECT & _
';ulong_ptr itemData;'
Global Const $tagMEASUREITEMSTRUCT = _
'uint CtlType;' & _
'uint CtlID;' & _
'uint itemID;' & _
'uint itemWidth;' & _
'uint itemHeight;' & _
'ulong_ptr itemData;'
Global $hGUI
Global $ComboBox
Global $hBrushNorm = _WinAPI_CreateSolidBrush($clrWindow)
Global $hBrushSel = _WinAPI_CreateSolidBrush($clrHighlight)
Global $hBrushes =
For $i = 1 To UBound($hBrushes) - 1
$hBrushes[$i] = _WinAPI_CreateSolidBrush(Random(0, 16777215, 1))
Next
GUIRegisterMsg($WM_MEASUREITEM, '_WM_MEASUREITEM')
GUIRegisterMsg($WM_DRAWITEM, '_WM_DRAWITEM')
$hGUI = GUICreate('Test', 220, 300)
$ComboBox = GUICtrlCreateCombo('', 10, 10, 200, 300, BitOR($WS_CHILD, $CBS_OWNERDRAWVARIABLE, $CBS_HASSTRINGS, $CBS_DROPDOWNLIST, $CBS_AUTOHSCROLL, $__COMBOBOXCONSTANT_WS_VSCROLL))
GUICtrlSetData($ComboBox, "Auto|||||||||||||||||||")
GUISetState()
Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE
For $i = 0 To UBound($hBrushes) - 1
_WinAPI_DeleteObject($hBrushes[$i])
Next
_WinAPI_DeleteObject($hBrushSel)
_WinAPI_DeleteObject($hBrushNorm)
GUIDelete()
Func _WM_MEASUREITEM($hWnd, $iMsg, $iwParam, $ilParam)
Local $tMIS = DllStructCreate($tagMEASUREITEMSTRUCT, $ilParam)
Local $iCtlType, $iCtlID, $iItemID, $iItemWidth, $iItemHeight
Local $hComboBox
Local $tSize
Local $sText
$iCtlType = DllStructGetData($tMIS, 'CtlType')
$iCtlID = DllStructGetData($tMIS, 'CtlID')
$iItemID = DllStructGetData($tMIS, 'itemID')
$iItemWidth = DllStructGetData($tMIS, 'itemWidth')
$iItemHeight = DllStructGetData($tMIS, 'itemHeight')
$hComboBox = GUICtrlGetHandle($iCtlID)
If $iCtlType = $ODT_COMBOBOX Then DllStructSetData($tMIS, 'itemHeight', 50)
Return $GUI_RUNDEFMSG
EndFunc ;==>_WM_MEASUREITEM
Func _WM_DRAWITEM($hWnd, $iMsg, $iwParam, $ilParam)
Local $tDIS = DllStructCreate($tagDRAWITEMSTRUCT, $ilParam)
Local $iCtlType, $iCtlID, $iItemID, $iItemAction, $iItemState
Local $clrForeground, $clrBackground
Local $hWndItem, $hDC, $hOldPen, $hOldBrush
Local $tRect, $aRect
Local $sText
$iCtlType = DllStructGetData($tDIS, 'CtlType')
$iCtlID = DllStructGetData($tDIS, 'CtlID')
$iItemID = DllStructGetData($tDIS, 'itemID')
$iItemAction = DllStructGetData($tDIS, 'itemAction')
$iItemState = DllStructGetData($tDIS, 'itemState')
$hWndItem = DllStructGetData($tDIS, 'hwndItem')
$hDC = DllStructGetData($tDIS, 'hDC')
$tRect = DllStructCreate($tagRECT)
If $iCtlType = $ODT_COMBOBOX Then
Switch $iCtlID
Case $ComboBox
For $i = 1 To 4
DllStructSetData($tRect, $i, DllStructGetData($tDIS, $i + 7))
$aRect[$i - 1] = DllStructGetData($tRect, $i)
Next
_GUICtrlComboBox_GetLBText($hWndItem, $iItemID, $sText)
If BitAND($iItemState, $ODS_SELECTED) Then
$clrForeground = _WinAPI_SetTextColor($hDC, $clrHighlightText)
$clrBackground = _WinAPI_SetBkColor($hDC, $clrHighlight)
_WinAPI_FillRect($hDC, DllStructGetPtr($tRect), $hBrushSel)
Else
$clrForeground = _WinAPI_SetTextColor($hDC, $clrWindowText)
$clrBackground = _WinAPI_SetBkColor($hDC, $clrWindow)
_WinAPI_FillRect($hDC, DllStructGetPtr($tRect), $hBrushNorm)
EndIf
_WinAPI_SetBkMode($hDC, $TRANSPARENT)
$hFont = _WinAPI_CreateFont(50, 0, 0, 0, 400, False, False, False, $DEFAULT_CHARSET, _
$OUT_DEFAULT_PRECIS, $CLIP_DEFAULT_PRECIS, $DEFAULT_QUALITY, 0, 'Arial')
$hOldFont = _WinAPI_SelectObject($hDC, $hFont)
If $sText <> "" Then
_WinAPI_DrawText($hDC, $sText, $tRect, BitOR($DT_CENTER, $DT_VCENTER))
Else
If $iItemID <> 4294967295 Then
DllStructSetData($tRect, "Left", $aRect + 10)
DllStructSetData($tRect, "Top", $aRect + 2)
DllStructSetData($tRect, "Right", $aRect - 10)
DllStructSetData($tRect, "Bottom", $aRect - 2)
_WinAPI_FillRect($hDC, DllStructGetPtr($tRect), $hBrushes[$iItemID])
_WinAPI_FrameRect($hDC, DllStructGetPtr($tRect), $hBrushes)
_WinAPI_DrawText($hDC, "Test", $tRect, BitOR($DT_CENTER, $DT_VCENTER))
Else
_WinAPI_FillRect($hDC, DllStructGetPtr($tRect), $hBrushes)
EndIf
EndIf
_WinAPI_SetTextColor($hDC, $clrForeground)
_WinAPI_SetBkColor($hDC, $clrBackground)
_WinAPI_SetBkMode($hDC, $TRANSPARENT)
EndSwitch
EndIf
Return $GUI_RUNDEFMSG
EndFunc ;==>_WM_DRAWITEM
我得先看看,够我消化一段时间了,不要笑话我,不好意思{:face (396):} 学习了,谢谢!!! 回复 4# 3mile
学习了,正在学自绘 可以使用bmp图片(或转成bmp图片),然后 _GUIImageList_AddBitmap() 就不用自绘了,也简单多了~ 用了皮肤估计也不起作用吧? 回复 8# afan
果断可以了,因为之前一系列错乱,竟然 @ScriptDir & '\Test.bmp' 里面少了个“\”。导致之前实验不成功。
不过还是很感谢3mile的列子!
#include <GuiComboBoxEx.au3>
#include <GuiImageList.au3>
#include <GuiConstantsEx.au3>
Opt('MustDeclareVars', 1)
$Debug_CB = False ; 检查传递给ComboBox/ComboBoxEx函数的类名, 设置为真并使用另一控件的句柄查看其运行
Global $iMemo
_Main()
Func _Main()
Local $hGUI, $hImage, $hCombo
; 创建界面
$hGUI = GUICreate("ComboBoxEx Image List", 400, 300)
$hCombo = _GUICtrlComboBoxEx_Create($hGUI, "", 2, 2, 394, 100)
GUICtrlSetFont($iMemo, 9, 400, 0, "Courier New")
GUISetState()
$hImage = _GUIImageList_Create(40, 40, 5, 3)
_GUIImageList_AddIcon($hImage, @SystemDir & "\shell32.dll", 110)
_GUIImageList_AddBitmap($hImage, @ScriptDir & '\Test.bmp')
; 设置图像列表
_GUICtrlComboBoxEx_SetImageList($hCombo, $hImage)
For $x = 0 To 1
_GUICtrlComboBoxEx_AddString($hCombo, StringFormat("%03d : Random string", Random(1, 100, 1)), $x, $x)
Next
Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE
EndFunc ;==>_Main
页:
[1]