c# - Ratio between height and width of the form should be constant -


i have form 2 pictureboxes , buttons, , want them organize depending on screen resolution. here code:

public partial class form1 : form     {                  int systemwidth = system.windows.forms.screen.primaryscreen.bounds.width;         int systemheight = system.windows.forms.screen.primaryscreen.bounds.height;         double ratio;          public form1()         {                            initializecomponent();             this.width = systemwidth-200;             this.height = systemheight-200;            // this.windowstate = formwindowstate.maximized;              ratio= systemwidth / systemheight;          }          private void form1_resize(object sender, eventargs e)         {                        anordnen();           }          private void anordnen()         {             pic1.width = this.clientsize.width / 2 - 30;             pic2.width = this.clientsize.width / 2 - 30;             pic1.left = 20;             pic2.left = pic1.right + 20;              btnvergleichen.left = pic1.right + 10 - btnvergleichen.width / 2;             btneinlesen1.left = pic1.left + pic1.width / 2 - btneinlesen1.width / 2;             btnbewerten1.left = pic1.left + pic1.width / 2 - btnbewerten1.width / 2;             btneinlesen2.left = pic2.left + pic2.width / 2 - btneinlesen2.width / 2;             btnbewerten2.left = pic2.left + pic2.width / 2 - btnbewerten2.width / 2;         } 

now want ratio between systemwidth , systemheight stay same. if enlarge width of form, height should automatically bigger. tries failed.

try code:

public partial class form1 : form {              int systemwidth = system.windows.forms.screen.primaryscreen.bounds.width;     int systemheight = system.windows.forms.screen.primaryscreen.bounds.height;     private readonly double ratio;     private int oldwidth;     private int oldheight;      public form1()     {                        initializecomponent();          ratio = (double)systemwidth / systemheight;         size = new size((int)(systemwidth - 200 * ratio), systemheight - 200);     }      protected override void onresizebegin(eventargs e)     {         oldwidth = width;         oldheight = height;         base.onresizebegin(e);     }      protected override void onresize(eventargs e)     {         int dw = width - oldwidth;         int dh = height - oldheight;         if (math.abs(dw) < math.abs(dh * ratio))             width = (int)(oldwidth + dh * ratio);         else             height = (int)(oldheight + dw / ratio);         base.onresize(e);     } } 

edit
condition if (dw > dh * ratio) replaced if (math.abs(dw) < math.abs(dh * ratio)).


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -