winform中,如何控制控件位置随窗体的大小改变而改变

winform中,如何控制控件位置随窗体的大小改变而改变

有如下3种方法:

方法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. //改变字体大小
  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. }

方法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. /// 控件随窗体自动缩放
  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. --转载处:http://blog.csdn.net/xd43100678/article/details/7899047
时间: 2024-07-30 13:51:30

winform中,如何控制控件位置随窗体的大小改变而改变的相关文章

WPF中嵌入WinForm中的webbrowser控件

原文:WPF中嵌入WinForm中的webbrowser控件 使用VS2008创建WPF应用程序,需使用webbrowser.从工具箱中添加WPF组件中的webbrowser发现其中有很多属性事件不能使用.决定还是使用WinForm中的webbrowser.要想在WPF中使用WinForm控件,查看MSDN,需经过以下步骤. 创建名为 HostingWfInWpf 的 WPF 应用程序项目. 在解决方案资源管理器中,添加一个对名为 WindowsFormsIntegration.dll 的 Wi

WinForm中调用WPF控件

WinForm中调用WPF控件 在WinForm中可以使用WPF中的控件,或者由WPF创建的自定义控件: 步骤1:创建WinForm工程: 步骤2:在WinForm工程的解决方案资源管理器中,在刚刚创建的WinForm解决方案中新建或者添加现有的WPF用户控件工程: 步骤3:在WPF中创建自定义的控件,或者添加WPF控件到面板上: 步骤4:在WinForm面板上添加ElementHost控件(工具箱中): 步骤5:生成解决方案: 步骤6:在刚刚的ElementHost中的Child属性中添加刚刚

在C#中使用Panel控件实现在一个窗体中嵌套另一个窗体

在C#中使用Panel控件实现在一个窗体中嵌套另一个窗体ShowAllPage sAllPage = new ShowAllPage();          //  sAllPage.FormBorderStyle = FormBorderStyle.None;           // sAllPage.Dock = DockStyle.Fill;            sAllPage.TopLevel = false;            this.MainPanel.Controls.

Winform中使用用户控件实现带行数和标尺的RichTextBox(附代码下载)

场景 RichTextBox控件允许用户输入和编辑文本的同时提供了比普通的TextBox控件更高级的格式特征. 效果 注: 博客主页: https://blog.csdn.net/badao_liumang_qizhi 关注公众号 霸道的程序猿 获取编程相关电子书.教程推送与免费下载. 实现 新建一个用户控件GuageRichTextBox. 编辑用户控件,相当于自己定义了一个控件,和其他控件一样在窗体中使用,是一个类. 然后打开其设计页面,放置一个RichTextBox 然后进入其代码 usi

WINFORM中加入WPF控件并绑定数据源实现跨线程自动更新

1. WINFORM中添加两个ElementHost,一个放WPF的Button,一个放WPF的TextBox.其中TextBox与数据源绑定,实现跨线程也可以自动更新,而不会出现WINFORM的TextBox控件与数据源绑定后,存在子线程中更新数据源报错(跨线程更新控件)的情况. using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System

c#在WinForm中重写ProgressBar控件(带%的显示)

#region 定义textProgressBar控件的类 namespace csPublish { [ToolboxItem(true)] class textProgressBar : System.Windows.Forms.ProgressBar { [System.Runtime.InteropServices.DllImport("user32.dll ")] static extern IntPtr GetWindowDC(IntPtr hWnd); [System.R

winform 中调用用户控件中 嵌套用户控件的事件

工作了很久,一直没有很深入的了解C#中的委托和事件. 来到新公司,主要的工作就是使用委托和事件操作数据.一下子不知道该如何下手.各方请教大神,得出下面的方法 1. 在A控件中定义一个事件,B控件触发的时候,调用一下这个事件.然后再在A的父级控件中写方法,+=的方式把方法委托给A的事件就行了 窗体: private void Form1_Load(object sender, EventArgs e)        {            UC_Content content = new UC_

WinForm中按钮等控件的背景渐变色重绘

注:brush通过起止坐标来控制重绘范围及方向.比如从上到下渐变时,brush第二个Point参数是左下角坐标. 1 private void PaintGradientBackground(Button btn) 2 { 3 Bitmap newGradientBackImg = new Bitmap(btn.Width, btn.Height); 4 LinearGradientBrush brush = new LinearGradientBrush(new PointF(0, 0), n

Winform中修改WebBrowser控件User-Agent的方法(已经测试成功)

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; using System.Reflection; namespace WindowsFormsApplication2 { public class UserAgentHelper { priva