none
右下角弹出窗口,当任务栏在左侧和顶部的时候,位置不对了..... RRS feed

答案

  • 嗯,你是怎么样在左下角弹出窗口。 我这样写,弹出窗位置永远是在左下角的:

        public MainWindow()
        {
          InitializeComponent();
    
          this.Top = SystemParameters.PrimaryScreenHeight - this.Height - 40;
          this.Left = SystemParameters.PrimaryScreenWidth - this.Width - 90;
        }

    如果你再一句我在这个帖子里的答案 http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/97d1ed9c-23fa-48c5-93d7-a9a8d039de07,去获得当前Task Bar的位置,你也可以动态的给出要多减去的 40 高度和 90 宽度。这样就能做到永远贴合屏幕左下,并且不会被Task Bar覆盖:

      public partial class MainWindow : Window
      {
        [DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    
        [DllImport("SHELL32", CallingConvention = CallingConvention.StdCall)]
        public static extern int SHAppBarMessage(int dwMessage, ref APPBARDATA pData);
    
        public MainWindow()
        {
          InitializeComponent();
    
          this.Top = SystemParameters.PrimaryScreenHeight - this.Height;
          this.Left = SystemParameters.PrimaryScreenWidth - this.Width;
          switch (GetTaskBarPos())
          {
            case ABEdge.ABE_BOTTOM:
              this.Top -= 40;
              break;
            case ABEdge.ABE_RIGHT:
              this.Left -= 90;
              break;
          }
        }
    
        private ABEdge GetTaskBarPos()
        {
          IntPtr hwnd = FindWindow("Shell_TrayWnd", null);
          if (hwnd != null)
          {
            APPBARDATA abd = new APPBARDATA();
            abd.cbSize = Marshal.SizeOf(abd);
            abd.hWnd = hwnd;
            SHAppBarMessage((int)ABMsg.ABM_GETTASKBARPOS, ref abd);
            int uEdge = GetEdge(abd.rc);
    
            return (ABEdge)uEdge;
          }
          return ABEdge.ABE_BOTTOM;
        }
    
        private int GetEdge(RECT rc)
        {
          int uEdge = -1;
          if (rc.top == rc.left && rc.bottom > rc.right)
            uEdge = (int)ABEdge.ABE_LEFT;
          else if (rc.top == rc.left && rc.bottom < rc.right)
            uEdge = (int)ABEdge.ABE_TOP;
          else if (rc.top > rc.left)
            uEdge = (int)ABEdge.ABE_BOTTOM;
          else
            uEdge = (int)ABEdge.ABE_RIGHT;
          return uEdge;
        }
      }
    
      [StructLayout(LayoutKind.Sequential)]
      public struct APPBARDATA
      {
        public int cbSize;
        public IntPtr hWnd;
        public int uCallbackMessage;
        public int uEdge;
        public RECT rc;
        public bool lParam;
      }
    
      [StructLayout(LayoutKind.Sequential)]
      public struct RECT
      {
        public int left;
        public int top;
    
        public int right;
        public int bottom;
      }
    
      public enum ABEdge
      {
        ABE_LEFT = 0,
        ABE_TOP = 1,
        ABE_RIGHT = 2,
        ABE_BOTTOM = 3
      }
    
      public enum ABMsg
      {
        ABM_NEW = 0,
        ABM_REMOVE = 1,
        ABM_QUERYPOS = 2,
        ABM_SETPOS = 3,
        ABM_GETSTATE = 4,
        ABM_GETTASKBARPOS = 5,
        ABM_ACTIVATE = 6,
        ABM_GETAUTOHIDEBAR = 7,
        ABM_SETAUTOHIDEBAR = 8,
        ABM_WINDOWPOSCHANGED = 9,
        ABM_SETSTATE = 10
      }

    Sincerely,

    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us


    2012年3月27日 3:07
    版主

全部回复

  • 嗯,你是怎么样在左下角弹出窗口。 我这样写,弹出窗位置永远是在左下角的:

        public MainWindow()
        {
          InitializeComponent();
    
          this.Top = SystemParameters.PrimaryScreenHeight - this.Height - 40;
          this.Left = SystemParameters.PrimaryScreenWidth - this.Width - 90;
        }

    如果你再一句我在这个帖子里的答案 http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/97d1ed9c-23fa-48c5-93d7-a9a8d039de07,去获得当前Task Bar的位置,你也可以动态的给出要多减去的 40 高度和 90 宽度。这样就能做到永远贴合屏幕左下,并且不会被Task Bar覆盖:

      public partial class MainWindow : Window
      {
        [DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    
        [DllImport("SHELL32", CallingConvention = CallingConvention.StdCall)]
        public static extern int SHAppBarMessage(int dwMessage, ref APPBARDATA pData);
    
        public MainWindow()
        {
          InitializeComponent();
    
          this.Top = SystemParameters.PrimaryScreenHeight - this.Height;
          this.Left = SystemParameters.PrimaryScreenWidth - this.Width;
          switch (GetTaskBarPos())
          {
            case ABEdge.ABE_BOTTOM:
              this.Top -= 40;
              break;
            case ABEdge.ABE_RIGHT:
              this.Left -= 90;
              break;
          }
        }
    
        private ABEdge GetTaskBarPos()
        {
          IntPtr hwnd = FindWindow("Shell_TrayWnd", null);
          if (hwnd != null)
          {
            APPBARDATA abd = new APPBARDATA();
            abd.cbSize = Marshal.SizeOf(abd);
            abd.hWnd = hwnd;
            SHAppBarMessage((int)ABMsg.ABM_GETTASKBARPOS, ref abd);
            int uEdge = GetEdge(abd.rc);
    
            return (ABEdge)uEdge;
          }
          return ABEdge.ABE_BOTTOM;
        }
    
        private int GetEdge(RECT rc)
        {
          int uEdge = -1;
          if (rc.top == rc.left && rc.bottom > rc.right)
            uEdge = (int)ABEdge.ABE_LEFT;
          else if (rc.top == rc.left && rc.bottom < rc.right)
            uEdge = (int)ABEdge.ABE_TOP;
          else if (rc.top > rc.left)
            uEdge = (int)ABEdge.ABE_BOTTOM;
          else
            uEdge = (int)ABEdge.ABE_RIGHT;
          return uEdge;
        }
      }
    
      [StructLayout(LayoutKind.Sequential)]
      public struct APPBARDATA
      {
        public int cbSize;
        public IntPtr hWnd;
        public int uCallbackMessage;
        public int uEdge;
        public RECT rc;
        public bool lParam;
      }
    
      [StructLayout(LayoutKind.Sequential)]
      public struct RECT
      {
        public int left;
        public int top;
    
        public int right;
        public int bottom;
      }
    
      public enum ABEdge
      {
        ABE_LEFT = 0,
        ABE_TOP = 1,
        ABE_RIGHT = 2,
        ABE_BOTTOM = 3
      }
    
      public enum ABMsg
      {
        ABM_NEW = 0,
        ABM_REMOVE = 1,
        ABM_QUERYPOS = 2,
        ABM_SETPOS = 3,
        ABM_GETSTATE = 4,
        ABM_GETTASKBARPOS = 5,
        ABM_ACTIVATE = 6,
        ABM_GETAUTOHIDEBAR = 7,
        ABM_SETAUTOHIDEBAR = 8,
        ABM_WINDOWPOSCHANGED = 9,
        ABM_SETSTATE = 10
      }

    Sincerely,

    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us


    2012年3月27日 3:07
    版主
  • 谢谢....

    2012年3月27日 9:18