获取任务栏大小

Posted on 2019-01-19 21:10  努力成长静待花开  阅读(433)  评论(0编辑  收藏  举报

实现效果;

  

知识运用:

API  FindWindow函数  //可用来返回任务栏所在窗口Shell_TrayWnd的句柄

   IpClassName :要返回的窗口的类名称字符串指针

  IpWindowName:要返回的窗口的名称

  GetWindowRect函数  //获取任何一个窗口的矩阵范围的函数

  hwnd:目标窗口的句柄

  IpRect:存储窗口区域的Rectangle结构变量

  

实现代码:

        [DllImport("user32.dll")]
        public static extern int FindWindow(string IpClassName,string IpWindowName);
        [DllImport("user32.dll")]
        public static extern int GetWindowRect(int hwnd,ref Rectangle IpRect);
        Rectangle rect;
        private void button1_Click(object sender, EventArgs e)
        {
            if ((GetWindowRect(FindWindow("Shell_TrayWnd", null), ref rect)) == 0)
                return;
            else
            {
                textBox1.Text = Convert.ToString(rect.Left);
                textBox2.Text = Convert.ToString(rect.Top);
                textBox3.Text = Convert.ToString(rect.Right);
                textBox4.Text = Convert.ToString(rect.Bottom);
            }
        }