在winforms中,用户如何在运行时调整控件的大小

jw5wzhpr  于 2023-11-21  发布在  其他
关注(0)|答案(6)|浏览(274)

假设我有一个图片框。
现在我想要的是,用户应该能够随意调整pictureBox的大小。然而,我甚至不知道如何开始这件事。我已经搜索了互联网,但信息是稀缺的。
有没有人能告诉我从哪里开始?

b4qexyjb

b4qexyjb1#

这很容易做到,Windows中的每个窗口都有天生的可调整大小的能力。它只是关闭了PictureBox,你可以通过监听WM_NCHITTEST message来重新打开它。你只需告诉Windows光标在窗口的一角,你还需要画一个抓柄,这样用户就可以清楚地看到拖动角可以调整框的大小。
添加一个新的类到你的项目中,然后粘贴如下所示的代码。Build + Build。你将在工具箱的顶部得到一个新的控件,将它放在窗体上。设置Image属性,你就可以尝试了。

  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. class SizeablePictureBox : PictureBox {
  5. public SizeablePictureBox() {
  6. this.ResizeRedraw = true;
  7. }
  8. protected override void OnPaint(PaintEventArgs e) {
  9. base.OnPaint(e);
  10. var rc = new Rectangle(this.ClientSize.Width - grab, this.ClientSize.Height - grab, grab, grab);
  11. ControlPaint.DrawSizeGrip(e.Graphics, this.BackColor, rc);
  12. }
  13. protected override void WndProc(ref Message m) {
  14. base.WndProc(ref m);
  15. if (m.Msg == 0x84) { // Trap WM_NCHITTEST
  16. var pos = this.PointToClient(new Point(m.LParam.ToInt32()));
  17. if (pos.X >= this.ClientSize.Width - grab && pos.Y >= this.ClientSize.Height - grab)
  18. m.Result = new IntPtr(17); // HT_BOTTOMRIGHT
  19. }
  20. }
  21. private const int grab = 16;
  22. }

字符串
另一个免费获取WndProc的 * 非常 * 便宜的方法是给控件一个可调整大小的边框。它适用于所有的角和边。将以下代码粘贴到类中(你不再需要WndProc了):

  1. protected override CreateParams CreateParams {
  2. get {
  3. var cp = base.CreateParams;
  4. cp.Style |= 0x840000; // Turn on WS_BORDER + WS_THICKFRAME
  5. return cp;
  6. }
  7. }

展开查看全部
h22fl7wq

h22fl7wq2#

this article中使用ControlMoverOrResizer类,您可以在运行时仅用一行代码即可实现可移动和可调整大小的控件!:)示例:
System. out. println();
现在button 1是一个可移动和可调整大小的控件!

q35jwt9p

q35jwt9p3#

这里有一篇文章
http://www.codeproject.com/Articles/20716/Allow-the-User-to-Resize-Controls-at-Runtime
这应该对你有帮助,因为它在vb这里的C#翻译

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Diagnostics;
  6. public class Form1
  7. {
  8. ResizeableControl rc;
  9. private void Form1_Load(System.Object sender, System.EventArgs e)
  10. {
  11. rc = new ResizeableControl(pbDemo);
  12. }
  13. public Form1()
  14. {
  15. Load += Form1_Load;
  16. }
  17. }

字符串
和重新调整功能大小

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Diagnostics;
  6. public class ResizeableControl
  7. {
  8. private Control withEventsField_mControl;
  9. private Control mControl {
  10. get { return withEventsField_mControl; }
  11. set {
  12. if (withEventsField_mControl != null) {
  13. withEventsField_mControl.MouseDown -= mControl_MouseDown;
  14. withEventsField_mControl.MouseUp -= mControl_MouseUp;
  15. withEventsField_mControl.MouseMove -= mControl_MouseMove;
  16. withEventsField_mControl.MouseLeave -= mControl_MouseLeave;
  17. }
  18. withEventsField_mControl = value;
  19. if (withEventsField_mControl != null) {
  20. withEventsField_mControl.MouseDown += mControl_MouseDown;
  21. withEventsField_mControl.MouseUp += mControl_MouseUp;
  22. withEventsField_mControl.MouseMove += mControl_MouseMove;
  23. withEventsField_mControl.MouseLeave += mControl_MouseLeave;
  24. }
  25. }
  26. }
  27. private bool mMouseDown = false;
  28. private EdgeEnum mEdge = EdgeEnum.None;
  29. private int mWidth = 4;
  30. private bool mOutlineDrawn = false;
  31. private enum EdgeEnum
  32. {
  33. None,
  34. Right,
  35. Left,
  36. Top,
  37. Bottom,
  38. TopLeft
  39. }
  40. public ResizeableControl(Control Control)
  41. {
  42. mControl = Control;
  43. }
  44. private void mControl_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
  45. {
  46. if (e.Button == System.Windows.Forms.MouseButtons.Left) {
  47. mMouseDown = true;
  48. }
  49. }
  50. private void mControl_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
  51. {
  52. mMouseDown = false;
  53. }
  54. private void mControl_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
  55. {
  56. Control c = (Control)sender;
  57. Graphics g = c.CreateGraphics;
  58. switch (mEdge) {
  59. case EdgeEnum.TopLeft:
  60. g.FillRectangle(Brushes.Fuchsia, 0, 0, mWidth * 4, mWidth * 4);
  61. mOutlineDrawn = true;
  62. break;
  63. case EdgeEnum.Left:
  64. g.FillRectangle(Brushes.Fuchsia, 0, 0, mWidth, c.Height);
  65. mOutlineDrawn = true;
  66. break;
  67. case EdgeEnum.Right:
  68. g.FillRectangle(Brushes.Fuchsia, c.Width - mWidth, 0, c.Width, c.Height);
  69. mOutlineDrawn = true;
  70. break;
  71. case EdgeEnum.Top:
  72. g.FillRectangle(Brushes.Fuchsia, 0, 0, c.Width, mWidth);
  73. mOutlineDrawn = true;
  74. break;
  75. case EdgeEnum.Bottom:
  76. g.FillRectangle(Brushes.Fuchsia, 0, c.Height - mWidth, c.Width, mWidth);
  77. mOutlineDrawn = true;
  78. break;
  79. case EdgeEnum.None:
  80. if (mOutlineDrawn) {
  81. c.Refresh();
  82. mOutlineDrawn = false;
  83. }
  84. break;
  85. }
  86. if (mMouseDown & mEdge != EdgeEnum.None) {
  87. c.SuspendLayout();
  88. switch (mEdge) {
  89. case EdgeEnum.TopLeft:
  90. c.SetBounds(c.Left + e.X, c.Top + e.Y, c.Width, c.Height);
  91. break;
  92. case EdgeEnum.Left:
  93. c.SetBounds(c.Left + e.X, c.Top, c.Width - e.X, c.Height);
  94. break;
  95. case EdgeEnum.Right:
  96. c.SetBounds(c.Left, c.Top, c.Width - (c.Width - e.X), c.Height);
  97. break;
  98. case EdgeEnum.Top:
  99. c.SetBounds(c.Left, c.Top + e.Y, c.Width, c.Height - e.Y);
  100. break;
  101. case EdgeEnum.Bottom:
  102. c.SetBounds(c.Left, c.Top, c.Width, c.Height - (c.Height - e.Y));
  103. break;
  104. }
  105. c.ResumeLayout();
  106. } else {
  107. switch (true) {
  108. case e.X <= (mWidth * 4) & e.Y <= (mWidth * 4):
  109. //top left corner
  110. c.Cursor = Cursors.SizeAll;
  111. mEdge = EdgeEnum.TopLeft;
  112. break;
  113. case e.X <= mWidth:
  114. //left edge
  115. c.Cursor = Cursors.VSplit;
  116. mEdge = EdgeEnum.Left;
  117. break;
  118. case e.X > c.Width - (mWidth + 1):
  119. //right edge
  120. c.Cursor = Cursors.VSplit;
  121. mEdge = EdgeEnum.Right;
  122. break;
  123. case e.Y <= mWidth:
  124. //top edge
  125. c.Cursor = Cursors.HSplit;
  126. mEdge = EdgeEnum.Top;
  127. break;
  128. case e.Y > c.Height - (mWidth + 1):
  129. //bottom edge
  130. c.Cursor = Cursors.HSplit;
  131. mEdge = EdgeEnum.Bottom;
  132. break;
  133. default:
  134. //no edge
  135. c.Cursor = Cursors.Default;
  136. mEdge = EdgeEnum.None;
  137. break;
  138. }
  139. }
  140. }
  141. private void mControl_MouseLeave(object sender, System.EventArgs e)
  142. {
  143. Control c = (Control)sender;
  144. mEdge = EdgeEnum.None;
  145. c.Refresh();
  146. }
  147. }

展开查看全部
cbeh67ev

cbeh67ev4#

创建一个新的c# winform应用程序并粘贴以下内容:
别忘了说谢谢,当它帮助你...
http://www.codeproject.com/script/Articles/ArticleVersion.aspx?aid=743923&av=1095793&msg=4778687

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. namespace WindowsFormsApplication1
  10. {
  11. public partial class MyForm : Form
  12. {
  13. //Public Declaration:
  14. double rW = 0;
  15. double rH = 0;
  16. int fH = 0;
  17. int fW = 0;
  18. // @ Form Initialization
  19. public MyForm()
  20. {
  21. InitializeComponent();
  22. this.Resize += MyForm_Resize; // handles resize routine
  23. this.tabControl1.Dock = DockStyle.None;
  24. }
  25. private void MyForm_Resize(object sender, EventArgs e)
  26. {
  27. rResize(this,true); //Call the routine
  28. }
  29. private void rResize(Control t, bool hasTabs) // Routine to Auto resize the control
  30. {
  31. // this will return to normal default size when 1 of the conditions is met
  32. string[] s = null;
  33. if (this.Width < fW || this.Height < fH)
  34. {
  35. this.Width = (int)fW;
  36. this.Height = (int)fH;
  37. return;
  38. }
  39. foreach (Control c in t.Controls)
  40. {
  41. // Option 1:
  42. double rRW = (t.Width > rW ? t.Width / (rW) : rW / t.Width);
  43. double rRH = (t.Height > rH ? t.Height / (rH) : rH / t.Height);
  44. // Option 2:
  45. // double rRW = t.Width / rW;
  46. // double rRH = t.Height / rH;
  47. s = c.Tag.ToString().Split('/');
  48. if (c.Name == s[0].ToString())
  49. {
  50. //Use integer casting
  51. c.Width = (int)(Convert.ToInt32(s[3]) * rRW);
  52. c.Height = (int)(Convert.ToInt32(s[4]) * rRH);
  53. c.Left = (int)(Convert.ToInt32(s[1]) * rRW);
  54. c.Top = (int)(Convert.ToInt32(s[2]) * rRH);
  55. }
  56. if (hasTabs)
  57. {
  58. if (c.GetType() == typeof(TabControl))
  59. {
  60. foreach (Control f in c.Controls)
  61. {
  62. foreach (Control j in f.Controls) //tabpage
  63. {
  64. s = j.Tag.ToString().Split('/');
  65. if (j.Name == s[0].ToString())
  66. {
  67. j.Width = (int)(Convert.ToInt32(s[3]) * rRW);
  68. j.Height = (int)(Convert.ToInt32(s[4]) * rRH);
  69. j.Left = (int)(Convert.ToInt32(s[1]) * rRW);
  70. j.Top = (int)(Convert.ToInt32(s[2]) * rRH);
  71. }
  72. }
  73. }
  74. }
  75. }
  76. }
  77. }
  78. // @ Form Load Event
  79. private void MyForm_Load(object sender, EventArgs e)
  80. {
  81. // Put values in the variables
  82. rW = this.Width;
  83. rH = this.Height;
  84. fW = this.Width;
  85. fH = this.Height;
  86. // Loop through the controls inside the form i.e. Tabcontrol Container
  87. foreach (Control c in this.Controls)
  88. {
  89. c.Tag = c.Name + "/" + c.Left + "/" + c.Top + "/" + c.Width + "/" + c.Height;
  90. // c.Anchor = (AnchorStyles.Right | AnchorStyles.Left );
  91. if (c.GetType() == typeof(TabControl))
  92. {
  93. foreach (Control f in c.Controls)
  94. {
  95. foreach (Control j in f.Controls) //tabpage
  96. {
  97. j.Tag = j.Name + "/" + j.Left + "/" + j.Top + "/" + j.Width + "/" + j.Height;
  98. }
  99. }
  100. }
  101. }
  102. }
  103. }
  104. }

字符串
问候,Kix46

展开查看全部
n8ghc7c1

n8ghc7c15#

  1. namespace utils
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.IO;
  9. using System.Data;
  10. using System.Globalization;
  11. using System.Xml;
  12. using System.Text.RegularExpressions;
  13. using System.Drawing;
  14. public static class Helper
  15. {
  16. public const int WM_NCLBUTTONDOWN = 0xA1;
  17. public const int HTCAPTION = 2;
  18. public static int WS_THICKFRAME = 0x00040000;
  19. public static int GWL_STYLE = -16;
  20. public static int GWL_EXSTYLE = -20;
  21. public static int WS_EX_LAYERED = 0x00080000;
  22. public const int WS_RESIZABLE = 0x840000; //WS_BORDER + WS_THICKFRAME
  23. [System.Runtime.InteropServices.DllImport("user32.dll")]
  24. static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
  25. [System.Runtime.InteropServices.DllImport("user32.dll")]
  26. static extern bool AppendMenu(IntPtr hMenu, uint uFlags, uint uIDNewItem, string lpNewItem);
  27. [System.Runtime.InteropServices.DllImport("user32.dll")]
  28. public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
  29. [System.Runtime.InteropServices.DllImport("user32.dll")]
  30. public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
  31. [System.Runtime.InteropServices.DllImport("user32.dll")]
  32. public static extern int
  33. SetLayeredWindowAttributes(IntPtr Handle, int Clr, byte transparency, int clrkey);
  34. [System.Runtime.InteropServices.DllImport("shell32.dll")]
  35. public static extern IntPtr ShellExecute(IntPtr hwnd, string lpOperation, string lpFile, string lpParameters, string lpDirectory, int nShowCmd);
  36. [return: System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)]
  37. [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
  38. public static extern bool PostMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
  39. [System.Runtime.InteropServices.DllImport("kernel32.dll")]
  40. public static extern uint GetCurrentThreadId();
  41. public static string ProcessException(this Exception ex)
  42. {
  43. StringBuilder strBuild = new StringBuilder(5000);
  44. Exception inner = ex;
  45. Enumerable.Range(0, 30).All(x =>
  46. {
  47. if (x == 0) strBuild.Append("########## Exception begin on thread id : " + GetCurrentThreadId().ToString() + " @ :" + DateTime.Now.ToString() + " ##########\n");
  48. strBuild.Append("---------------------[" + x.ToString() + "]---------------------\n");
  49. strBuild.Append("Message : " + inner.Message + "\nStack Trace : " + inner.StackTrace + "\n");
  50. strBuild.Append("---------------------[" + x.ToString() + "]---------------------\n");
  51. inner = inner.InnerException;
  52. if (inner == null)
  53. {
  54. strBuild.Append("########## Exception End on thread id : " + GetCurrentThreadId().ToString() + " @ :" + DateTime.Now.ToString() + " ##########\n\n");
  55. return false;
  56. }
  57. return true;
  58. });
  59. return strBuild.ToString();
  60. }
  61. public static void MakeResizable(this Panel pnl)
  62. {
  63. int dwCurStyle = Helper.GetWindowLong(pnl.Handle, GWL_STYLE);
  64. dwCurStyle = dwCurStyle | Helper.WS_RESIZABLE;
  65. Helper.SetWindowLong(pnl.Handle, GWL_STYLE, dwCurStyle);
  66. }
  67. }
  68. protected override void OnLoad(EventArgs e)
  69. {
  70. imagePanel.MakeResizable();
  71. base.OnLoad(e);
  72. }

字符串
其中imgPanel是WinForm应用程序中的某个面板

展开查看全部
xfyts7mz

xfyts7mz6#

我在我的中奖申请表上写了

  1. public partial class logoPosition : Form
  2. {
  3. bool Draggable = false, ResizableH = false, ResizableW = false;
  4. int XOffset = 0, YOffset = 0;
  5. public logoPosition()
  6. {
  7. InitializeComponent();
  8. }
  9. private void logoPosition_Load(object sender, EventArgs e)
  10. {
  11. Image logo = new Bitmap(@"\Sign_Transparent.png");
  12. PBLogo.Image = logo;
  13. PBLogo.Margin = new Padding(3);
  14. PBLogo.Location = new Point(3, 3);
  15. PBLogo.BorderStyle = BorderStyle.FixedSingle;
  16. PBLogo.SizeMode = PictureBoxSizeMode.StretchImage;
  17. txtW.Text = PBLogo.Width.ToString();
  18. txtH.Text = PBLogo.Height.ToString();
  19. txtX.Text = PBLogo.Location.X.ToString();
  20. txtY.Text = PBLogo.Location.Y.ToString();
  21. }
  22. private void btnReset_Click(object sender, EventArgs e)
  23. {
  24. PBLogo.Margin = new Padding(3);
  25. PBLogo.Location = new Point(3, 3);
  26. PBLogo.BorderStyle = BorderStyle.FixedSingle;
  27. PBLogo.SizeMode = PictureBoxSizeMode.StretchImage;
  28. PBLogo.Height = 50;
  29. PBLogo.Width = 100;
  30. }
  31. private void PBLogo_MouseMove(object sender, MouseEventArgs e)
  32. {
  33. //set location for PictureBox bottom side;
  34. Rectangle recBottomSide = new Rectangle(0 , PBLogo.Height-3, PBLogo.Width,10);
  35. //set location for PictureBox right side;
  36. Rectangle recRightSide = new Rectangle(PBLogo.Width-3, 0, 10, PBLogo.Height);
  37. if (recBottomSide.Contains(e.Location))
  38. {
  39. PBLogo.Cursor = Cursors.SizeNS;
  40. }
  41. else if(recRightSide.Contains(e.Location))
  42. {
  43. PBLogo.Cursor = Cursors.SizeWE;
  44. }
  45. else
  46. {
  47. PBLogo.Cursor = Cursors.SizeAll;
  48. }
  49. int XMoved = e.Location.X - XOffset;
  50. int YMoved = e.Location.Y - YOffset;
  51. Point newPosition = new Point();
  52. if (Draggable)
  53. {
  54. newPosition.X = PBLogo.Location.X + XMoved;
  55. newPosition.Y = PBLogo.Location.Y + YMoved;
  56. if (newPosition.X + PBLogo.Width >= panel1.Width || newPosition.X <= 0)
  57. newPosition.X = PBLogo.Location.X;
  58. if (newPosition.Y + PBLogo.Height > panel1.Height || newPosition.Y <= 0)
  59. newPosition.Y = PBLogo.Location.Y;
  60. PBLogo.Location = newPosition;
  61. }
  62. if(ResizableH)
  63. {
  64. if (PBLogo.Location.Y + e.Y < panel1.Height)
  65. {
  66. PBLogo.Height = e.Y;
  67. if (PBLogo.Height < 30) PBLogo.Height = 30;
  68. }
  69. }
  70. if(ResizableW)
  71. {
  72. if (PBLogo.Location.X + e.X < panel1.Width)
  73. {
  74. PBLogo.Width = e.X;
  75. if (PBLogo.Width < 30) PBLogo.Width = 30;
  76. }
  77. }
  78. txtW.Text = PBLogo.Width.ToString();
  79. txtH.Text = PBLogo.Height.ToString();
  80. txtX.Text = PBLogo.Location.X.ToString();
  81. txtY.Text = PBLogo.Location.Y.ToString();
  82. }
  83. private void PBLogo_MouseDown(object sender, MouseEventArgs e)
  84. {
  85. XOffset = e.X;
  86. YOffset = e.Y;
  87. if (PBLogo.Cursor == Cursors.SizeAll)
  88. {
  89. Draggable = true;
  90. ResizableW = false;
  91. ResizableH = false;
  92. }
  93. if(PBLogo.Cursor == Cursors.SizeNS)
  94. {
  95. Draggable = false;
  96. ResizableW = false;
  97. ResizableH = true;
  98. }
  99. if (PBLogo.Cursor == Cursors.SizeWE)
  100. {
  101. Draggable = false;
  102. ResizableW = true;
  103. ResizableH = false;
  104. }
  105. }
  106. private void PBLogo_MouseUp(object sender, MouseEventArgs e)
  107. {
  108. Draggable = false;
  109. ResizableW = false;
  110. ResizableH = false;
  111. }
  112. }

字符串

展开查看全部

相关问题