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

使用鼠标在C#运行时调整按钮大小,c#,winforms,user-controls,windows-runtime,C#,Winforms,User Controls,Windows Runtime,我使用以下代码在运行时通过鼠标制作和移动按钮 我还想用鼠标调整它们的大小。此代码由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 Butto

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

我还想用鼠标调整它们的大小。此代码由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);
    }
}
我无法在“移动”和“调整大小”选项之间切换。我希望当鼠标指针位于按钮边缘时,它应该调整大小,当它位于按钮中心时,它应该用鼠标指针移动按钮。

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);

        }

谢谢你的回复,它可以工作,但我忘了提到我希望它与鼠标一起使用,我想用鼠标调整它们的大小。你能帮我吗,我已经在我的帖子中编辑了它谢谢你的编辑。一个按钮控件已经有了定义良好的鼠标用法,它会生成点击事件。当然,没有内置的对调整大小的支持,这是在设计器中完成的。它使用覆盖来拦截鼠标。很难猜测为什么要在运行时执行此操作,它不应该是一个按钮。如果你想创建你自己的设计师,那么你应该。