Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I get a height and Width of 0?

Tags:

c#

.net

winapi

Why do I get a heigh and Width of 0 with the below:

    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();
    }

Here is my definition of GetWindowRect:

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

This is my definition for 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
    }

Thanks all for any help.

like image 766
Kay Avatar asked Apr 18 '11 15:04

Kay


People also ask

Why is height 0 in HTML?

Height = 0 because the parent of the elements have 0 height, so make sure your parent, which is body here have 100% height or else.

Why does my div have 0 height?

The content is overflowing the div when it has zero height. If you add "overflow:hidden;" to your css the problem should go away.

How do you find the height of a view?

You can use the window. innerHeight property to get the viewport height, and the window. innerWidth to get its width. let viewportHeight = window.


1 Answers

You are passing a process handle to a function, GetWindowRect, that expects a window handle. Naturally, this fails. You should send Notepad.MainWindowHandle instead.

like image 99
David Heffernan Avatar answered Sep 28 '22 08:09

David Heffernan