使用鼠标在C#中在运行时调整按钮大小

我正在使用以下代码在运行时通过鼠标制作和移动按钮。

我想用鼠标调整它们的大小。 此代码由KekuSemau提供。 非常感谢KekuSemau; 它帮助了我。

private Point Origin_Cursor; private Point Origin_Control; private bool BtnDragging = false; private void button1_Click(object sender, EventArgs e) { var b = new Button(); b.Text = "My Button"; b.Name = "button"; //b.Click += new EventHandler(b_Click); b.MouseUp += (s, e2) => { this.BtnDragging = false; }; b.MouseDown += new MouseEventHandler(this.b_MouseDown); b.MouseMove += new MouseEventHandler(this.b_MouseMove); this.panel1.Controls.Add(b); } private void b_MouseDown(object sender, MouseEventArgs e) { Button ct = sender as Button; ct.Capture = true; this.Origin_Cursor = System.Windows.Forms.Cursor.Position; this.Origin_Control = ct.Location; this.BtnDragging = true; } private void b_MouseMove(object sender, MouseEventArgs e) { if(this.BtnDragging) { Button ct = sender as Button; ct.Left = this.Origin_Control.X - (this.Origin_Cursor.X - Cursor.Position.X); ct.Top = this.Origin_Control.Y - (this.Origin_Cursor.Y - Cursor.Position.Y); } } 

我在移动和resize选项之间有变化的问题。 我希望当鼠标指针位于按钮的边缘时,它应该resize,当它位于按钮的中心时,它应该用鼠标指针移动按钮。

winforms中的控件(如按钮)通常具有大小(宽度,高度)和位置(x,y),其中单位是像素。

修改这些属性相对简单:这显示了一个示例,单击按钮将使其宽10 px和高10 px,并将其向右移10 px向下移10 px。

  private void button1_Click(object sender, EventArgs e) { Button button = (Button)sender; button.Width = button.Width + 10; button.Height = button.Height + 10; button.Location = new Point(button.Location.X + 10, button.Location.Y + 10); }