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

标签 c# .net winapi

为什么我的高度和宽度为 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

关于c# - 为什么我的高度和宽度为 0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5705483/

相关文章:

windows - 计划任务是否收到 WM_QUERYENDSESSION 消息?

c# - 动态文本转化为图像

c# - 如何进行正则表达式平衡匹配,当括号可能是 'escaped'

.net - 将 native C++ COM .dll 替换为 .NET COM .dll

c++ - 使用仿函数子类化窗口 (Win32)

c++ - 如何界定编辑框的容量?

c# - 如何更改 DataGridComboBoxColumn 样式?

c# - Winforms ToolTip 获取和更改所有分配的工具提示

.net - 斯坦福解析器或 dotnet 中的替代品

c# - .Net 可移植类库和 Microsoft.WindowsAzure.Mobile.Service.EntityData