获取Windows窗体的大小 [英] Getting the size of a Windows Form

查看:164
本文介绍了获取Windows窗体的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个Windows窗体应用程序。我想知道我如何捕获Windows窗体的大小?



目前我有一些看起来像这样在我的代码:

 图片框显示=新的PictureBox(); 
display.Width = 360;
display.Height = 290;
this.Controls.Add(显示);
display.Image = BMP;



然而,我的显示器的大小是硬编码为一个特定的值。



我知道,如果我想画一个正方形的重新大小我可以使用这样的:

 私人矩形PlotArea; 
私人诠释偏移量= 30;
保护覆盖无效的OnPaint(PaintEventArgs的E)
{
图形G = e.Graphics;

////计算绘图区域
的位置和大小////在其中我们要绘制图形:

矩形ChartArea = ClientRectangle ;
PlotArea =新的Rectangle(ChartArea.Location,ChartArea.Size);

PlotArea.Inflate(-offset,-offset);

绘制PlotArea:
g.DrawRectangle(Pens.Black,PlotArea);
}

有什么办法,我使用的方法之一,抢的大小形成了高度和宽度?



我试过下面的代码,但它不工作...

 图片框显示=新的PictureBox(); 
display.Width = ClientRectangle.Y;
display.Height = ClientRectangle.X;
this.Controls.Add(显示);
display.Image = BMP;


解决方案

 图片框显示=新的PictureBox(); 
display.Width = ClientRectangle.Width;
display.Height = ClientRectangle.Height;
this.Controls.Add(显示);
display.Image = BMP;

当你重新调整窗口大小

 私人无效Form1_Resize(对象发件人,发送System.EventArgs)
{
控制的控制=(控制)发送;
//设置图片框的宽度和使用control.Size.Height
//并唤起注意这里control.Size.Width
}


I'm creating a windows form application. I'm wondering how do i capture the size of the windows form?

Currently i have something that looks like this in my code:

PictureBox display = new PictureBox();
display.Width = 360;
display.Height = 290;
this.Controls.Add(display);
display.Image = bmp;

However, the size of my display is hard-coded to a specific value.

I know that if i want to draw a square that re-sizes i can use something like this:

private Rectangle PlotArea;
private int offset = 30;
protected override void OnPaint(PaintEventArgs e)
    {
        Graphics g = e.Graphics;

        //// Calculate the location and size of the plot area
        //// within which we want to draw the graphics:

        Rectangle ChartArea = ClientRectangle;
        PlotArea = new Rectangle(ChartArea.Location, ChartArea.Size);

        PlotArea.Inflate(-offset, -offset);

        Draw PlotArea:
        g.DrawRectangle(Pens.Black, PlotArea);
    }

Is there any way for me to use method one and grab the size of the form for Height and Width?

I've tried the following code but it doesn't work...

        PictureBox display = new PictureBox();
        display.Width = ClientRectangle.Y;
        display.Height = ClientRectangle.X;
        this.Controls.Add(display);
        display.Image = bmp;

解决方案

    PictureBox display = new PictureBox();
    display.Width = ClientRectangle.Width;
    display.Height = ClientRectangle.Height;
    this.Controls.Add(display);
    display.Image = bmp;

when you re size the window

private void Form1_Resize(object sender, System.EventArgs e)
{
   Control control = (Control)sender;
   //set the PictureBox width and heigh here    using control.Size.Height 
   // and control.Size.Width
}

这篇关于获取Windows窗体的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆