Toggle Windows 1O LightMode - DarkMode

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
emmanuel d
Posts: 90
Joined: 17 Nov 2013, 04:45

Toggle Windows 1O LightMode - DarkMode

28 Mar 2020, 16:01

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 2765 times
20200331203139_screenshot.jpg
Screenshot of the "Dark Mode"
20200331203139_screenshot.jpg (380.92 KiB) Viewed 2765 times
Last edited by emmanuel d on 08 Nov 2020, 07:47, edited 2 times in total.
User avatar
emmanuel d
Posts: 90
Joined: 17 Nov 2013, 04:45

Re: Toggle Windows 1O LightMode - DarkMode

31 Mar 2020, 14:15

Updated with screenshots and download link.
gregster
Posts: 9087
Joined: 30 Sep 2013, 06:48

Re: Toggle Windows 1O LightMode - DarkMode

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).
grovt
Posts: 1
Joined: 05 Nov 2020, 17:13

Re: Toggle Windows 1O LightMode - DarkMode

05 Nov 2020, 17:19

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
Greast
Posts: 71
Joined: 24 Oct 2020, 19:01

Re: Toggle Windows 1O LightMode - DarkMode

06 Nov 2020, 17:53

@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.
“You see things; you say, 'Why?' But I dream things that never were; and I say 'Why not?”
― George Bernard Shaw
User avatar
emmanuel d
Posts: 90
Joined: 17 Nov 2013, 04:45

Re: Toggle Windows 1O LightMode - DarkMode

08 Nov 2020, 07:09

It looks like you are trying to run the executable.
It is just the compiled version of the script.
it should be fine.
User avatar
emmanuel d
Posts: 90
Joined: 17 Nov 2013, 04:45

Re: Toggle Windows 1O LightMode - DarkMode

08 Nov 2020, 07:55

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-)
dharmaturtle
Posts: 3
Joined: 20 Nov 2021, 19:48

Re: Toggle Windows 1O LightMode - DarkMode

20 Nov 2021, 20:05

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

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: daiweisc and 143 guests