我们曾经想过能够在我们的
计算机上将窗口隐蔽起来,不想被从身边走过的老板看见。尝试便捷的Windows隐藏并定义热键来控制它们。下面我们将演示如何通过热键,我们将会用到DllImports of Win32 API、CallBacks/Delegates,定制事件与事件的句柄。
using System;
using System.Text;
using System.Collections;
using System.Runtime.InteropServices;
namespace WindowHider
{
/// <summary>
/// Object used to control a Windows Form.
/// </summary>
public class Window
{
/// <summary>
/// Win32 API Imports
/// </summary>
[DllImport("user32.dll")] private static extern
bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")] private static extern
bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")] private static extern
bool IsIconic(IntPtr hWnd);
[DllImport("user32.dll")] private static extern
bool IsZoomed(IntPtr hWnd);
[DllImport("user32.dll")] private static extern
IntPtr GetForegroundWindow();
[DllImport("user32.dll")] private static extern
IntPtr GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId);
[DllImport("user32.dll")] private static extern
IntPtr AttachThreadInput(IntPtr idAttach, IntPtr idAttachTo, int fAttach);
/// <summary>
/// Win32 API Constants for ShowWindowAsync()
/// </summary>
private const int SW_HIDE = 0;
private const int SW_SHOWNORMAL = 1;
private const int SW_SHOWMINIMIZED = 2;
private const int SW_SHOW