2011-10-30 2 views
4

Windows Forms 응용 프로그램을 만들고 있습니다. Windows 양식의 크기를 어떻게 캡처합니까?Windows Form의 크기 가져 오기

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

날 방법 중 하나를 사용하여 폼의 크기를 잡아 할 수있는 방법이 있나요을 높이와 너비? ClientRectangle.Width에 나는 다음과 같은 코드를 시도했지만, 그것이 작동하지 않습니다

...

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을 설정합니다. 윈도우 폼의 크기를 얻기 를 들어

+0

흠 ... 나는 이것을 시도했지만 양식을 다시 크기 때 줄을 다시하지 않습니다 ... – BigBug

+0

고마워요! 그게 도움이되었다 .. upvoted – BigBug

2

세트를. 당신이 크기를 창

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

방금 ​​쓴 것을 읽었습니까? 'X'는 크기가 아니라 위치를 의미합니다. – SLaks

+0

도움을 주셔서 감사합니다 - upvoted – BigBug

4

을 다시 할 때

+0

도움말 주셔서 감사합니다 - upvoted – BigBug

9

는 :

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

질문을 기반으로 요점. 감사! – CarinaPilar