开发者

Set form size to fit the controls

I have a form that contains just a single PictureBox. I have a method that resizes the box to a given size, but the form s开发者_StackOverflow社区tays the default 300x300. How do I resize the form to fit the box inside it? Surely there must be a better way than adding hardcoded title bar and margin widths to the PictureBox dimensions.


As far as I know, you can do so by enabling AutoSize (make it true), also keep an eye on the AutoSizeMode (perhaps it's better to set it to GrowAndShrink).

Hope that's useful.


The simple solution is to set the PictureBox::Dock property to Fill and change the form's ClientSize property. Which helps you get to the more efficient solution, no need to burn up a control when you can simply override the form's OnPaint() method and call e->Graphics->DrawImage().

Changing the form's AutoSize property to True works too. Set AutoSizeMode to GrowAndShrink to allow it to get smaller. And a MinimumSize would be advisable.

Beware that it is very unusual for programs to automatically change their window sizes. I can't think of a single program I commonly use that does this. The right way is to leave it up to the user to size and position the windows. Setting the AutoScroll property to True is now the right thing to do.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜