开发者

C# Screen resolution and Form display

开发者 https://www.devze.com 2023-03-15 20:06 出处:网络
I have a C# WinForms application and when I give the executable to different users the application displays in different sizes (based on their screen resolution). Some of the parts of the application

I have a C# WinForms application and when I give the executable to different users the application displays in different sizes (based on their screen resolution). Some of the parts of the application can't be seen开发者_运维知识库.

Is there anyway to auto-size the window based on the screen resolution, or is there another approach?

EDIT : furthermore it appears in different styles under different Operating systems, is there away to standardize its design ?


It sounds like you have specified your controls with absolute positioning and other layout defaults. In order to make a WinForms application that looks and feels the same and behaves correctly in various resizing scenarios, you need to utilize the Anchor and Dock properties. Arranging controls in WinForms can be a tiring process, but MSDN includes some nice How To's on the subject.

I would also suggest following along with this TechRepublic article, which covers the difference between Anchoring and Docking, and shows you visually what each property accomplishes:

C# Screen resolution and Form display


You can use Control.ScaleControl and Control.Scale

private void MainForm_Load( object sender, EventArgs e )
{
    float width_ratio = (Screen.PrimaryScreen.Bounds.Width / 1280);
    float heigh_ratio = (Screen.PrimaryScreen.Bounds.Height / 800f);

    SizeF scale = new SizeF(width_ratio, heigh_ratio);

    this.Scale(scale);

   //And for font size
   foreach (Control control in this.Controls)
   {
      control.Font = new Font("Microsoft Sans Serif", c.Font.SizeInPoints * heigh_ratio * width_ratio);
   }
}

In the case when the development platform resolution is 1280x800

According to @sixlettervariables 's answer Docking and anchoring will help of course.


Try this

private void MainForm_Load( object sender, EventArgs e ) 
  { 
     this.Size = Screen.PrimaryScreen.WorkingArea.Size 
  }
0

精彩评论

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