简体   繁体   中英

How to detect tablet mode

I'm using the following code to detect if a user is in tablet mode or not. I'm on a Surface Pro and when I decouple the keyboard and make the PC into a tablet, IsTabletMode returns true (which it should.) When I use the "Tablet Mode" button without decoupling the screen, IsTabletMode always returns false. Has anyone experienced this and How can I resolve it?

/*
 * Credit to Cheese Lover
 * Retrieved From: http://stackoverflow.com/questions/31153664/how-can-i-detect-when-window-10-enters-tablet-mode-in-a-windows-forms-applicatio
 */
public static class TabletPCSupport
{
   private static readonly int SM_CONVERTIBLESLATEMODE = 0x2003;
   private static readonly int SM_TABLETPC = 0x56;

   private Boolean isTabletPC = false;

   public Boolean SupportsTabletMode { get { return isTabletPC; }}

   public 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;
   }
}

Edit 2: The SM_TABLETPC is only supported by Windows XP Tablet PC Edition and Windows Vista. There doesn't seem to be any reference to Windows 10 here: https://msdn.microsoft.com/en-us/library/windows/desktop/ms700675(v=vs.85).aspx

You can use this: GetSystemMetrics(SM_CONVERTIBLESLATEMODE). A “0” returned means it is in tablet mode. A “1” returned means it is in non-tablet mode. https://software.intel.com/en-us/articles/how-to-write-a-2-in-1-aware-application

Can you replace the QueryTabletMode method with this:

   private static Boolean QueryTabletMode ()
   {
       int state = GetSystemMetrics(SM_CONVERTIBLESLATEMODE);
       return (state == 0);
   }

Edit: You might need to check this periodically as there's no event to see if the PC's tablet mode was turned on

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