WinForm, how to control the position of a control changes with the size of the form

Source: Internet
Author: User

 WinForm, how to control the position of a control changes with the size of the form There are 3 ways to do this:

Method 1
[CSharp]View Plaincopy
  1. Using System;
  2. Using System.Collections.Generic;
  3. Using System.ComponentModel;
  4. Using System.Data;
  5. Using System.Drawing;
  6. Using System.Text;
  7. Using System.Windows.Forms;
  8. Namespace Markprinter
  9. {
  10. Public partial class Resizetest:form
  11. {
  12. public float X;
  13. public float Y;
  14. //public float y;
  15. Public resizetest ()
  16. {
  17. InitializeComponent ();
  18. }
  19. private void Settag (Control cons)
  20. {
  21. foreach (Control con in cons. Controls)
  22. {
  23. Con. Tag = con. Width + ":" + con. Height + ":" + con. Left + ":" + con. Top + ":" + con.  Font.Size;
  24. if (con. Controls.Count > 0)
  25. Settag (con);
  26. }
  27. }
  28. private void Setcontrols (float newx, float newy, Control cons)
  29. {
  30. foreach (Control con in cons. Controls)
  31. {
  32. string[] MyTag = con. Tag.tostring ().  Split (new char[] { ': '});
  33. float a = Convert.tosingle (mytag[0]) * NEWX;
  34. Con.  Width = (int) A;
  35. A = Convert.tosingle (mytag[1]) * NEWY;
  36. Con.  Height = (int) (a);
  37. A = Convert.tosingle (mytag[2]) * NEWX;
  38. Con.  left = (int) (a);
  39. A = Convert.tosingle (mytag[3]) * NEWY;
  40. Con.  Top = (int) (a);
  41. Single currentsize = Convert.tosingle (mytag[4]) * NEWY;
  42. //Change font size
  43. Con. Font = new Font (con. Font.Name, CurrentSize, con. Font.style, Con.  Font.unit);
  44. if (con. Controls.Count > 0)
  45. {
  46. Setcontrols (newx, newy, con);
  47. }
  48. }
  49. }
  50. void Form1_Resize (object sender, EventArgs e)
  51. {
  52. //throw new Exception ("The method or operation is not implemented.");
  53. float newx = (this.  Width)/X;
  54. //Float Newy = (this.  Height-this.statusstrip1.height)/(Y-Y);
  55. float Newy = this .  height/y;
  56. Setcontrols (Newx, Newy, this );
  57. This . Text = This . Width.tostring () + "" + This .  Height.tostring ();
  58. }
  59. private void Resizetest_load (object sender, EventArgs e)
  60. {
  61. This .  Resize + = new EventHandler (form1_resize);
  62. X = This .  Width;
  63. Y = This .  Height;
  64. //y = this.statusStrip1.Height;
  65. Settag (this);
  66. }
  67. }
  68. }




Method 2

[CSharp]View Plaincopy
  1. Using System;
  2. Using System.Collections.Generic;
  3. Using System.ComponentModel;
  4. Using System.Data;
  5. Using System.Drawing;
  6. Using System.Text;
  7. Using System.Windows.Forms;
  8. Namespace Markprinter
  9. {
  10. Public partial class Resizetest:form
  11. {
  12. public float X;
  13. public float Y;
  14. public float y;
  15. Public resizetest ()
  16. {
  17. InitializeComponent ();
  18. }
  19. private void Resizetest_load (object sender, EventArgs e)
  20. {
  21. Autoscale (this);
  22. }
  23. // <summary>
  24. /// controls automatically scaled with the form
  25. // </summary>
  26. /// <param name= "frm" ></param>
  27. public static void Autoscale (Form frm)
  28. {
  29. frm. Tag = frm. Width.tostring () + "," + frm.  Height.tostring ();
  30. frm.  SizeChanged + = new EventHandler (frm_sizechanged);
  31. }
  32. static void frm_sizechanged (object sender, EventArgs e)
  33. {
  34. string[] tmp = ((Form) sender). Tag.tostring ().  Split (', ');
  35. Float width = (float) ((Form) sender).  Width/(float) convert.toint16 (tmp[0]);
  36. float heigth = (float) ((Form) sender).  Height/(float) convert.toint16 (tmp[1]);
  37. (Form) sender). Tag = ((Form) sender). Width.tostring () + "," + ((Form) sender).  Height;
  38. foreach (Control control in ( Form) sender). Controls)
  39. {
  40. Control.  Scale (new SizeF (width, heigth));
  41. }
  42. }
  43. }
  44. }
      1. --Reprint Office: http://blog.csdn.net/xd43100678/article/details/7899047

WinForm, how to control the position of a control changes with the size of the form

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.