c# – 为什么我的高度和宽度为0?

为什么我用下面的高度和宽度为0:
static void Main(string[] args)
    {
        Process notePad = new Process();
        notePad.StartInfo.FileName = "notepad.exe";
        notePad.Start();
        IntPtr handle = notePad.Handle;

        RECT windowRect = new RECT();
        GetwindowRect(handle,ref windowRect);
        int width = windowRect.Right - windowRect.Left;
        int height = windowRect.Bottom - windowRect.Top;

        Console.WriteLine("Height: " + height + ",Width: " + width);
        Console.ReadLine();
    }

这是我对GetwindowRect的定义:

[DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool GetwindowRect(IntPtr hWnd,ref RECT lpRect);

这是我对RECT的定义:

[StructLayout(LayoutKind.Sequential)]
    public struct RECT
    {
        public int Left;        // x position of upper-left corner
        public int Top;         // y position of upper-left corner
        public int Right;       // x position of lower-right corner
        public int Bottom;      // y position of lower-right corner
    }

谢谢大家的帮助.

解决方法

您正在将进程句柄传递给期望窗口句柄的函数GetwindowRect.当然,这失败了.您应该发送Notepad.MainWindowHandle.

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...