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

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


To get whether the system is in tablet mode or not, query the system metric ConvertibleSlateMode like so (not tested, but it should work fine as far back as XP):

public static class TabletPCSupport{   private static readonly int SM_CONVERTIBLESLATEMODE = 0x2003;   private static readonly int SM_TABLETPC = 0x56;   private static Boolean isTabletPC = false;   public static Boolean SupportsTabletMode { get { return isTabletPC; }}   public static Boolean IsTabletMode    {       get       {           return QueryTabletMode();       }   }   static TabletPCSupport ()   {        isTabletPC = (GetSystemMetrics(SM_TABLETPC) != 0);   }   [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto, EntryPoint = "GetSystemMetrics")]   private static extern int GetSystemMetrics (int nIndex);   private static Boolean QueryTabletMode ()   {       int state = GetSystemMetrics(SM_CONVERTIBLESLATEMODE);       return (state == 0) && isTabletPC;   }}

(Documentation here)


I have looked everywhere for how to tell if Windows 10 is in tablet mode and here is the simplest solution I found:

bool bIsTabletMode = false;var uiMode = UIViewSettings.GetForCurrentView().UserInteractionMode;if (uiMode == Windows.UI.ViewManagement.UserInteractionMode.Touch) bIsTabletMode = true;else bIsTabletMode = false;// (Could also compare with .Mouse instead of .Touch)


According to this article, you cant listen to WM_SETTINGCHANGE message. Here is a short c# sample :

protected override void WndProc(ref Message m)        {                        const int WM_WININICHANGE = 0x001A,                WM_SETTINGCHANGE = WM_WININICHANGE;            if (m.Msg == WM_SETTINGCHANGE)            {                if (Marshal.PtrToStringUni(m.LParam) == "UserInteractionMode")                {                    MessageBox.Show(Environment.OSVersion.VersionString);                }            }            base.WndProc(ref m);        }

For Windows 10 you should then perform some COM Interfacing with some WinRT stuff, to check if you are in UserInteractionMode.Mouse (desktop) or UserInteractionMode.Touch (tablet).

The Com Interop stuff looks rather tricky but it seems to be the only way if you are in a stock win32 app.