Advertisement
sashaatx

Untitled

Jul 24th, 2023
3,948
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; http://www.autohotkey.com/board/topic/104539-controlcol-set-background-and-text-color-gui-controls/
  2. g := Gui()
  3.  
  4. but := g.add("button")
  5.  
  6. ControlColor.Set(but.hwnd, g.hwnd, 0x000000)
  7. g.Show()
  8.  
  9. class ControlColor
  10. {
  11.     static Set(Control, Window, bc := "", tc := "", Redraw := True) {
  12.         Local a := {}
  13.         a.c := Control
  14.         a.g := Window
  15.         a.bc := (bc = "") ? "" : (((bc & 255) << 16) + (((bc >> 8) & 255) << 8) + (bc >> 16))
  16.         a.tc := (tc = "") ? "" : (((tc & 255) << 16) + (((tc >> 8) & 255) << 8) + (tc >> 16))
  17.  
  18.             ControlColor.CC_WindowProc("Set", a, "", "")
  19.  
  20.             If (Redraw) {
  21.                 WinRedraw(, , "ahk_id " Control)
  22.             }
  23.     }
  24.  
  25.  
  26.     static CC_WindowProc(hWnd, uMsg, wParam, lParam) {
  27.         Local tc, bc, a
  28.         Static Win := {}
  29.         ; Critical
  30.  
  31.         If uMsg.c <= 0x138 && uMsg.c >= 0x132
  32.         {
  33.             If (Win[hWnd].HasKey(lParam)) {
  34.                 If (tc := Win[hWnd, lParam, "tc"]) {
  35.                     DllCall("gdi32.dll\SetTextColor", "Ptr", wParam, "UInt", tc)
  36.                 }
  37.  
  38.                 If (bc := Win[hWnd, lParam, "bc"]) {
  39.                     DllCall("gdi32.dll\SetBkColor", "Ptr", wParam, "UInt", bc)
  40.                 }
  41.  
  42.                 Return Win[hWnd, lParam, "Brush"] ; Return the HBRUSH to notify the OS that we altered the HDC.
  43.             }
  44.  
  45.  
  46.         }
  47.         ; WM_CTLCOLOR(MSGBOX|EDIT|LISTBOX|BTN|DLG|SCROLLBAR|STATIC)
  48.  
  49.         If (hWnd == "Set") {
  50.             a := uMsg
  51.             Win[a.g, a.c] := a
  52.  
  53.             If ((Win[a.g, a.c, "tc"] == "") && (Win[a.g, a.c, "bc"] == "")) {
  54.                 Win[a.g].Remove(a.c, "")
  55.             }
  56.  
  57.             If (!Win[a.g, "WindowProcOld"]) {
  58.                 Win[a.g, "WindowProcOld"] := DllCall("SetWindowLong" . (A_PtrSize == 8 ? "Ptr" : "")
  59.                     , "Ptr", a.g, "Int", -4, "Ptr", CallbackCreate("CC_WindowProc", "", 4), "UPtr")
  60.             }
  61.  
  62.             If (Win[a.g, a.c, "bc"] != "") {
  63.                 Win[a.g, a.c, "Brush"] := DllCall("gdi32.dll\CreateSolidBrush", "UInt", a.bc, "UPtr")
  64.             }
  65.  
  66.             Return
  67.         }
  68.         Return DllCall("CallWindowProc", "Ptr", Win[hWnd, "WindowProcOld"], "Ptr", hWnd, "UInt", uMsg, "Ptr", wParam, "Ptr", lParam, "Ptr")
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement