如何在运行时移动新的(自定义)按钮
本文关键字:自定义 按钮 运行时 移动 | 更新日期: 2023-09-27 18:31:55
我的 winforms C# 项目有问题。
我想在窗体周围移动一个新的(自定义)按钮(在运行时)。我该怎么做?
Button[] buttons = new Button[1000];
int counter = 0;
Button myText = new Button();
private void button2_Click(object sender, EventArgs e)
{
Button myText = new Button();
myText.Tag = counter;
myText.Location = new Point(x2,y2);
myText.Text = Convert.ToString(textBox3.Text);
this.Controls.Add(myText);
myText.MouseMove += new MouseEventHandler(myText_MouseMove);
myText.MouseDown += new MouseEventHandler(myText_MouseDown);
buttons[counter] = myText;
counter++;
}
public void myText_MouseMove(object sender, MouseEventArgs e)
{
int s = e.GetHashCode();
int check = 0;
for (int i = 0; i < counter; i++)
{
if (buttons[i].GetHashCode() == s)
check = i;
}
if (e.Button == MouseButtons.Left)
{
buttons[check].Left += e.X - move.X;
buttons[check].Top += e.Y - move.Y;
}
}
void myText_MouseDown(object sender, MouseEventArgs e)
{
move = e.Location;
}
我使用上面的代码创建新按钮,并尝试在表单周围移动它。
如果我只为 1 个按钮编码,我可以移动它,但是,我也希望能够为更多按钮执行此操作。
试试这个
public void myText_MouseMove(object sender, MouseEventArgs e)
{
Button button = (Button)sender;
if (e.Button == MouseButtons.Left)
{
button .Left += e.X - move.X;
button .Top += e.Y - move.Y;
}
}
我在做类似的事情时遇到了这个线程。
即使线程很旧;我正在分享我实现这一目标的方式。
笔记:与上面的操作员问题不同;在示例波纹管中;我没有使用带有 1000 个按钮的按钮[](数组);(但这可以很容易地改变)。
在下面的例子中,我有:带有 1 个按钮的表单。单击此按钮时;它将创建一个新的(自定义)按钮(因此,代码中有两个按钮单击事件只是为了测试目的)。
您可以在下面找到:一种生成按钮的方法;以及如何在窗体周围拖动它。
/* Variables */
// Used to Store the Mouse Location on the Screen when Button is Clicked
private Point mouseDownLocation;
// Used to know if the Button can be Moved.
private bool isMoveable = false;
// Used to prevent clicking the button when being dragged
private bool clickEnabled = true;
/// <summary> Occurrs when the visible button on the Form is clicked. </summary>
private void Button_Click(object sender, EventArgs e)
{
// Create a variable to set the Size of the new Button.
Size size = new Size(100, 100);
// Create the Button. Specifying the Button Text and its Size.
CreateButton("Button Text", size);
}
/* Custom Button : Create & Configure the Button */
private void CreateButton(string btnText, Size btnSize)
{
// Create the Button instance:
Button btn = new Button();
// Custom Button Style Configuration Code.
// i.e: FlatStyle; FlatAppearance; BackColor; ForeColor; Text; Size; Location; etc...
btn.FlatStyle = FlatStyle.Standard;
btn.FlatAppearance.BorderColor = Color.White;
btn.BackColor = Color.Purple;
btn.ForeColor = Color.White;
btn.Text = btnText;
btn.Size = btnSize;
btn.Location = new Point(100, 100);
// Custom Button Event.
btn.Click += new EventHandler(CustomButton_Click);
btn.MouseDown += new MouseEventHandler(CustomButton_MouseDown);
btn.MouseMove += new MouseEventHandler(CustomButton_MouseMove);
btn.MouseUp += new MouseEventHandler(CustomButton_MouseUp);
// Add Button to Control Collection.
Controls.Add(btn);
// Show Button.
btn.Show();
}
/* Custom Button Events */
/// <summary> Occurrs whenever the Custom Button is Clicked </summary>
private void CustomButton_Click(object sender, EventArgs e)
{
if (clickEnabled)
{
// Note: The "for" loop bellow lets you get the current button; so you can drag it.
Button btn = (Button)sender;
// Iterate over the Form Controls
for (int i = 0; i < Controls.Count; i++)
{
// If the Button Clicked Corresponds to a Found Index in Control Collection...
if (i == Controls.IndexOf(btn))
{
// Perform Corresponding Button Action.
MessageBox.Show(btn.Name + " Clicked!");
}
}
}
}
/// <summary> Occurrs whenever Left Mouse Button Clicks the Button and is Kept Down. </summary>
private void CustomButton_MouseDown(object sender, MouseEventArgs e)
{
// Check if Left Mouse Button is Clicked.
if (e.Button == MouseButtons.Left)
{
// Set mouseDownLocation (Point) variable, according to the current mouse location (X and Y).
mouseDownLocation = e.Location;
// Enable the hability to move the Button.
isMoveable = true;
}
}
/// <summary> Occurrs whenever Left Mouse Button is Down and Mouse is Moving the Button. </summary>
private void CustomButton_MouseMove(object sender, MouseEventArgs e)
{
Button btn = (Button)sender;
// If the hability to move the button is "true" and the mouse button used to drag the button is the left mouse button:
if (isMoveable)
{
if (e.Button == MouseButtons.Left)
{
// Set the Button location X and Y...
btn.Left = e.X + btn.Left - mouseDownLocation.X;
btn.Top = e.Y + btn.Top - mouseDownLocation.Y;
}
}
// Disable Click hability (See: Note at the end of this post).
clickEnabled = false;
}
/// <summary> Occurrs whenever Left Mouse Button is not longer being hold down (Left Mouse Button is Up). </summary>
private void CustomButton_MouseUp(object sender, MouseEventArgs e)
{
// Disables the hability to move the button
isMoveable = false;
clickEnabled = true;
}
Note: The Custom Button Click interferes with the MouseDown Event. In a "simple" manner:
I updated the code by including a boolean value to prevent clicking the button when actually it is being dragged.
Hope this helps you out understanding how to accomplish this feature.
希望这有助于人们实现这一目标。