[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
IntPtr vHandle = FindWindow("Shell_TrayWnd", null);
vHandle = FindWindowEx(vHandle, IntPtr.Zero, "ToolbarWindow32", null);
单步调试vHandle=0
GetWindowRect(this.Handle, out rect);这样能得到句柄.但得到的也是0
FindWindow("高手们来看看-Windows Internet Explorer", 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));
对 PInvoke 函数“WindowsApplication2!WindowsApplication2.Form1::GetWindowRect”的调用导致堆栈不对称。原因可能是托管的 PInvoke 签名与非托管的目标签名不匹配。请检查 PInvoke 签名的调用约定和参数与非托管的目标签名是否匹配。
在vista+VS2005测试结果是显示0 0 0 0
全是0
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));
}
}
}
是不是哪写错了啊.这是全部代码
[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));
}