32

Update

While not the most elegant solution, one method that seems to work is to watch the relevant registry value. Here's an example using WMI to do this. I'd be happy to hear from anyone if there's a better solution than this.

using System;
using System.Management;
using System.Security.Principal;
using System.Windows.Forms;
using Microsoft.Win32;

public partial class MainForm : Form
{
    public MainForm()
    {
        this.InitializeComponent();
        this.UpdateModeFromRegistry();

        var currentUser = WindowsIdentity.GetCurrent();
        if (currentUser != null && currentUser.User != null)
        {
            var wqlEventQuery = new EventQuery(string.Format(@"SELECT * FROM RegistryValueChangeEvent WHERE Hive='HKEY_USERS' AND KeyPath='{0}\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\ImmersiveShell' AND ValueName='TabletMode'", currentUser.User.Value));
            var managementEventWatcher = new ManagementEventWatcher(wqlEventQuery);
            managementEventWatcher.EventArrived += this.ManagementEventWatcher_EventArrived;
            managementEventWatcher.Start();
        }
    }

    private void ManagementEventWatcher_EventArrived(object sender, EventArrivedEventArgs e)
    {
        this.UpdateModeFromRegistry();
    }

    private void UpdateModeFromRegistry()
    {
        var tabletMode = (int)Registry.GetValue("HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\ImmersiveShell", "TabletMode", 0);
        if (tabletMode == 1)
        {
            Console.Write(@"Tablet mode is enabled");
        }
        else
        {
            Console.Write(@"Tablet mode is disabled");
        }
    }
}

Original Question

I'm interested in make some optimizations in my Windows Forms application based on whether a user is in "Tablet Mode" (or not) using the new Windows 10 Continuum feature.

There is some guidance on how to do this in a UWP project at https://msdn.microsoft.com/en-us/library/windows/hardware/dn917883(v=vs.85).aspx (i.e. check the current view's UserInteractionMode to see if it's UserInteractionMode.Mouse or UserInteractionMode.Touch), however I'm not sure if or how I can do the same in Windows Forms.

Would there be any way I can call the necessary UWP APIs from my Windows Forms application, or is there some Windows Forms equivalent I can use?

Cœur
  • 32,421
  • 21
  • 173
  • 232
Adam Stiskala
  • 457
  • 1
  • 6
  • 10

3 Answers3

10

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)

Oded Ben Dov
  • 8,892
  • 6
  • 29
  • 45
Cheese Lover
  • 450
  • 1
  • 5
  • 14
  • Works great for me for a WPF Desktop Application. Verified on a Surface Book Pro, for when the "screen" is attached and when it is detached to become a tablet. Thanks! Best answer I found on the net – Oded Ben Dov Nov 23 '18 at 08:23
  • Spoke to soon. Breaks some time, as seen in this question: https://stackoverflow.com/questions/43106246/how-to-detect-tablet-mode – Oded Ben Dov Jan 08 '19 at 09:03
4

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)
Michał Perłakowski
  • 70,955
  • 24
  • 137
  • 155
Gary Shaw
  • 51
  • 2
  • 1
    I don't see UIViewSettings and Windows.UI at all in my app. – tofutim Apr 04 '16 at 23:09
  • Keep in mind This will only work if you are creating an UWP. Here is the link to msdn for more info: https://docs.microsoft.com/en-us/uwp/api/Windows.UI.ViewManagement.UIViewSettings#Windows_UI_ViewManagement_UIViewSettings_UserInteractionMode This won't work for traditional win32 app. – AvikB Feb 19 '17 at 15:21
1

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.

Community
  • 1
  • 1
Usul
  • 119
  • 8