[DllImport("user32.dll", SetLastError = true)]
        static extern Rectangle GetWindowRect(IntPtr hWnd, out  RECT lpRect);
        internal struct RECT        
      {
            public int left;
            public int top;
            public int right;
            public int bottom;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            IntPtr vHandle = FindWindow("Shell_TrayWnd", null);
            vHandle = FindWindowEx(vHandle, IntPtr.Zero, "ToolbarWindow32", null);
            RECT rect = new RECT();
            GetWindowRect(vHandle, out  rect);
            MessageBox.Show(string.Format("x={0}, y={1}, width={2}, height={3}", rect.left, rect.top, rect.right, rect.bottom));
}我想得到ToolbarWindow32大小.怎么得出来的全是0呢
这样写GetWindowRect(this.Handle, out  rect);得到的也是0

解决方案 »

  1.   


    IntPtr vHandle = FindWindow("Shell_TrayWnd", null);
                vHandle = FindWindowEx(vHandle, IntPtr.Zero, "ToolbarWindow32", null);
    单步调试vHandle=0
      

  2.   

    ("Shell_TrayWnd",  这个 是你当前窗体的哪个名称. 你确实是否对了?
      

  3.   


    GetWindowRect(this.Handle, out  rect);这样能得到句柄.但得到的也是0
      

  4.   

    vHandle = 0那当然也是0啦 
      

  5.   

    Shell_TrayWnd这个是托盘的类名啊
      

  6.   

    ?? 比如 我打开你这个网址,获取这个窗体的进程句柄
    FindWindow("高手们来看看-Windows Internet Explorer", null);
      

  7.   

    MessageBox.Show(string.Format("x={0}, y={1}, width={2}, height={3}", rect.left, rect.top, rect.right, rect.bottom));怎么都是0呢
      

  8.   

    问题应该在这:不是在第2层上,ToolbarWindow32窗口应该是第四层上。IntPtr vHandle = FindWindow("Shell_TrayWnd", null);
    vHandle = FindWindowEx(vHandle, IntPtr.Zero, "TrayNotifyWnd", null);
    vHandle = FindWindowEx(vHandle, IntPtr.Zero, "SysPager", null);
    vHandle = FindWindowEx(vHandle, IntPtr.Zero, "ToolbarWindow32", null);
    RECT rect = new RECT();
    GetWindowRect(vHandle, ref  rect);
    MessageBox.Show(str+string.Format("x={0}, y={1}, width={2}, height={3}", rect.left, rect.top, rect.right, rect.bottom));
      

  9.   

    12楼代码在 WINXP + VS2005测试通过。你可以用VS2005中的spy++工具查看。
      

  10.   

    我在WINXP + VS2005测试 报错GetWindowRect(vHandle, ref  rect);
    对 PInvoke 函数“WindowsApplication2!WindowsApplication2.Form1::GetWindowRect”的调用导致堆栈不对称。原因可能是托管的 PInvoke 签名与非托管的目标签名不匹配。请检查 PInvoke 签名的调用约定和参数与非托管的目标签名是否匹配。
    在vista+VS2005测试结果是显示0 0 0 0
    全是0
      

  11.   


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    namespace WindowsApplication1
    {
        public partial class Form1 : Form
        {
            
            public Form1()
            {
                InitializeComponent();
                
            }
            public struct RECT
            {
                public int left;
                public int top;
                public int right;
                public int bottom;
            }
            [DllImport("user32.DLL")]
            public static extern IntPtr FindWindow(string lpszClass, string lpszWindow);
            [DllImport("user32.DLL")]
            public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
            [DllImport("user32.dll", SetLastError = true)]
            static extern Rectangle GetWindowRect(IntPtr hWnd, ref  RECT rect);
            private void button1_Click(object sender, EventArgs e)
            {
                IntPtr vHandle = FindWindow("Shell_TrayWnd", null);
                vHandle = FindWindowEx(vHandle, IntPtr.Zero, "TrayNotifyWnd", null);
                vHandle = FindWindowEx(vHandle, IntPtr.Zero, "SysPager", null);
                vHandle = FindWindowEx(vHandle, IntPtr.Zero, "ToolbarWindow32", null);
                RECT rect = new RECT();
                GetWindowRect(vHandle, ref  rect);
                MessageBox.Show(string.Format("x={0}, y={1}, width={2}, height={3}", rect.left, rect.top, rect.right, rect.bottom));
            }
        }
    }
    是不是哪写错了啊.这是全部代码
      

  12.   

    FindWindow 不要使用这个,enumwindows不可以嘛?
      

  13.   

    GetWindowRect 声明类型不对,应该是bool
    [DllImport("user32.dll")]
    static extern IntPtr FindWindow(string lpClassName, string lpWindowName);[DllImport("user32.dll")]
    static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);[DllImport("user32.dll")]
    public static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);public struct RECT
    {
        public int left;
        public int top;
        public int right;
        public int bottom;
    }private void button1_Click(object sender, EventArgs e)
    {
        IntPtr vHandle = FindWindow("Shell_TrayWnd", null);
        vHandle = FindWindowEx(vHandle, IntPtr.Zero, "TrayNotifyWnd", null);
        vHandle = FindWindowEx(vHandle, IntPtr.Zero, "SysPager", null);
        vHandle = FindWindowEx(vHandle, IntPtr.Zero, "ToolbarWindow32", null);
        RECT rect = new RECT();
        GetWindowRect(vHandle, ref  rect);
        MessageBox.Show(string.Format("x={0}, y={1}, width={2}, height={3}", rect.left, rect.top, rect.right, rect.bottom));
    }