public static void ToggleMouseCursorVisibility(MainWindow frmMain, Tools.Boolstate forced = Tools.Boolstate.Indeterminate) { if (((forced == Tools.Boolstate.True) && (!MouseCursorIsHidden)) || ((forced == Tools.Boolstate.False) && MouseCursorIsHidden)) { return; } if ((forced == Tools.Boolstate.True) || MouseCursorIsHidden) { Native.SetSystemCursor(hCursorOriginal, OCR_SYSTEM_CURSORS.OCR_NORMAL); Native.DestroyIcon(hCursorOriginal); hCursorOriginal = IntPtr.Zero; MouseCursorIsHidden = false; } else { string fileName = null; try { hCursorOriginal = frmMain.Cursor.CopyHandle(); if (curInvisibleCursor == null) { // Can't load from a memory stream because the constructor new Cursor() does not accept animated or non-monochrome cursors fileName = Path.GetTempPath() + Guid.NewGuid().ToString() + ".cur"; using (FileStream fileStream = File.Open(fileName, FileMode.Create)) { using (MemoryStream ms = new MemoryStream(Properties.Resources.blank)) { ms.WriteTo(fileStream); } fileStream.Flush(); fileStream.Close(); } curInvisibleCursor = new Cursor(Native.LoadCursorFromFile(fileName)); } Native.SetSystemCursor(curInvisibleCursor.CopyHandle(), OCR_SYSTEM_CURSORS.OCR_NORMAL); MouseCursorIsHidden = true; } catch { // swallow exception and assume cursor set failed } finally { try { if (!string.IsNullOrEmpty(fileName)) { if (File.Exists(fileName)) { File.Delete(fileName); } } } catch { } } } }
public static void ToggleWindowsTaskbarVisibility(Tools.Boolstate forced = Tools.Boolstate.Indeterminate) { try { IntPtr hTaskBar = Native.FindWindow("Shell_TrayWnd", null); if ((hTaskBar.ToInt32() == Native.INVALID_HANDLE_VALUE) || (hTaskBar == IntPtr.Zero)) { return; } bool TaskBarIsCurrentlyVisible = Native.IsWindowVisible(hTaskBar); bool WantToMakeWindowsTaskbarVisible = (forced == Tools.Boolstate.True) ? true : (forced == Tools.Boolstate.False) ? false : !TaskBarIsCurrentlyVisible; // For forced modes, if the taskbar is already visible and we're requesting to show it, then do nothing if (WantToMakeWindowsTaskbarVisible && TaskBarIsCurrentlyVisible) { return; } // For forced modes, if the taskbar is already hidden and we're requesting to hide it, then do nothing if (!WantToMakeWindowsTaskbarVisible && !TaskBarIsCurrentlyVisible) { return; } // If we're hiding the taskbar, let's take some notes on the original screen desktop work areas if (!WantToMakeWindowsTaskbarVisible) { foreach (Screen screen in Screen.AllScreens) { OriginalScreenInfo osi = new OriginalScreenInfo(); osi.screen = screen; osi.workarea = new Native.Rect(); osi.workarea.Left = screen.WorkingArea.Left; osi.workarea.Top = screen.WorkingArea.Top; osi.workarea.Right = screen.WorkingArea.Right; osi.workarea.Bottom = screen.WorkingArea.Bottom; OriginalScreens.Add(osi); } } // Show or hide the Windows taskbar Native.ShowWindow(hTaskBar, (WantToMakeWindowsTaskbarVisible) ? WindowShowStyle.ShowNoActivate : WindowShowStyle.Hide); // Keep track of the taskbar state so we don't let the user accidentally close Borderless Gaming WindowsTaskbarIsHidden = !WantToMakeWindowsTaskbarVisible; if (WantToMakeWindowsTaskbarVisible) { // If we're showing the taskbar, let's restore the original screen desktop work areas... foreach (OriginalScreenInfo osi in OriginalScreens) { Native.SystemParametersInfo(SPI.SPI_SETWORKAREA, 0, ref osi.workarea, SPIF.SPIF_SENDCHANGE); } // ...and then forget them (we don't need them anymore) OriginalScreens.Clear(); // And we need to redraw the system tray in case tray icons from other applications did something while the // taskbar was hidden. Simulating mouse movement over the system tray seems to be the best way to get this // done. RedrawWindowsSystemTrayArea(); } else { // If we're hiding the taskbar, let's set the screen desktop work area over the entire screen so that // maximizing windows works as expected. foreach (OriginalScreenInfo osi in OriginalScreens) { Native.Rect rect = new Native.Rect(); rect.Left = osi.screen.Bounds.Left; rect.Top = osi.screen.Bounds.Top; rect.Right = osi.screen.Bounds.Right; rect.Bottom = osi.screen.Bounds.Bottom; Native.SystemParametersInfo(SPI.SPI_SETWORKAREA, 0, ref rect, SPIF.SPIF_SENDCHANGE); // Note: WinAPI SystemParametersInfo() will automatically determine which screen by the rectangle we pass in. // (it's not possible to specify which screen we're referring to directly) } } } catch { } }