C# Get Width And Height of a Window Using Windows API | C# Examples
10 Oct

C# Get Width And Height of a Window Using Windows API

This example shows how to get width and height of a window using Windows API.

Usage :

            RECT rect;
            UserControl uc = new UserControl();
            if (GetWindowRect(new HandleRef(uc, uc.Handle), out rect))
            {                
                //To do anything  
                int width = rect.Right - rect.Left;
                int height = rect.Bottom - rect.Top;
            }
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool GetWindowRect(HandleRef hWnd, out RECT lpRect);

        [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
        }

Leave a Reply

Your email address will not be published. Required fields are marked *