关闭Windows 8 Charm Bar

我有一个Surface Pro,我需要“锁定”作为一种Kiosk模式。 我知道有一个更新正在为“Kiosk模式”..但是我需要在此之前这样做。

我在互联网上搜索过,但看起来你无法通过屏幕上的滑动禁用魅力栏。 我已经找到了禁用触控板的方法..但不幸的是,由于这款平板电脑将在没有键盘的情况下使用,我需要禁用魅力吧。

我的新想法是将整个Charm条HWND移出屏幕数千个像素,或者,可能在其上设置一个窗口样式,以便它不会出现。 我试图使用Spy ++和一个基于EnumWindows的自定义控制台应用程序来给我一些Window句柄..但是我无法让Charm栏保持足够长的时间以使它们生效。

所以我的问题是:如何在Windows 8中找到Charm Bar的窗口句柄( HWND )? 或者,如何以其他方式获取对Charm栏的引用以便在其上抛出SetWindowLongSetWindowPos

事实上,我已经找到了一种方法(显然,没有其他人有:/)。

对于那些想知道的人来说,像“Start8”和“SkipMetroSuite”这样的软件可以通过按键来停止Charm Bar。 他们从字面上模拟按键,在紧密的循环中关闭它。

我发现(我认为是)更好的方法。

首先……一些WinAPI函数:

 using System.Runtime.InteropServices; .... private enum WindowShowStyle : uint { // find more info at http://stackoverflow.com/a/8210120/1245420 Hide = 0, ShowNormal = 1, ShowMinimized = 2, ShowMaximized = 3, ShowNormalNoActvate = 4, Show = 5, Minimize = 6, ShowNoActivate = 8, Restore = 9, ShowDefault = 10, ForceMinimized = 11 } [DllImport("user32.dll", SetLastError = true)] static extern System.IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)] static extern System.IntPtr FindWindowByCaption(System.IntPtr ZeroOnly, string lpWindowName); [DllImport("user32.dll")] static extern bool ShowWindow(System.IntPtr hWnd, WindowShowStyle nCmdShow); 

所以首先出现的问题是Charm Bar。 这个Window的标题原来是Charm Bar 。 产生一个不断寻找这个窗口并隐藏它的线程很漂亮。 所以我产生一个线程,并不断轮询它:

 System.Threading.Tasks.Task.Factory.StartNew(() => { while (true) { System.IntPtr hWndCharmBar = FindWindowByCaption(System.IntPtr.Zero, "Charm Bar"); ShowWindow(hWndCharmBar, 0); System.Threading.Thread.Sleep(100); // sleep for a bit } }); 

这很好用,并且在应用程序关闭时使Charm Bar继续工作的额外好处。 Thread.Sleep用于阻止线程粉碎CPU – 但延迟也允许Charm Bar一瞬间出现。 我还没有成功打开Charm Bar并按下一个按钮,然后线程再次隐藏它,所以这很好。 降低睡眠时间显然会使这更快。

Windows 8的另一个问题是,如果你有某种滑块(在我的应用程序中,我有一个ListBox包含图库的图像),那么你实际上可以滑到屏幕的一侧…把手指放在那里,访问任务栏…

那么..下一部分是关闭任务栏:

 IntPtr hWndTray = FindWindow("Shell_TrayWnd", null); ShowWindow(hWndTray, 0); 

..然后我再次在app关闭时显示:

 IntPtr hWndTray = FindWindow("Shell_TrayWnd", null); ShowWindow(hWndTray, 1); 

从function上讲,这就是我的应用所需的全部内容。 希望能帮助别人。

我还要注意,时钟包含在一个标题为“时钟和日期”的窗口中。

 IntPtr hWndCharmClock = FindWindowByCaption(IntPtr.Zero, "Clock and Date"); 

我做了一个小应用程序来切换魅力栏: https : //bitbucket.org/darkwingduck/charmsbartoggle/overview

这是使用SHGetPropertyStoreForWindow互操作最大化应用程序时如何禁用魅力栏。 应该很容易转换为C#:

 Imports System.Runtime.InteropServices Imports System.Runtime.CompilerServices Public Class EdgeGestureUtil Private Shared DISABLE_TOUCH_SCREEN As Guid = New Guid("32CE38B2-2C9A-41B1-9BC5-B3784394AA44") Private Shared IID_PROPERTY_STORE As Guid = New Guid("886d8eeb-8cf2-4446-8d02-cdba1dbdcf99") Private Shared VT_BOOL As Short = 11 #Region "Structures"  _ Public Structure PropertyKey Public Sub New(guid As Guid, pid As UInt32) fmtid = guid Me.pid = pid End Sub  _ Public fmtid As Guid Public pid As UInteger End Structure  _ Public Structure PropVariant  _ Public vt As Short  _ Private wReserved1 As Short  _ Private wReserved2 As Short  _ Private wReserved3 As Short  _ Private cVal As SByte  _ Private bVal As Byte  _ Private iVal As Short  _ Public uiVal As UShort  _ Private lVal As Integer  _ Private ulVal As UInteger  _ Private intVal As Integer  _ Private uintVal As UInteger  _ Private hVal As Long  _ Private uhVal As Long  _ Private fltVal As Single  _ Private dblVal As Double  _ Public boolVal As Boolean  _ Private scode As Integer 'CY cyVal;  _ Private [date] As DateTime  _ Private filetime As System.Runtime.InteropServices.ComTypes.FILETIME 'CLSID* puuid; 'CLIPDATA* pclipdata; 'BSTR bstrVal; 'BSTRBLOB bstrblobVal;  _ Private blobVal As Blob 'LPSTR pszVal;  _ Private pwszVal As IntPtr 'LPWSTR 'IUnknown* punkVal; 'IDispatch* pdispVal; ' IStream* pStream; ' IStorage* pStorage; ' LPVERSIONEDSTREAM pVersionedStream; ' LPSAFEARRAY parray; ' CAC cac; ' CAUB caub; ' CAI cai; ' CAUI caui; ' CAL cal; ' CAUL caul; ' CAH cah; ' CAUH cauh; ' CAFLT caflt; ' CADBL cadbl; ' CABOOL cabool; ' CASCODE cascode; ' CACY cacy; ' CADATE cadate; ' CAFILETIME cafiletime; ' CACLSID cauuid; ' CACLIPDATA caclipdata; ' CABSTR cabstr; ' CABSTRBLOB cabstrblob; ' CALPSTR calpstr; ' CALPWSTR calpwstr; ' CAPROPVARIANT capropvar; ' CHAR* pcVal; ' UCHAR* pbVal; ' SHORT* piVal; ' USHORT* puiVal; ' LONG* plVal; ' ULONG* pulVal; ' INT* pintVal; ' UINT* puintVal; ' FLOAT* pfltVal; ' DOUBLE* pdblVal; ' VARIANT_BOOL* pboolVal; ' DECIMAL* pdecVal; ' SCODE* pscode; ' CY* pcyVal; ' DATE* pdate; ' BSTR* pbstrVal; ' IUnknown** ppunkVal; ' IDispatch** ppdispVal; ' LPSAFEARRAY* pparray; ' PROPVARIANT* pvarVal; ' '''  ''' Helper method to gets blob data '''  Private Function GetBlob() As Byte() Dim Result As Byte() = New Byte(blobVal.Length - 1) {} Marshal.Copy(blobVal.Data, Result, 0, Result.Length) Return Result End Function '''  ''' Property value '''  Public ReadOnly Property Value() As Object Get Dim ve As VarEnum = vt Select Case ve Case VarEnum.VT_I1 Return bVal Case VarEnum.VT_I2 Return iVal Case VarEnum.VT_I4 Return lVal Case VarEnum.VT_I8 Return hVal Case VarEnum.VT_INT Return iVal Case VarEnum.VT_UI4 Return ulVal Case VarEnum.VT_LPWSTR Return Marshal.PtrToStringUni(pwszVal) Case VarEnum.VT_BLOB Return GetBlob() End Select Throw New NotImplementedException("PropVariant " + ve.ToString()) End Get End Property End Structure Friend Structure Blob Public Length As Integer Public Data As IntPtr 'Code Should Compile at warning level4 without any warnings, 'However this struct will give us Warning CS0649: Field [Fieldname] 'is never assigned to, and will always have its default value 'You can disable CS0649 in the project options but that will disable 'the warning for the whole project, it's a nice warning and we do want 'it in other places so we make a nice dummy function to keep the compiler 'happy. Private Sub FixCS0649() Length = 0 Data = IntPtr.Zero End Sub End Structure #End Region #Region "Interfaces"  _ Interface IPropertyStore  _ Sub GetCount( ByRef cProps As UInteger)  _ Sub GetAt(<[In]> iProp As UInteger, ByRef pkey As PropertyKey)  _ Sub GetValue(<[In]> ByRef key As PropertyKey, ByRef pv As PropVariant)  _ Sub SetValue(<[In]> ByRef key As PropertyKey, <[In]> ByRef pv As PropVariant)  _ Sub Commit()  _ Sub Release() End Interface #End Region #Region "Methods"  _ Private Shared Function SHGetPropertyStoreForWindow(handle As IntPtr, ByRef riid As Guid, ByRef propertyStore As IPropertyStore) As Integer End Function Public Shared Sub EnableEdgeGestures(ByVal hwnd As IntPtr, ByVal enable As Boolean) Dim pPropStore As IPropertyStore = Nothing Dim hr As Integer hr = SHGetPropertyStoreForWindow(hwnd, IID_PROPERTY_STORE, pPropStore) If hr = 0 Then Dim propKey As New PropertyKey propKey.fmtid = DISABLE_TOUCH_SCREEN propKey.pid = 2 Dim var As New PropVariant var.vt = VT_BOOL var.boolVal = enable pPropStore.SetValue(propKey, var) Marshal.FinalReleaseComObject(pPropStore) End If End Sub #End Region End Class 

更好,最简单的方法是在启动应用程序时更改2个注册表项,并在关闭时将其还原?

您只需在以下位置创建(如果不存在) EdgeUI密钥:

HKEY_CURRENT_USER \软件\微软\的Windows \ CurrentVersion \ ImmersiveShell

并添加新的DWORD键: DisableTLcornerDisableCharmsHint

禁用:

DisableTLcorner = 1 DisableCharmsHint = 1

对于启用更改为零或删除它们:

DisableTLcorner = 0 DisableCharmsHint = 0

所有这些都可以使用任何语言轻松完成!

简单的解决方案,并非完美,每次激活魅力栏时,您的应用程序都会停用,因此立即重新激活它并且魅力栏消失。 在App.xaml.cs中添加它

  private enum WindowShowStyle : uint { // find more info at http://stackoverflow.com/a/8210120/1245420 Hide = 0, ShowNormal = 1, ShowMinimized = 2, ShowMaximized = 3, ShowNormalNoActvate = 4, Show = 5, Minimize = 6, ShowNoActivate = 8, Restore = 9, ShowDefault = 10, ForceMinimized = 11 } [DllImport("user32.dll", SetLastError = true)] static extern System.IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)] static extern System.IntPtr FindWindowByCaption(System.IntPtr ZeroOnly, string lpWindowName); [DllImport("user32.dll")] static extern bool ShowWindow(System.IntPtr hWnd, WindowShowStyle nCmdShow); DispatcherTimer timer = new System.Windows.Threading.DispatcherTimer(); public App() { this.Deactivated += App_Deactivated; this.Activated += App_Activated; timer.Tick += delegate { Application.Current.MainWindow.Activate(); System.IntPtr hWndCharmBar = FindWindowByCaption(System.IntPtr.Zero, "Charm Bar"); ShowWindow(hWndCharmBar, 0); }; timer.Interval = new TimeSpan(0, 0, 0, 0, 10); } void App_Activated(object sender, EventArgs e) { timer.Stop(); } void App_Deactivated(object sender, EventArgs e) { timer.Start(); }