Почему я получаю высоту и ширину 0? - PullRequest
6 голосов
/ 18 апреля 2011

Почему я получаю высоту и ширину 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
    }

Спасибо всем за любую помощь.

Ответы [ 3 ]

9 голосов
/ 18 апреля 2011

Вы передаете дескриптор процесса функции GetWindowRect, которая ожидает дескриптор окна.Естественно, это не удается.Вы должны отправить Notepad.MainWindowHandle вместо.

5 голосов
/ 18 апреля 2011

Возможно, вы запрашиваете размер до полного запуска блокнота. Попробуйте это:

    notePad.Start();
    notePad.WaitForInputIdle(); // Waits for notepad to finish startup
    IntPtr handle = notePad.Handle;
1 голос
/ 18 апреля 2011

Мне нравится использовать pinvoke.net для проверки всех моих PInvokes.GetWindowRect хорошо описан в: http://pinvoke.net/default.aspx/user32/GetWindowRect.html

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...