【已解决】绘制COMBO,如何让收起的时候背景为白色
本帖最后由 862228699 于 2017-6-4 15:08 编辑目前这个COMBO是通过绘制,我也是朋友给我的一个源码。现在遇到不满意的地方。
运行代码,不进行任何操作的时候,COMBO收起的时候,背景色和,展开鼠标划过背景一样。
我想实现的是,不管任何选择,或者运行代码不进行选择,背景色为白色。
#include <GUIComboBox.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <WinAPI.au3>
#include <WinAPIFiles.au3>;含RGB转换BGR颜色UDF
Global $iColor = 0x114c93;选中颜色
Global $clrWindow = 0xffffff;底色
Global $hBrushNorm = _WinAPI_CreateSolidBrush($clrWindow)
Global $hBrushSel = _WinAPI_CreateSolidBrush(_WinAPI_SwitchColor($iColor))
Global $hPen = _WinAPI_CreatePen($PS_SOLID, 2, _WinAPI_SwitchColor($iColor))
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))
GUICtrlSetData($ComboBox, "实现这里背景为白色|苹果|香蕉|桔子|梨子|李子|西瓜|桔子|梨子|李子|西瓜|桔子|梨子|李子|西瓜", "实现这里背景为白色")
GUICtrlSetFont(-1, 12, 400, -1)
GUISetState()
Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE
_WinAPI_DeleteObject($hPen)
_WinAPI_DeleteObject($hBrushSel)
_WinAPI_DeleteObject($hBrushNorm)
GUIDelete()
Func _WM_MEASUREITEM($hWnd, $iMsg, $iwParam, $ilParam)
Local $tagMEASUREITEMSTRUCT = 'uint CtlType;uint CtlID;uint itemID;uint itemWidth;uint itemHeight;ulong_ptr itemData;'
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 = 3 Then DllStructSetData($tMIS, 'itemHeight', 30)
Return $GUI_RUNDEFMSG
EndFunc ;==>_WM_MEASUREITEM
Func _WM_DRAWITEM($hWnd, $iMsg, $iwParam, $ilParam)
Local $tagDRAWITEMSTRUCT = 'uint CtlType;uint CtlID;uint itemID;uint itemAction;uint itemState;hwnd hwndItem;hwnd hDC;' & $tagRECT & ';ulong_ptr itemData;'
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 = 3 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, 1) Then
$hOldBrush = _WinAPI_SelectObject($hDC, $hBrushSel)
$hOldPen = _WinAPI_SelectObject($hDC, $hPen)
_WinAPI_Rectangle($hDC, $aRect + 1, $aRect + 1, $aRect, $aRect)
_WinAPI_SelectObject($hDC, $hOldPen)
_WinAPI_SelectObject($hDC, $hOldBrush)
$clrBackground = _WinAPI_SetBkColor($hDC, _WinAPI_SwitchColor($iColor))
Else
$clrBackground = _WinAPI_SetBkColor($hDC, $clrWindow)
_WinAPI_FillRect($hDC, DllStructGetPtr($tRect), $hBrushNorm)
EndIf
DllStructSetData($tRect, "Left", $aRect + 4)
DllStructSetData($tRect, "Top", $aRect + 8)
DllStructSetData($tRect, "Bottom", $aRect - 2)
_WinAPI_DrawText($hDC, $sText, $tRect, BitOR($DT_LEFT, $DT_VCENTER))
_WinAPI_SetTextColor($hDC, $clrForeground)
_WinAPI_SetBkColor($hDC, $clrBackground)
EndSwitch
EndIf
Return $GUI_RUNDEFMSG
EndFunc ;==>_WM_DRAWITEM
Func _WinAPI_Rectangle($hDC, $iLeft, $iTop, $iRight, $iBottom)
Local $aResult = DllCall("gdi32.dll", "int", "Rectangle", "hwnd", $hDC, "int", $iLeft, "int", $iTop, "int", $iRight, "int", $iBottom)
If @error Then Return SetError(@error, @extended, 0)
Return $aResult <> 0
EndFunc ;==>_WinAPI_Rectangle
回复 1# 862228699
己修改,你试试
#include <GUIComboBox.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <WinAPI.au3>
#include <WinAPIFiles.au3>;含RGB转换BGR颜色UDF
Global $iColor = 0xffffff;0x114c93;选中颜色
Global $clrWindow = 0xffffff;底色
Global $hBrushNorm = _WinAPI_CreateSolidBrush($clrWindow)
Global $hBrushSel = _WinAPI_CreateSolidBrush(_WinAPI_SwitchColor($iColor))
Global $hPen = _WinAPI_CreatePen($PS_SOLID, 2, _WinAPI_SwitchColor($iColor))
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))
GUICtrlSetData($ComboBox, "实现这里背景为白色|苹果|香蕉|桔子|梨子|李子|西瓜|桔子|梨子|李子|西瓜|桔子|梨子|李子|西瓜", "实现这里背景为白色")
GUICtrlSetFont(-1, 10, 400, -1)
GUISetState()
Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE
_WinAPI_DeleteObject($hPen)
_WinAPI_DeleteObject($hBrushSel)
_WinAPI_DeleteObject($hBrushNorm)
GUIDelete()
Func _WM_MEASUREITEM($hWnd, $iMsg, $iwParam, $ilParam)
Local $tagMEASUREITEMSTRUCT = 'uint CtlType;uint CtlID;uint itemID;uint itemWidth;uint itemHeight;ulong_ptr itemData;'
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 = 3 Then DllStructSetData($tMIS, 'itemHeight', 30)
Return $GUI_RUNDEFMSG
EndFunc ;==>_WM_MEASUREITEM
Func _WM_DRAWITEM($hWnd, $iMsg, $iwParam, $ilParam)
Local $tagDRAWITEMSTRUCT = 'uint CtlType;uint CtlID;uint itemID;uint itemAction;uint itemState;hwnd hwndItem;hwnd hDC;' & $tagRECT & ';ulong_ptr itemData;'
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 = 3 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, 1) Then
$hOldBrush = _WinAPI_SelectObject($hDC, $hBrushSel)
$hOldPen = _WinAPI_SelectObject($hDC, $hPen)
_WinAPI_Rectangle($hDC, $aRect + 1, $aRect + 1, $aRect, $aRect)
_WinAPI_SelectObject($hDC, $hOldPen)
_WinAPI_SelectObject($hDC, $hOldBrush)
$clrBackground = _WinAPI_SetBkColor($hDC, _WinAPI_SwitchColor($iColor))
$clrBackground = _WinAPI_SetTextColor($hDC, 0x000000)
Else
$clrBackground = _WinAPI_SetBkColor($hDC, $clrWindow)
_WinAPI_FillRect($hDC, DllStructGetPtr($tRect), $hBrushNorm)
EndIf
DllStructSetData($tRect, "Left", $aRect + 4)
DllStructSetData($tRect, "Top", $aRect + 8)
DllStructSetData($tRect, "Bottom", $aRect - 2)
_WinAPI_DrawText($hDC, $sText, $tRect, BitOR($DT_LEFT, $DT_VCENTER))
_WinAPI_SetTextColor($hDC, $clrForeground)
_WinAPI_SetBkColor($hDC, $clrBackground)
EndSwitch
EndIf
Return $GUI_RUNDEFMSG
EndFunc ;==>_WM_DRAWITEM
Func _WinAPI_Rectangle($hDC, $iLeft, $iTop, $iRight, $iBottom)
Local $aResult = DllCall("gdi32.dll", "int", "Rectangle", "hwnd", $hDC, "int", $iLeft, "int", $iTop, "int", $iRight, "int", $iBottom)
If @error Then Return SetError(@error, @extended, 0)
Return $aResult <> 0
EndFunc ;==>_WinAPI_Rectangle
回复 2# chzj589
回复 2# chzj589
但是我的选中颜色没有了。。。就是展开那个鼠标经过的颜色没有了。 回复 4# 862228699
把第8行:
Global $clrWindow = 0xffffff;底色
改为:
Global $clrWindow = 14675183;0xFF0000;0xffffff;底色 回复 2# chzj589
#include <GUIComboBox.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <WinAPI.au3>
#include <WinAPIFiles.au3>;含RGB轉換BGR顏色UDF
Global $iColor = 0xffffff;0x114c93;選中顏色
Global $clrWindow = 0xffffff;底色
Global $hBrushNorm = _WinAPI_CreateSolidBrush($clrWindow)
Global $hBrushSel = _WinAPI_CreateSolidBrush(_WinAPI_SwitchColor($iColor))
Global $hPen = _WinAPI_CreatePen($PS_SOLID, 2, _WinAPI_SwitchColor($iColor))
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))
GUICtrlSetData($ComboBox, "實現這裡背景為白色|蘋果|香蕉|桔子|梨子|李子|西瓜|桔子|梨子|李子|西瓜|桔子|梨子|李子|西瓜", "實現這裡背景為白色")
GUICtrlSetFont(-1, 10, 400, -1)
GUISetState()
Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE
_WinAPI_DeleteObject($hPen)
_WinAPI_DeleteObject($hBrushSel)
_WinAPI_DeleteObject($hBrushNorm)
GUIDelete()
Func _WM_MEASUREITEM($hWnd, $iMsg, $iwParam, $ilParam)
Local $tagMEASUREITEMSTRUCT = 'uint CtlType;uint CtlID;uint itemID;uint itemWidth;uint itemHeight;ulong_ptr itemData;'
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 = 3 Then DllStructSetData($tMIS, 'itemHeight', 30)
Return $GUI_RUNDEFMSG
EndFunc ;==>_WM_MEASUREITEM
Func _WM_DRAWITEM($hWnd, $iMsg, $iwParam, $ilParam)
Local $tagDRAWITEMSTRUCT = 'uint CtlType;uint CtlID;uint itemID;uint itemAction;uint itemState;hwnd hwndItem;hwnd hDC;' & $tagRECT & ';ulong_ptr itemData;'
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 = 3 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 GUICtrlRead($ComboBox) ="實現這裡背景為白色" THEN
Global $iColor = 0xffffff;選中顏色
Global $clrWindow = 0xffffff;底色
Global $hBrushNorm = _WinAPI_CreateSolidBrush($clrWindow)
Global $hBrushSel = _WinAPI_CreateSolidBrush(_WinAPI_SwitchColor($iColor))
Global $hPen = _WinAPI_CreatePen($PS_SOLID, 2, _WinAPI_SwitchColor($iColor))
Else
Global $iColor = 0x114c93;選中顏色
Global $clrWindow = 0xffffff;底色
Global $hBrushNorm = _WinAPI_CreateSolidBrush($clrWindow)
Global $hBrushSel = _WinAPI_CreateSolidBrush(_WinAPI_SwitchColor($iColor))
Global $hPen = _WinAPI_CreatePen($PS_SOLID, 2, _WinAPI_SwitchColor($iColor))
EndIf
If BitAND($iItemState, 1) Then
$hOldBrush = _WinAPI_SelectObject($hDC, $hBrushSel)
$hOldPen = _WinAPI_SelectObject($hDC, $hPen)
_WinAPI_Rectangle($hDC, $aRect + 1, $aRect + 1, $aRect, $aRect)
_WinAPI_SelectObject($hDC, $hOldPen)
_WinAPI_SelectObject($hDC, $hOldBrush)
$clrBackground = _WinAPI_SetBkColor($hDC, _WinAPI_SwitchColor($iColor))
$clrBackground = _WinAPI_SetTextColor($hDC, 0x000000)
Else
$clrBackground = _WinAPI_SetBkColor($hDC, $clrWindow)
_WinAPI_FillRect($hDC, DllStructGetPtr($tRect), $hBrushNorm)
EndIf
DllStructSetData($tRect, "Left", $aRect + 4)
DllStructSetData($tRect, "Top", $aRect + 8)
DllStructSetData($tRect, "Bottom", $aRect - 2)
_WinAPI_DrawText($hDC, $sText, $tRect, BitOR($DT_LEFT, $DT_VCENTER))
_WinAPI_SetTextColor($hDC, $clrForeground)
_WinAPI_SetBkColor($hDC, $clrBackground)
EndSwitch
EndIf
Return $GUI_RUNDEFMSG
EndFunc ;==>_WM_DRAWITEM
Func _WinAPI_Rectangle($hDC, $iLeft, $iTop, $iRight, $iBottom)
Local $aResult = DllCall("gdi32.dll", "int", "Rectangle", "hwnd", $hDC, "int", $iLeft, "int", $iTop, "int", $iRight, "int", $iBottom)
If @error Then Return SetError(@error, @extended, 0)
Return $aResult <> 0
EndFunc ;==>_WinAPI_Rectangle
回复 6# kk_lee69
这个代码要实现收起的时候背景为白色,只要修改两个地方
1:
Global $iColor = 0x114c93;选中颜色
Global $clrWindow = 0xffffff;底色
2:
$clrBackground = _WinAPI_SetBkColor($hDC, _WinAPI_SwitchColor($iColor))
增加一行字体颜色
$clrBackground = _WinAPI_SetTextColor($hDC, _WinAPI_SwitchColor(0xFF0000FF)) 回复 6# kk_lee69
如果再添加一个combo2的话怎么传递给combo2,也就是 combo2要同样的效果。 回复 8# xzf680
我的程式第70行 不就是 表示 第一個嗎??
要用在第二個 就再加一段就好啊 感谢各们大神。。目前我把字体颜色改了,背景还是白色。 回复 9# kk_lee69
那个我当然知道,你测试一下,要改其他地方的,$ilParam只能combo1,combo2的话无效。 感谢各们大神。。目前我把字体颜色改了,背景还是白色。
862228699 发表于 2017-6-2 18:31 http://www.autoitx.com/images/common/back.gif
回复 12# chzj589
两个都可以吗?源码发来看看,谢谢 回复chzj589
两个都可以吗?源码发来看看,谢谢
xzf680 发表于 2017-6-2 20:01 http://www.autoitx.com/images/common/back.gif
代码用1楼、2楼、6楼再添加一个 GUICtrlCreateCombo,
然后修改一下Func _WM_DRAWITEM($hWnd, $iMsg, $iwParam, $ilParam)
就成了
自己动手一下不更好吗? 回复 14# chzj589
自己修改一下自然是很好,就是修改一下Func _WM_DRAWITEM($hWnd, $iMsg, $iwParam, $ilParam)我知道,还没有搞明白,谢谢!
页:
[1]
2