2010-01-09 56 views
10

Tôi cần biết liệu thanh tác vụ Windows có bị ẩn hay không. Tôi tin rằng không có phương pháp .NET để làm điều này, và tôi cũng đã bắt gặp rất nhiều "cách ẩn và hiển thị các thanh tác vụ", nhưng tôi chưa thấy bất cứ điều gì dựa trên những gì tôi đang tìm kiếm. Tôi không quen với Windows API, vì vậy tôi thấy khó hiểu mã Windows truyền thống. Ai đó có thể vui lòng hướng dẫn tôi đến một bài viết hoặc mã loại cho biết trạng thái hiện tại của thanh tác vụ có bị ẩn hay không? Tôi đang mã hóa trong C#.Làm cách nào để xác định xem thanh tác vụ của Windows có bị ẩn hay không?

Cảm ơn.

Trả lời

10

winSharp93 trình bày một lớp trợ giúp ("Find out Size (and position) of the taskbar") có vẻ như hoạt động. Nó sử dụng số SHAppBarMessage function của Win32.

Dưới đây là đoạn code (với các bổ sung nhỏ) từ blog của mình:

using System; 
using System.Drawing; 
using System.Runtime.InteropServices; 

namespace TaskbarTest 
{ 
    public enum TaskbarPosition 
    { 
     Unknown = -1, 
     Left, 
     Top, 
     Right, 
     Bottom, 
    } 

    public sealed class Taskbar 
    { 
     private const string ClassName = "Shell_TrayWnd"; 

     public Rectangle Bounds { 
      get; 
      private set; 
     } 
     public TaskbarPosition Position { 
      get; 
      private set; 
     } 
     public Point Location { 
      get { 
       return this.Bounds.Location; 
      } 
     } 
     public Size Size { 
      get { 
       return this.Bounds.Size; 
      } 
     } 
     //Always returns false under Windows 7 
     public bool AlwaysOnTop { 
      get; 
      private set; 
     } 
     public bool AutoHide { 
      get; 
      private set; 
     } 

     public Taskbar() { 
      IntPtr taskbarHandle = User32.FindWindow(Taskbar.ClassName, null); 

      APPBARDATA data = new APPBARDATA(); 
      data.cbSize = (uint)Marshal.SizeOf(typeof(APPBARDATA)); 
      data.hWnd = taskbarHandle; 
      IntPtr result = Shell32.SHAppBarMessage(ABM.GetTaskbarPos, ref data); 
      if (result == IntPtr.Zero) 
       throw new InvalidOperationException(); 

      this.Position = (TaskbarPosition)data.uEdge; 
      this.Bounds = Rectangle.FromLTRB(data.rc.left, data.rc.top, data.rc.right, data.rc.bottom); 

      data.cbSize = (uint)Marshal.SizeOf(typeof(APPBARDATA)); 
      result = Shell32.SHAppBarMessage(ABM.GetState, ref data); 
      int state = result.ToInt32(); 
      this.AlwaysOnTop = (state & ABS.AlwaysOnTop) == ABS.AlwaysOnTop; 
      this.AutoHide = (state & ABS.Autohide) == ABS.Autohide; 
     } 
    } 

    public enum ABM : uint 
    { 
     New = 0x00000000, 
     Remove = 0x00000001, 
     QueryPos = 0x00000002, 
     SetPos = 0x00000003, 
     GetState = 0x00000004, 
     GetTaskbarPos = 0x00000005, 
     Activate = 0x00000006, 
     GetAutoHideBar = 0x00000007, 
     SetAutoHideBar = 0x00000008, 
     WindowPosChanged = 0x00000009, 
     SetState = 0x0000000A, 
    } 

    public enum ABE : uint 
    { 
     Left = 0, 
     Top = 1, 
     Right = 2, 
     Bottom = 3 
    } 

    public static class ABS 
    { 
     public const int Autohide = 0x0000001; 
     public const int AlwaysOnTop = 0x0000002; 
    } 

    public static class Shell32 
    { 
     [DllImport("shell32.dll", SetLastError = true)] 
     public static extern IntPtr SHAppBarMessage(ABM dwMessage, [In] ref APPBARDATA pData); 
    } 

    public static class User32 
    { 
     [DllImport("user32.dll", SetLastError = true)] 
     public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); 
    } 

    [StructLayout(LayoutKind.Sequential)] 
    public struct APPBARDATA 
    { 
     public uint cbSize; 
     public IntPtr hWnd; 
     public uint uCallbackMessage; 
     public ABE uEdge; 
     public RECT rc; 
     public int lParam; 
    } 

    [StructLayout(LayoutKind.Sequential)] 
    public struct RECT 
    { 
     public int left; 
     public int top; 
     public int right; 
     public int bottom; 
    } 
} 

Tác giả khẳng định nó hoạt động trên máy tính Windows 7 của mình và nó xuất hiện để làm việc trên máy XP Pro của tôi.

Đây là cách bạn có thể sử dụng nó:

Taskbar tb = new Taskbar(); 
    Console.WriteLine("w:{0}, h:{1} - hide:{2}", tb.Size.Width, tb.Size.Height, tb.AutoHide); 

đâu: tb.Size.Width và tb.Size.Height trả về chiều rộng và chiều cao của Taskbar, và tb.AutoHide trả về true nếu Taskbar là ẩn và sai nếu không.

1

SystemParametersInfo với SPI_GETWORKAREA

Truy xuất kích thước vùng làm việc trên màn hình chính. Vùng làm việc là phần màn hình không bị che khuất bởi thanh tác vụ hệ thống hoặc bằng thanh công cụ của ứng dụng trên màn hình. Tham số pvParam phải trỏ đến một cấu trúc RECT nhận tọa độ của vùng làm việc, được biểu diễn trong các tọa độ màn hình ảo.

Để nhận vùng làm việc của màn hình không phải là màn hình hiển thị chính, hãy gọi hàm GetMonitorInfo.

0

Bạn có thể sử dụng chức năng IsWindowVisible Win32.

[DllImport("user32.dll")] 
    [return: MarshalAs(UnmanagedType.Bool)] 
    static extern bool IsWindowVisible(IntPtr hWnd); 

    IntPtr hWnd = FindWindow("Shell_TrayWnd", null); 
    if (hWnd != null) IsTaskBarVisible = IsWindowVisible(hWnd); 
Các vấn đề liên quan