Jump to content

System Color Picker


 Share

Recommended Posts

Since color picking appears to be the hot topic, I knocked the following together to show another way that this could be done but this time for system colors. Would be great if Yashied (nudge nudge) could incorporate this into his excellent color picker!

#include <WinAPI.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <Misc.au3>

Opt("MouseCoordMode", 2)

;Global $hWnd = GUICreate("System Colors", 200, 232, Default, Default, Default, $WS_EX_TOOLWINDOW)
Global $hWnd = GUICreate("System Colors", 200, 232, Default, Default, $WS_POPUP)
Global $hTip = GUICtrlGetHandle(GUICtrlCreateLabel("", 8, 200, 184, 24, _
                BitOR($GUI_SS_DEFAULT_LABEL, $WS_BORDER, $SS_CENTER, $SS_CENTERIMAGE)))
GUISetState()

Global Const $NUMCOLORS = 36

Global $gaBrush[$NUMCOLORS]
For $i = 0 To $NUMCOLORS - 1
    If $i < 31 Then
        $gaBrush[$i] = _WinAPI_GetSysColorBrush($i)
    Else
        $gaBrush[$i] = _WinAPI_GetStockObject($WHITE_BRUSH)
    EndIf
Next
Global $gaNames[$NUMCOLORS] = ["Scrollbar", _
                                "Desktop", _
                                "Active Caption", _
                                "Inactive Caption", _
                                "Menu", _
                                "Window", _
                                "Window Frame", _
                                "Menu Text", _
                                "Window Text", _
                                "Caption Text", _
                                "Active Border", _
                                "Inactive Border", _
                                "Application Workspace", _
                                "Highlight", _
                                "Highlight Text", _
                                "3D Face", _
                                "3D Shadow", _
                                "Gray Text", _
                                "Button Text", _
                                "Inactive Caption Text", _
                                "3D Highlight", _
                                "3D Dark Shadow", _
                                "3D Light", _
                                "Information Text", _
                                "Information Background", _
                                "Unknown", _
                                "Hotlight", _
                                "Gradient Active Caption", _
                                "Gradient Inactive Caption", _
                                "Menu Highlight", _
                                "Menu Bar", _
                                "Unused", "Unused", "Unused", "Unused", "Unused"]

Global $hDC = _WinAPI_GetDC($hWnd)
Global $tRect = DllStructCreate($tagRECT)
Global $hPen = _WinAPI_GetStockObject($BLACK_PEN)
Global $hOldPen = _WinAPI_SelectObject($hDC, $hPen)

For $i = 0 To $NUMCOLORS - 1
    _DrawButton($i, True)
Next

Global $currentButton = -1, $mPos, $thisButton, $downButton = -2

While GUIGetMsg() <> -3
    If WinActive($hWnd) Then
        $mPos = MouseGetPos()
        $thisButton = _GetButtonNumber($mPos[0], $mPos[1])
        If $currentButton <> $thisButton Then
            If $currentButton <> -1 Then
                _DrawButton($currentButton, True)
            EndIf
            $currentButton = $thisButton
            If $thisButton >=0 Then
                _DrawButton($thisButton, False)
                _WinAPI_SetWindowText($hTip, $gaNames[$thisButton])
            Else
                _WinAPI_SetWindowText($hTip, "")
            EndIf
        EndIf
        If _IsPressed("01") Then
            If $downButton = -2 Then $downButton = $thisButton
        Else
            If $thisButton = $downButton Then
                If $thisButton >= 0 Then ExitLoop
            EndIf
            $downButton = -2
        EndIf
    Else
        $thisButton = -1
        ExitLoop
    EndIf
    Sleep(10)
WEnd

_WinAPI_SelectObject($hDC, $hOldPen)
_WinAPI_ReleaseDC($hWnd, $hDC)

GUIDelete($hWnd)

If $thisButton >= 0 Then
    MsgBox(64, "Selected Color", $thisButton & " : " & $gaNames[$thisButton])
EndIf

Exit

Func _GetButtonNumber($mouseX, $mouseY)
    Local $overButton = False, $x, $y, $i
    For $i = 0 To $NUMCOLORS - 1
        $x = Mod($i, 6) * 32 + 8
        $y = Int($i / 6) * 32 + 8
        If $mouseX >= $x And $mouseX <= $x + 24 _
        And $mouseY >= $y And $mouseY <= $y + 24 Then
            $overButton = True
            ExitLoop
        EndIf
    Next
    If $overButton Then Return $i
    Return -1
EndFunc

Func _DrawButton($buttonIndex, $fSmall)
    Local $x = Mod($buttonIndex, 6) * 32 + 8
    Local $y = Int($buttonIndex / 6) * 32 + 8
    Local $hOldBrush = _WinAPI_SelectObject($hDC, $gaBrush[$buttonIndex])
    If $fSmall Then
        DllStructSetData($tRect, 1, $x - 2)
        DllStructSetData($tRect, 2, $y - 2)
        DllStructSetData($tRect, 3, $x + 26)
        DllStructSetData($tRect, 4, $y + 26)
        _WinAPI_FillRect($hDC, DllStructGetPtr($tRect), $gaBrush[15])
        DllCall("gdi32.dll", "int", "Rectangle", "hwnd", $hDC, _
                    "int", $x, "int", $y, "int", $x + 24, "int", $y + 24)
    Else
        DllCall("gdi32.dll", "int", "Rectangle", "hwnd", $hDC, _
                    "int", $x - 2, "int", $y - 2, "int", $x + 26, "int", $y + 26)
    EndIf
    _WinAPI_SelectObject($hDC, $hOldBrush)
EndFunc

WBD

Link to comment
Share on other sites

Any code that I post on the forum is considered by me to be "out there" and anyone can do with it as they please. I was looking again at COP the other day and started thinking about some enhancements but I don't have my notes to hand and I can't remember what they were.

WBD

Link to comment
Share on other sites

Any code that I post on the forum is considered by me to be "out there" and anyone can do with it as they please. I was looking again at COP the other day and started thinking about some enhancements but I don't have my notes to hand and I can't remember what they were.

Any comments are welcome :D , already proceeded to v 0.3.9 and currently Im trying to implement wraithdu's comments. And your function will definitly go into the next version :D ...
Link to comment
Share on other sites

Any comments are welcome :D , already proceeded to v 0.3.9 and currently Im trying to implement wraithdu's comments. And your function will definitly go into the next version :D ...

Note that it's just a prototype; it needs some work to be "release ready". For example, try hitting [Escape] when the mouse is over a color. If you need some help polishing it further then let me know and I'll help where I can.

Regards,

WBD

Link to comment
Share on other sites

Since color picking appears to be the hot topic, I knocked the following together to show another way that this could be done but this time for system colors. Would be great if Yashied (nudge nudge) could incorporate this into his excellent color picker!

Incorporate what? In my UDF, you can define any palette itself, at its discretion. This is not a problem.

_GUIColorPicker_SetPalette()

Link to comment
Share on other sites

Incorporate what? In my UDF, you can define any palette itself, at its discretion. This is not a problem.

_GUIColorPicker_SetPalette()

I realise that but I thought it would be nice if, as well as the "Custom" button at the bottom, there could be a "System" button that allowed you to pick a system color. I'm thinking about other color picking implementations that also do this (e.g. in Visual Studio where you have "Custom", "Web" and "System"). It's not important; I may take a look at doing it myself if you don't mind?

WBD

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...