我正在使用以下代码来检测用户是否处于平板模式。我在Surface Pro上使用时,当我分离键盘并将PC变成平板电脑时,
IsTabletMode
返回true(这是应该的)。但是,当我在没有分离屏幕的情况下使用"平板电脑模式"按钮时,IsTabletMode
始终返回false。有人遇到过这种情况吗?我该如何解决?/*
* Credit to Cheese Lover
* Retrieved From: https://dev59.com/Wl0Z5IYBdhLWcg3wtSLr
*/
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;
}
}