2011-10-30 6 views
4

Windowsフォームアプリケーションを作成しています。どのように私は窓のフォームのサイズをキャプチャするのですか?Windowsフォームのサイズを取得する

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

しかし、私のディスプレイのサイズが特定の値にハードコードされている:

現在、私は私のコードでは、このようなものを持っています。

私は私がこのようなものを使用することができ、再サイズの正方形描きたい場合ことを知っている:

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

を私は方法1を使用してのフォームのサイズをつかむための方法があります高さと幅?

私は、次のコードを試してみたが、それは動作しません...

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

答えて

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

あなたのサイズにあなたは、その親を埋めるためにコントロールをドッキングするDock = DockStyle.Fillを設定するウィンドウ

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 
} 
+0

ええと...私はこれを試しましたが、フォームがサイズ変更されているときにラインのサイズを変更しません... – BigBug

+0

ありがとうございました!それは有用だった.. upvoted – BigBug

2

ClientRectangle.Widthに設定します。

+0

は、うーん、私は私のコードの末尾に編集を追加しました - これはあなたが、私が試してみてくださいどのような意味ですか?そうでない場合は、例を挙げてください。 – BigBug

+0

あなたが書き込んだものを読んだことがありますか? 'X'はサイズではなく位置を意味します。 – SLaks

+0

助けてくれてありがとうございます - upvoted – BigBug

4

を再度。 Windowsフォームのサイズを取得するために

+0

助けをありがとう - upvoted – BigBug

9

は:

int formHeight = this.Height; 
int formWidth = this.Width; 
+0

質問に基づいてポイントに右。ありがとう! – CarinaPilar

関連する問題