Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,897 questions

51,828 answers

573 users

How to get form size using windows API in C#

1 Answer

0 votes
using System.Runtime.InteropServices;

namespace WinFormsApp1
{
    public partial class Form1 : Form
    {
        [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 upper left 
            public int Top;     // y upper left 
            public int Right;   // x lower right
            public int Bottom;  // y lower right
        }
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            RECT rect;
            if (GetWindowRect(new HandleRef(this, this.Handle), out rect)) {
                int width = rect.Right - rect.Left;
                int height = rect.Bottom - rect.Top;

                label1.Text = "width: " + width.ToString() + " height: " + height.ToString();
            }
        }
    }
}




/*
 * run:
 *
 * "width: 816 height: 489"
 * 
 */

 



answered Sep 5, 2023 by avibootz
edited Sep 6, 2023 by avibootz

Related questions

...