Invert window colors Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
d_romeo
Posts: 28
Joined: 23 Dec 2020, 12:18

Invert window colors

22 Aug 2021, 15:32

Hi, I use AutoHotKey to change dark/light modes for many programs I use, but for some that don't support it I'd like to reverse the colors for the specific window, or alternatively for some screen coordinates. It is not an aesthetically perfect solution, but sufficient for the purpose in the absence of alternatives. Is there a script for this?
Thank you.
Last edited by d_romeo on 22 Aug 2021, 16:51, edited 2 times in total.
teadrinker
Posts: 4360
Joined: 29 Mar 2015, 09:41
Contact:

Re: Reverse window colors

22 Aug 2021, 16:13

I'm not sure, what do you mean by «reverse», perhaps you mean «invert».
SRCINVERT
d_romeo
Posts: 28
Joined: 23 Dec 2020, 12:18

Re: Invert window colors

22 Aug 2021, 16:56

Yes, I'm sorry. Invert the colors. With Magnify and ^!i you invert the colors of the whole screen, but I'd like to do it for just a few windows.
teadrinker
Posts: 4360
Joined: 29 Mar 2015, 09:41
Contact:

Re: Invert window colors

22 Aug 2021, 19:25

I was wrong, NOTSRCCOPY is more suitable. A simple examle:

Code: Select all

NOTSRCCOPY := 0x330008

X := 100, Y := 100, W := 600, H := 400

hDC := DllCall("GetDC", "Ptr", 0, "Ptr")
hBM := DllCall("CreateCompatibleBitmap", "Ptr", hDC, "Int", W, "Int", H, "Ptr")
hMDC := DllCall("CreateCompatibleDC", "Ptr", hDC, "Ptr")
hObj := DllCall("SelectObject", "Ptr", hMDC, "Ptr", hBM)
DllCall("BitBlt", "Ptr", hMDC, "Int", 0, "Int", 0, "Int", W, "Int", H
                , "Ptr", hDC, "Int", X, "Int", Y, "UInt", NOTSRCCOPY)
DllCall("SelectObject", "Ptr", hMDC, "Ptr", hObj)
DllCall("DeleteDC", "Ptr", hMDC)
DllCall("ReleaseDC", "Ptr", 0, "Ptr", hDC)

Gui, New, -Caption +AlwaysOnTop +Owner -DPIScale
Gui, Margin, 0, 0
Gui, Add, Pic,, HBITMAP:%hBM%
Gui, Show, x%X% y%Y% NA
malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: Invert window colors  Topic is solved

23 Aug 2021, 01:04

Example with magnification api and CreateWindowInBand.

Code: Select all

x := 500
y := 500
w := 320
h := 240

Gui, +HWNDhGui +AlwaysOnTop
DllCall("GetWindowBand", "ptr", hGui, "uint*", band)
Gui, Destroy
hGui := ""
if (band = 1)
{
   If (A_PtrSize = 8)
      RunWait "C:\Program Files\AutoHotkey\AutoHotkeyU64_UIA.exe" "%A_ScriptFullPath%"
   Else If A_IsUnicode
      RunWait "C:\Program Files\AutoHotkey\AutoHotkeyU32_UIA.exe" "%A_ScriptFullPath%"
   Else
      RunWait "C:\Program Files\AutoHotkey\AutoHotkeyA32_UIA.exe" "%A_ScriptFullPath%"
}
#SingleInstance Force
SetBatchLines -1
SetWinDelay -1
OnExit, Uninitialize 
Gui, +HWNDhGui -DPIScale -Caption +AlwaysOnTop +E0x02000000 +E0x00080000 +E0x20 ;  WS_EX_COMPOSITED := E0x02000000  WS_EX_LAYERED := E0x00080000
Gui, Margin, 0,0
Gui, Show, x%x% y%y% w%w% h%h% NA

DllCall("LoadLibrary", "str", "magnification.dll")
DllCall("magnification.dll\MagInitialize")
hChildMagnifier := DllCall("CreateWindowInBand", "UInt", 0, "Str", "Magnifier", "Str", "MagnifierWindow", "UInt", WS_CHILD := 0x40000000, "Int", 0, "Int", 0, "Int", w, "Int", h, "Ptr", hGui, "UInt", 0, "Ptr", DllCall("GetWindowLong" (A_PtrSize=8 ? "Ptr" : ""), "Ptr", hGui, "Int", GWL_HINSTANCE := -6 , "ptr"), "UInt", 0, "uint", ZBID_UIACCESS := 2, "ptr")
Matrix := "-1|0|0|0|0|"
        . "0|-1|0|0|0|"
        . "0|0|-1|0|0|"  
        . "0|0|0|1|0|"  
        . "1|1|1|0|1"  
VarSetCapacity(MAGCOLOREFFECT, 100, 0)
Loop, Parse, Matrix, |  
   NumPut(A_LoopField, MAGCOLOREFFECT, (A_Index - 1) * 4, "Float")
DllCall("magnification.dll\MagSetColorEffect", "Ptr", hChildMagnifier, "Ptr", &MAGCOLOREFFECT)
WinShow, ahk_id %hChildMagnifier%
Loop
{
   if (A_PtrSize = 8)
   {
      VarSetCapacity(RECT, 16, 0)
      NumPut(x, RECT, 0, "Int")
      NumPut(y, RECT, 4, "Int")
      NumPut(w, RECT, 8, "Int")
      NumPut(h, RECT, 12, "Int")
      DllCall("magnification.dll\MagSetWindowSource", "Ptr", hChildMagnifier, "Ptr", &RECT)
   }
   else
      DllCall("magnification.dll\MagSetWindowSource", "Ptr", hChildMagnifier, "Int", x, "Int", y, "Int", w, "Int", h)
}
Return 

Uninitialize: 	
DllCall("magnification.dll\MagUninitialize")
ExitApp
Last edited by malcev on 25 Sep 2021, 13:04, edited 5 times in total.
d_romeo
Posts: 28
Joined: 23 Dec 2020, 12:18

Re: Invert window colors

23 Aug 2021, 10:10

teadrinker your code works fine thanks.

malcev I had problems with your version, I don't have AutoHotkeyU64_UIA.exe installed, I tried replacing with AutoHotkeyU64.exe and the code works, but it draws a completely white rettag. I have the 64bit Unicode version installed.
d_romeo
Posts: 28
Joined: 23 Dec 2020, 12:18

Re: Invert window colors

23 Aug 2021, 12:07

Ok, now it works, and I haven't noticed any display lag either, which is very important. There is a problem with the 2 sides as shown in the screenshot, do you have this bug too?
However, I have UAC disabled, so I don't know if that would affect it.
image.png
image.png (38.31 KiB) Viewed 2719 times
malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: Invert window colors

23 Aug 2021, 13:20

It is because of dpi scaling.
I have modified code.
d_romeo
Posts: 28
Joined: 23 Dec 2020, 12:18

Re: Invert window colors

23 Aug 2021, 14:23

It works perfectly. I will work on it now to fit the windows, thank you very much. :D
malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: Invert window colors

29 Aug 2021, 20:50

I have modified post with code - remove callback to prevent cpu usage.
viewtopic.php?p=416776#p416776
Also have created script that tracks window.
viewtopic.php?f=6&t=94264
just me
Posts: 9512
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Invert window colors

30 Aug 2021, 04:35

@malcev:
Very interesting stuff. Thx for sharing it.

@d_romeo:
Here's a full-screen color inverter, maybe you want to use it.

Code: Select all

#NoEnv
#SingleInstance Force
SetBatchLines -1
DllCall("LoadLibrary", "Str", "Magnification.dll")
Initialized := False
OnExit, Uninitialize
InvertMatrix := [-1,  0,  0,  0,  0
               ,  0, -1,  0,  0,  0
               ,  0,  0, -1,  0,  0
               ,  0,  0,  0,  1,  0
               ,  1,  1,  1,  0,  1]
VarSetCapacity(MagEffectInvert, 100, 0)
Addr := &MagEffectInvert
For I, V In InvertMatrix
   Addr := NumPut(V, Addr + 0, "Float")
Return
; ----------------------------------------------------------------------------------------------------------------------
Uninitialize:
   If (Initialized)
      DllCall("Magnification.dll\MagUninitialize")
ExitApp
; ----------------------------------------------------------------------------------------------------------------------
^+I::
   If (Initialized)
      Initialized := DllCall("Magnification.dll\MagUninitialize", "UInt") & 0
   Else If (Initialized := DllCall("Magnification.dll\MagInitialize", "UInt"))
      DllCall("Magnification.dll\MagSetFullscreenColorEffect", "Ptr", &MagEffectInvert)
Return
malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: Invert window colors

30 Aug 2021, 12:26

teadrinker detected that when We run script with UIACCESS, then if We set +AlwaysOnTop on Gui then it automatically runs with ZBID_UIACCESS band.

Code: Select all

If !RegExMatch(DllCall("GetCommandLine", "Str"), " /restart(?!\S)") {
   If (A_PtrSize = 8)
      RunWait "C:\Program Files\AutoHotkey\AutoHotkeyU64_UIA.exe" /restart "%A_ScriptFullPath%"
   Else If A_IsUnicode
      RunWait "C:\Program Files\AutoHotkey\AutoHotkeyU32_UIA.exe" /restart "%A_ScriptFullPath%"
   Else
      RunWait "C:\Program Files\AutoHotkey\AutoHotkeyA32_UIA.exe" /restart "%A_ScriptFullPath%"
}
Gui, +HWNDhGui
Gui, Add, Text,, Please enter your name:
Gui, Add, Edit, vName hwndhEdit
Gui, Show, x0 y0
DllCall("GetWindowBand", "ptr", hGui, "uint*", band)
msgbox % band
Gui, +AlwaysOnTop
DllCall("GetWindowBand", "ptr", hGui, "uint*", band)
msgbox % band
Also to invert colors in magnifier control We can do without matrix like this:

Code: Select all

x := 500
y := 500
w := 320
h := 240

Gui, +HWNDhGui +AlwaysOnTop
DllCall("GetWindowBand", "ptr", hGui, "uint*", band)
Gui, Destroy
hGui := ""
if (band = 1)
{
   If (A_PtrSize = 8)
      RunWait "C:\Program Files\AutoHotkey\AutoHotkeyU64_UIA.exe" "%A_ScriptFullPath%"
   Else If A_IsUnicode
      RunWait "C:\Program Files\AutoHotkey\AutoHotkeyU32_UIA.exe" "%A_ScriptFullPath%"
   Else
      RunWait "C:\Program Files\AutoHotkey\AutoHotkeyA32_UIA.exe" "%A_ScriptFullPath%"
}
#SingleInstance Force
SetBatchLines -1
SetWinDelay -1
OnExit, Uninitialize 
Gui, +HWNDhGui -DPIScale -Caption +AlwaysOnTop +E0x02000000 +E0x00080000 +E0x20 ;  WS_EX_COMPOSITED := E0x02000000  WS_EX_LAYERED := E0x00080000
Gui, Show, x%x% y%y% w%w% h%h% NA

DllCall("LoadLibrary", "str", "magnification.dll")
DllCall("magnification.dll\MagInitialize")
hChildMagnifier := DllCall("CreateWindowEx", "UInt", 0, "Str", "Magnifier", "Str", "MagnifierWindow", "UInt", (WS_CHILD := 0x40000000)|(MS_INVERTCOLORS := 0x0004), "Int", 0, "Int", 0, "Int", w, "Int", h, "Ptr", hGui, "UInt", 0, "Ptr", DllCall("GetWindowLong" (A_PtrSize=8 ? "Ptr" : ""), "Ptr", hGui, "Int", GWL_HINSTANCE := -6 , "ptr"), "UInt", 0, "ptr")
WinShow, ahk_id %hChildMagnifier%
Loop
{
   if (A_PtrSize = 8)
   {
      VarSetCapacity(RECT, 16, 0)
      NumPut(x, RECT, 0, "Int")
      NumPut(y, RECT, 4, "Int")
      NumPut(w, RECT, 8, "Int")
      NumPut(h, RECT, 12, "Int")
      DllCall("magnification.dll\MagSetWindowSource", "Ptr", hChildMagnifier, "Ptr", &RECT)
   }
   else
      DllCall("magnification.dll\MagSetWindowSource", "Ptr", hChildMagnifier, "Int", x, "Int", y, "Int", w, "Int", h)
}
Return 

Uninitialize: 	
DllCall("magnification.dll\MagUninitialize")
ExitApp
https://docs.microsoft.com/en-us/previous-versions/windows/desktop/magapi/magapi-magnifier-styles
Last edited by malcev on 25 Sep 2021, 13:05, edited 2 times in total.
just me
Posts: 9512
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Invert window colors

31 Aug 2021, 04:23

@malcev:
Shouldn't it be

Code: Select all

If !RegExMatch(DllCall("GetCommandLine", "Str"), " /restart(?!\S)") {
   If (A_PtrSize = 8)
      RunWait "C:\Program Files\AutoHotkey\AutoHotkeyU64_UIA.exe" /restart "%A_ScriptFullPath%"
   Else If A_IsUnicode
      RunWait "C:\Program Files\AutoHotkey\AutoHotkeyU32_UIA.exe" /restart "%A_ScriptFullPath%"
   Else
      RunWait "C:\Program Files\AutoHotkey\AutoHotkeyA32_UIA.exe" /restart "%A_ScriptFullPath%"
}
for AHK 1.1?
malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: Invert window colors

31 Aug 2021, 06:54

I chose this algorithm because of speed, but Your algorithm I think is more correct.
I have corrected codes.
d_romeo
Posts: 28
Joined: 23 Dec 2020, 12:18

Re: Invert window colors

16 Feb 2022, 08:59

malcev wrote:
30 Aug 2021, 12:26
Also to invert colors in magnifier control We can do without matrix like this:
Hi malcev, lately I can't get the script to invert the colors of a single window to work anymore, which I use quite often. Now I just get a black window, everything else works fine, like size, window tracking etc. but the colors are not reversed. Could the latest updates in Windows 11 have changed something? I currently use version 22000.527.
malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: Invert window colors

16 Feb 2022, 10:34

I do not have win11, but if You run script with ahk32 bit, then try to run it with ahk64 bit.
d_romeo
Posts: 28
Joined: 23 Dec 2020, 12:18

Re: Invert window colors

19 Feb 2022, 09:45

I fixed the problem, apparently AutoHotKey doesn't like Nvidia graphics cards. In case anyone has the same issue as me: I have configured Nvidia AutoHotkeyU64_UIA.exe in the control panel to use the integrated graphics card by default, in this way everything works correctly.
ErenJeager0
Posts: 1
Joined: 25 Feb 2022, 07:02

Re: Invert window colors

25 Feb 2022, 07:06

I'm a complete newbie. i want to invert colors of the specific window of one note whenever it opens. can someone help?
d_romeo
Posts: 28
Joined: 23 Dec 2020, 12:18

Re: Invert window colors

04 Mar 2022, 17:29

You mean you want to automatically invert a window, so it's always in dark mode?
I've included in my scripts a code that automatically inverts a window when it opens during night hours, even when closing and reopening, but it's a bit complicated. If that's what you're asking, I can help you.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], Descolada, DracoCapt, norot41087, science2002 and 77 guests