找回密码
 加入
搜索
查看: 6371|回复: 24

[图形处理] 【已解决】绘制COMBO,如何让收起的时候背景为白色

[复制链接]
发表于 2017-6-2 11:44:14 | 显示全部楼层 |阅读模式
本帖最后由 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[4]
        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[0] + 1, $aRect[1] + 1, $aRect[2], $aRect[3])
                                        _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[0] + 4)
                                DllStructSetData($tRect, "Top", $aRect[1] + 8)
                                DllStructSetData($tRect, "Bottom", $aRect[3] - 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] <> 0
EndFunc   ;==>_WinAPI_Rectangle

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
发表于 2017-6-2 12:54:32 | 显示全部楼层
回复 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[4]
        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[0] + 1, $aRect[1] + 1, $aRect[2], $aRect[3])
                                        _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[0] + 4)
                                DllStructSetData($tRect, "Top", $aRect[1] + 8)
                                DllStructSetData($tRect, "Bottom", $aRect[3] - 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] <> 0
EndFunc   ;==>_WinAPI_Rectangle

发表于 2017-6-2 12:57:29 | 显示全部楼层
回复 2# chzj589



本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
 楼主| 发表于 2017-6-2 12:58:28 | 显示全部楼层
回复 2# chzj589

但是我的选中颜色没有了。。。就是展开那个鼠标经过的颜色没有了。
发表于 2017-6-2 13:08:49 | 显示全部楼层
回复 4# 862228699
把第8行:
Global $clrWindow = 0xffffff;底色
改为:
Global $clrWindow = 14675183;0xFF0000;0xffffff;底色
发表于 2017-6-2 13:40:48 | 显示全部楼层
回复 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[4]
        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[0] + 1, $aRect[1] + 1, $aRect[2], $aRect[3])
                                        _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[0] + 4)
                                DllStructSetData($tRect, "Top", $aRect[1] + 8)
                                DllStructSetData($tRect, "Bottom", $aRect[3] - 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] <> 0
EndFunc   ;==>_WinAPI_Rectangle
发表于 2017-6-2 13:56:44 | 显示全部楼层
回复 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))
发表于 2017-6-2 16:07:53 | 显示全部楼层
回复 6# kk_lee69


    如果再添加一个combo2的话怎么传递给combo2,也就是 combo2要同样的效果。
发表于 2017-6-2 18:08:09 | 显示全部楼层
回复 8# xzf680

我的程式  第70行 不就是 表示   第一個嗎??

要用在第二個 就再加一段就好啊
 楼主| 发表于 2017-6-2 18:31:07 | 显示全部楼层
感谢各们大神。。目前我把字体颜色改了,背景还是白色。
发表于 2017-6-2 18:59:08 | 显示全部楼层
回复 9# kk_lee69


    那个我当然知道,你测试一下,要改其他地方的,$ilParam只能combo1,combo2的话无效。
发表于 2017-6-2 19:44:42 | 显示全部楼层
感谢各们大神。。目前我把字体颜色改了,背景还是白色。
862228699 发表于 2017-6-2 18:31





本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
发表于 2017-6-2 20:01:10 | 显示全部楼层
回复 12# chzj589


    两个都可以吗?源码发来看看,谢谢
发表于 2017-6-2 20:30:20 | 显示全部楼层
回复  chzj589


    两个都可以吗?源码发来看看,谢谢
xzf680 发表于 2017-6-2 20:01


代码用1楼、2楼、6楼再添加一个 GUICtrlCreateCombo,
然后修改一下Func _WM_DRAWITEM($hWnd, $iMsg, $iwParam, $ilParam)
就成了
自己动手一下不更好吗?
发表于 2017-6-2 20:53:50 | 显示全部楼层
回复 14# chzj589


    自己修改一下自然是很好,就是修改一下Func _WM_DRAWITEM($hWnd, $iMsg, $iwParam, $ilParam)我知道,还没有搞明白,谢谢!
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-3-29 03:32 , Processed in 0.086549 second(s), 24 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表