Page 1 of 1

Toggle Windows 1O LightMode - DarkMode

Posted: 28 Mar 2020, 16:01
by emmanuel d
This post will demonstrate how to toggle lightmode on windows 10
Normally you go to Settings to change it.

The first script will do this for you.
The second one shows you how to implement it in your scripts.

have fun.

The compiled version with icon (next to startbutton in the screenshots.

Code: Select all

#SingleInstance Force                             ; Allow only one instance off this app]
#NoTrayIcon                                       ; we don't want a SystemTray icon
; read the System lightmode from the registry 
RegRead,L_LightMode,HKCU,SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize,SystemUsesLightTheme
If L_LightMode {                                  ; if the mode was Light
	; write both system end App lightmode to the registry
	RegWrite,Reg_Dword,HKCU,SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize,SystemUsesLightTheme,0
	RegWrite,Reg_Dword,HKCU,SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize,AppsUseLightTheme   ,0
	}
else {                                            ; if the mode was dark
	; write both system end App lightmode to the registry
	RegWrite,Reg_Dword,HKCU,SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize,SystemUsesLightTheme,1
	RegWrite,Reg_Dword,HKCU,SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize,AppsUseLightTheme   ,1
	}
; tell the system it needs to refresh the user settings
run,RUNDLL32.EXE USER32.DLL`, UpdatePerUserSystemParameters `,2 `,True
Exitapp 

Code: Select all

#SingleInstance Force                             ; Allow only one instance off this app]
; setting up some global variables with prefix G_ for clarity
Global G_LightMode ; will contain the value from the system
Global G_ScriptLightMode := 0  ; the mode for the script
Global G_hTV ; handle for the treeview
Global G_hLV ; handle for the listview
Global G_hEB ; handle for the edit box
; read the System lightmode from the registry 
RegRead,G_LightMode,HKCU,SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize,AppsUseLightTheme
; setting up the GUI
; using handles instead of a control variable
Gui,Font,s14
Gui,Add,Treeview,x00 y0 w200 h700 hwndG_hTV
Gui,Add,Listview,x+0 yp w200 h700 hwndG_hLV,Name
Gui,Add,Edit    ,x+0 yp w600 h700 hwndG_hEB,Press Win + n to toggle nightmode for this app
; Fill up the gui with some data
Loop,10 
	TV_Add("TV " A_Index)
Loop,50
	LV_Add("","LV " A_Index)
Gui_Color_Changed(G_LightMode)                    ; Apply the lightmode from the system
gui,show,,NightModeCapableApp                     ; Show our GUI
OnMessage(0x001A,"WM_SETTINGCHANGE")              ; Get this message to see if the system changed lightmode
Return

#IfWinActive,NightModeCapableApp                  ; if our app is active
	#n::MI_Toggle_DarkMode()                          ; set Win + n to toggle nightmode
WM_SETTINGCHANGE(L_wParam,L_lParam,L_msg,L_hwnd){ ; message handler
	If (L_msg=0x001A)                               ; WM_SETTINGCHANGE	:= 0x001A
		RegRead,L_LightMode,HKCU,SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize,AppsUseLightTheme
	if L_LightMode<>G_LightMode                     ; If the lightmode has changed
		Gui_Color_Changed(G_LightMode:=L_LightMode)   ; assign the new mode and change the colors
	Return,1
	}
MI_Toggle_DarkMode(){                             ; MI_ stands for MenuItem
	G_ScriptLightMode := !G_ScriptLightMode         ; toggle the scripts lightmode
	Gui_Color_Changed()                             ; change the colors
	}
Gui_Color_Changed(L_LightMode:=""){
	If (L_LightMode=0)                              ; if lightmode is off
		G_ScriptLightMode:=0                          ; overwrite the app mode
	else If (L_LightMode=1)                         ; if lightmode is on
		G_ScriptLightMode:=1                          ; overwrite the app mode
	If G_ScriptLightMode {                          ; if the script needs to be Light
		Gui,Color,White,White                         ; change the gui color, and controls
		Gui,Font ,cBlack                              ; new controls wil have this text color
		GuiControl,+BackGroundWhite +cBlack,%G_hTV%
		GuiControl,+BackGroundWhite +cBlack,%G_hLV%
		GuiControl,+BackGroundWhite +cBlack,%G_hEB%
		}
	else {                                          ; if the script needs to be Dark
		Gui,Color,Black,Black                         ; change the gui color, and controls
		Gui,Font ,cWhite                              ; new controls wil have this text color
		GuiControl,+BackGroundBlack +cWhite,%G_hTV%
		GuiControl,+BackGroundBlack +cWhite,%G_hLV%
		GuiControl,+BackGroundBlack +cWhite,%G_hEB%
		}
	}
20200331203805_screenshot.jpg
Screenshot of the "Light Mode"
20200331203805_screenshot.jpg (434.84 KiB) Viewed 2800 times
20200331203139_screenshot.jpg
Screenshot of the "Dark Mode"
20200331203139_screenshot.jpg (380.92 KiB) Viewed 2800 times

Re: Toggle Windows 1O LightMode - DarkMode

Posted: 31 Mar 2020, 14:15
by emmanuel d
Updated with screenshots and download link.

Re: Toggle Windows 1O LightMode - DarkMode

Posted: 31 Mar 2020, 14:18
by gregster
These google drive links are not actually image links (at least they won't work with the bbcode of our forum software). Either you remove the img-tags and just keep them as links (like https://drive.google.com/uc?id=1jNYrgHXeheDjO3bLGQ2pZkLuyUYcMRPe) or you can upload pictures to the forums directly via the Full editor > Attachments > Add files (> place inline).

Re: Toggle Windows 1O LightMode - DarkMode

Posted: 05 Nov 2020, 17:19
by grovt
Hello, I am looking for a shortcut that do exactly this. When I try so start your script my computer warns me that it an be dangerous. I dont understand a lot of coding so I am asking if I can execute this safely and if doesn't is any kind of virus. Sorry for my question.
Best regards

Re: Toggle Windows 1O LightMode - DarkMode

Posted: 06 Nov 2020, 17:53
by Greast
@grovt
You can use akh format to open it, just paste code and run it.

p.s. I don't see any line of malware.

Re: Toggle Windows 1O LightMode - DarkMode

Posted: 08 Nov 2020, 07:09
by emmanuel d
It looks like you are trying to run the executable.
It is just the compiled version of the script.
it should be fine.

Re: Toggle Windows 1O LightMode - DarkMode

Posted: 08 Nov 2020, 07:55
by emmanuel d
gregster wrote:
31 Mar 2020, 14:18
These google drive links are not actually image links (at least they won't work with the bbcode of our forum software). Either you remove the img-tags and just keep them as links (like https://drive.google.com/uc?id=1jNYrgHXeheDjO3bLGQ2pZkLuyUYcMRPe) or you can upload pictures to the forums directly via the Full editor > Attachments > Add files (> place inline).
I have tried that on a other post before, and they got removed. :? :o :shock:
So i did as you asked . if they remove them, i am not gonna be bothered. 8-)

Re: Toggle Windows 1O LightMode - DarkMode

Posted: 20 Nov 2021, 20:05
by dharmaturtle
Thanks for this. Does exactly what I want on Win10.

For any newbies, here's how to run the first script using Win+O.

Code: Select all

#o::
RegRead,L_LightMode,HKCU,SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize,SystemUsesLightTheme
If L_LightMode {                                  ; if the mode was Light
	; write both system end App lightmode to the registry
	RegWrite,Reg_Dword,HKCU,SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize,SystemUsesLightTheme,0
	RegWrite,Reg_Dword,HKCU,SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize,AppsUseLightTheme   ,0
	}
else {                                            ; if the mode was dark
	; write both system end App lightmode to the registry
	RegWrite,Reg_Dword,HKCU,SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize,SystemUsesLightTheme,1
	RegWrite,Reg_Dword,HKCU,SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize,AppsUseLightTheme   ,1
	}
; tell the system it needs to refresh the user settings
run,RUNDLL32.EXE USER32.DLL`, UpdatePerUserSystemParameters `,2 `,True
return