C#自动隐藏任务栏

C#自动隐藏任务栏

在WPF应用程序开发过程中,我们经常需要全屏窗口,但又不想将窗口设置成TopMost=True,如果是顶层窗口会导致很多其他的问题。下面我们通过C#调用Win32的API实现应用程序窗口最大化时,任务栏自动隐藏效果。

  • 代码如下
public static class Taskbar
{
  //查找窗口
  [DllImport("user32.dll")]
  private static extern int FindWindow(string className, string windowText);

  [DllImport("shell32.dll")]
  public static extern UInt32 SHAppBarMessage(UInt32 dwMessage, ref APPBARDATA pData);

  private static int Handle => FindWindow("Shell_TrayWnd", "");

  //定义一些结构,后面需要用到
   public enum AppBarMessages
   {
      New =0x00000000,
      Remove =0x00000001,
      QueryPos =0x00000002,
      SetPos =0x00000003,
      GetState =0x00000004,
      GetTaskBarPos =0x00000005,
      Activate =0x00000006,
      GetAutoHideBar =0x00000007,
      SetAutoHideBar =0x00000008,
      WindowPosChanged =0x00000009,
      SetState =0x0000000a
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct RECT
    {
       public int Left, Top, Right, Bottom;
       public RECT(int left, int top, int right, int bottom)
       {
          Left = left;
          Top = top;
          Right = right;
          Bottom = bottom;
        }

       public RECT(System.Drawing.Rectangle r) : 
       this(r.Left, r.Top, r.Right, r.Bottom) { }

       public int X
       {
          get { return Left; }
          set { Right -= (Left - value); Left = value; }
       }

       public int Y
       {
          get { return Top; }
          set { Bottom -= (Top - value); Top = value; }
       }

       public int Height
       {
          get { return Bottom - Top; }
          set { Bottom = value + Top; }
       }

       public int Width
       {
          get { return Right - Left; }
          set { Right = value + Left; }
       }

       public System.Drawing.Point Location
       {
           get { return new System.Drawing.Point(Left, Top); }
           set { X = value.X; Y = value.Y; }
        }

       public System.Drawing.Size Size
        {
           get { return new System.Drawing.Size(Width, Height); }
           set { Width = value.Width; Height = value.Height; }
        }

       public static implicit operator System.Drawing.Rectangle(RECT r)
        {
           return new System.Drawing.Rectangle(r.Left, r.Top, r.Width, r.Height);
        }

        public static implicit operator RECT(System.Drawing.Rectangle r)
        {
            return new RECT(r);
        }

        public static bool operator ==(RECT r1, RECT r2)
        {
            return r1.Equals(r2);
        }

        public static bool operator !=(RECT r1, RECT r2)
        {
            return !r1.Equals(r2);
        }

        public bool Equals(RECT r)
        {
           return r.Left == Left && 
                  r.Right == Right && 
                  r.Bottom == Bottom;
        }

        public override bool Equals(object obj)
        {
           if (obj is RECT)
              return Equals((RECT)obj);
           else if (obj is System.Drawing.Rectangle)
                return Equals(new RECT((System.Drawing.Rectangle)obj));
           return false;
         }

        public override int GetHashCode()
         {
            return ((System.Drawing.Rectangle)this).GetHashCode();
         }

         public override string ToString()
         {
            return string.Format(System.Globalization.CultureInfo.CurrentCulture, " 
            {{Left={0},Top={1},Right={2},Bottom={3}}}", Left, Top, Right, Bottom);
          }
      } 

       [StructLayout(LayoutKind.Sequential)]
        public struct APPBARDATA
        {
            public UInt32 cbSize;
            public UInt32 hWnd;
            public UInt32 uCallbackMessage;
            public UInt32 uEdge;
            public RECT rc;
            public Int32 lParam;
        }

        public enum AppBarStates
        {
            AutoHide =0x00000001,
            AlwaysOnTop =0x00000002
        }

        public static void SetTaskbarState(AppBarStates option)
        {
            APPBARDATA msgData = new APPBARDATA();
            msgData.cbSize = (uint)Marshal.SizeOf(msgData);
            msgData.hWnd = (uint)Handle;
            msgData.lParam = (int)option;
            SHAppBarMessage((UInt32)AppBarMessages.SetState, ref msgData);
        }

        public static AppBarStates GetTaskbarState()
        {
            APPBARDATA msgData = new APPBARDATA();
            msgData.cbSize = (uint)Marshal.SizeOf(msgData);
            msgData.hWnd = (uint)Handle;
            return (AppBarStates)SHAppBarMessage((UInt32)AppBarMessages.GetState, ref msgData);
        }
}

SetTaskbarState和GetTaskbarState两个静态API就是设置状态栏状态和获取状态栏状态,上述代码经过测试,可以放心使用。

你可能感兴趣的:(C#)