问题描述
更新
虽然不是最优雅的解决方案,但似乎可行的一种方法是查看相关的注册表值.这是使用 WMI 执行此操作的示例.如果有比这更好的解决方案,我很乐意听取任何人的意见.
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");
}
}
}
原始问题
我有兴趣根据用户是否使用新的 Windows 10 Continuum 功能对我的 Windows 窗体应用程序进行一些优化.
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.
在 https://msdn.microsoft.com/en-us/library/windows/hardware/dn917883(v=vs.85).aspx (即查看当前视图的UserInteractionMode查看它是 UserInteractionMode.Mouse 还是 UserInteractionMode.Touch),但我不确定是否或如何在 Windows 窗体中执行相同操作.
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.
有什么方法可以从我的 Windows 窗体应用程序中调用必要的 UWP API,或者我可以使用一些等效的 Windows 窗体吗?
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?
推荐答案
要获取系统是否处于平板电脑模式,请像这样查询系统指标 ConvertibleSlateMode(未测试,但早在 XP 上应该可以正常工作):
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;
}
}
(文档 这里)
这篇关于如何在 Windows 窗体应用程序中检测 Windows 10 何时进入平板电脑模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!