简体   繁体   中英

Enable Tablet Mode on Windows 10 through Code?

Ive read various way on how to detect if a Windows 10 device is in Tablet Mode, most notably the topic below;

How can I detect when Window 10 enters tablet mode in a Windows Forms application?

I would like to enable/disable Tablet Mode through code (.Net C#) but I cant find any resources to achieve this. Ive tried changing the registry key and sending a HWND_BROADCAST that a change has occurred but this doesn't initiate the change to tablet mode.

Ive also tried using Spy++ style applications but cant see the messages being sent.

Does a method exist to do this?

There is no real way to do this in C#. Of course you can change the registry key, but you will need a log off/on to change from or to tablet mode.

HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\ImmersiveShell\TabletMode

Enable: 1 or disable 0

Since I had a problem that my WPF-App will not appear on startup with tablet mode on, I used a AutoHotKey script. You can create a .exe as well. Source: https://autohotkey.com/boards/viewtopic.php?t=15619

#NoEnv
SetBatchLines -1
ListLines Off
#NoTrayIcon 


TABLETMODESTATE_DESKTOPMODE := 0x0
TABLETMODESTATE_TABLETMODE := 0x1

TabletModeController_GetMode(TabletModeController, ByRef mode) {
    return DllCall(NumGet(NumGet(TabletModeController+0),3*A_PtrSize), "Ptr", TabletModeController, "UInt*", mode)
}

TabletModeController_SetMode(TabletModeController, _TABLETMODESTATE, _TMCTRIGGER := 4) {
    return DllCall(NumGet(NumGet(TabletModeController+0),4*A_PtrSize), "Ptr", TabletModeController, "UInt", _TABLETMODESTATE, "UInt", _TMCTRIGGER)  
}

ImmersiveShell := ComObjCreate("{C2F03A33-21F5-47FA-B4BB-156362A2F239}", "{00000000-0000-0000-C000-000000000046}")
TabletModeController := ComObjQuery(ImmersiveShell, "{4fda780a-acd2-41f7-b4f2-ebe674c9bf2a}", "{4fda780a-acd2-41f7-b4f2-ebe674c9bf2a}")

if (TabletModeController_GetMode(TabletModeController, mode) == 0)
    TabletModeController_SetMode(TabletModeController, mode == TABLETMODESTATE_DESKTOPMODE ? TABLETMODESTATE_TABLETMODE : TABLETMODESTATE_DESKTOPMODE)

ObjRelease(TabletModeController), TabletModeController := 0
ObjRelease(ImmersiveShell), ImmersiveShell := 0 ; Can be freed after TabletModeController is created, instead   

Poke around in here - You will want to focus on the samples for user interaction mode.

NOTE: This is for UWP (Universal Windows Platform), aka Windows 10+, and you will need code for other versions of Windows if you are not targeting Win 10 only.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM