Show/Hide PictureBox 中的按钮
Show/Hide Buttons inside a PictureBox
当我 show/hide PictureBox 中的某些按钮时出现问题,它们 show/hide 但我无法单击它们,如果我在 MouseLeave 事件中禁用隐藏部分它可以工作但是当我使其隐藏,由于某种我无法理解的原因而无法正常工作。
代码如下:
private void Form1_Load(object sender, EventArgs e)
{
PictureBox picB = new PictureBox();
Button btn1 = new Button();
Button btn2 = new Button();
picB.Size = new Size(130, 70);
btn1.Text = "btn1";
btn2.Text = "btn2";
btn1.Hide();
btn2.Hide();
picB.Controls.Add(btn1);
picB.Controls.Add(btn2);
flp.Controls.Add(picB);
picB.MouseEnter += picB_MouseEnter;
picB.MouseLeave += picB_MouseLeave;
btn1.MouseClick += btn1_MouseClick;
btn2.MouseClick += btn2_MouseClick;
}
private void picB_MouseEnter(object sender, EventArgs e)
{
PictureBox pb = (PictureBox)sender;
Button bt1 = (Button)pb.Controls[0];
Button bt2 = (Button)pb.Controls[1];
bt1.Show();
bt2.Show();
}
private void picB_MouseLeave(object sender, EventArgs e)
{
PictureBox pb = (PictureBox)sender;
Button bt1 = (Button)pb.Controls[0];
Button bt2 = (Button)pb.Controls[1];
bt1.Hide();
bt2.Hide();
}
private void btn1_MouseClick(object sender, EventArgs e)
{
MessageBox.Show("Button 1 pressed.");
}
private void btn2_MouseClick(object sender, EventArgs e)
{
MessageBox.Show("Button 2 pressed.");
}
MouseLeave
事件会在您的鼠标悬停在按钮上时立即触发(按钮将收到 MouseEnter
。因此在您单击它之前它们会隐藏起来。
我不会将这些按钮添加到图片框,而是创建一个同时具有图片框和按钮的面板。
var pnl = new Panel();
PictureBox picB = new PictureBox();
Button btn1 = new Button();
Button btn2 = new Button();
picB.Size = new Size(130, 70);
picB.BorderStyle = BorderStyle.Fixed3D;
btn1.Text = "btn1";
btn2.Text = "btn2";
btn1.Hide();
btn2.Hide();
pnl.Controls.Add(btn1);
pnl.Controls.Add(btn2);
pnl.Controls.Add(picB);
btn1.BringToFront();
btn2.BringToFront();
this.Controls.Add(pnl);
// picturebox and Panlel both handle MouseEnter
picB.MouseEnter += picAndpnl_MouseEnter;
pnl.MouseEnter += picAndpnl_MouseEnter;
pnl.MouseLeave += picB_MouseLeave;
btn1.MouseClick += btn1_MouseClick;
btn2.MouseClick += btn2_MouseClick;
然后在您的活动中:
private void picAndpnl_MouseEnter(object sender, EventArgs e)
{
// check if this is the PictureBox or the Panel
var ctl = (Control)sender;
if (ctl is PictureBox)
{
ctl = ctl.Parent;
}
Button bt1 = (Button)ctl.Controls[0];
Button bt2 = (Button)ctl.Controls[1];
bt1.Show();
bt2.Show();
}
您可以简单地测试鼠标是否 on/off 两者 PictureBox
和 您的 Buttons
:
private void picB_MouseLeave(object sender, EventArgs e)
{
PictureBox pb = (PictureBox)sender;
Button bt1 = (Button)pb.Controls[0];
Button bt2 = (Button)pb.Controls[1];
Point p = Control.MousePosition;
if (bt1.ClientRectangle.Contains(bt1.PointToClient(p)) ||
bt2.ClientRectangle.Contains(bt2.PointToClient(p))) return;
bt1.Hide();
bt2.Hide();
}
当我 show/hide PictureBox 中的某些按钮时出现问题,它们 show/hide 但我无法单击它们,如果我在 MouseLeave 事件中禁用隐藏部分它可以工作但是当我使其隐藏,由于某种我无法理解的原因而无法正常工作。
代码如下:
private void Form1_Load(object sender, EventArgs e)
{
PictureBox picB = new PictureBox();
Button btn1 = new Button();
Button btn2 = new Button();
picB.Size = new Size(130, 70);
btn1.Text = "btn1";
btn2.Text = "btn2";
btn1.Hide();
btn2.Hide();
picB.Controls.Add(btn1);
picB.Controls.Add(btn2);
flp.Controls.Add(picB);
picB.MouseEnter += picB_MouseEnter;
picB.MouseLeave += picB_MouseLeave;
btn1.MouseClick += btn1_MouseClick;
btn2.MouseClick += btn2_MouseClick;
}
private void picB_MouseEnter(object sender, EventArgs e)
{
PictureBox pb = (PictureBox)sender;
Button bt1 = (Button)pb.Controls[0];
Button bt2 = (Button)pb.Controls[1];
bt1.Show();
bt2.Show();
}
private void picB_MouseLeave(object sender, EventArgs e)
{
PictureBox pb = (PictureBox)sender;
Button bt1 = (Button)pb.Controls[0];
Button bt2 = (Button)pb.Controls[1];
bt1.Hide();
bt2.Hide();
}
private void btn1_MouseClick(object sender, EventArgs e)
{
MessageBox.Show("Button 1 pressed.");
}
private void btn2_MouseClick(object sender, EventArgs e)
{
MessageBox.Show("Button 2 pressed.");
}
MouseLeave
事件会在您的鼠标悬停在按钮上时立即触发(按钮将收到 MouseEnter
。因此在您单击它之前它们会隐藏起来。
我不会将这些按钮添加到图片框,而是创建一个同时具有图片框和按钮的面板。
var pnl = new Panel();
PictureBox picB = new PictureBox();
Button btn1 = new Button();
Button btn2 = new Button();
picB.Size = new Size(130, 70);
picB.BorderStyle = BorderStyle.Fixed3D;
btn1.Text = "btn1";
btn2.Text = "btn2";
btn1.Hide();
btn2.Hide();
pnl.Controls.Add(btn1);
pnl.Controls.Add(btn2);
pnl.Controls.Add(picB);
btn1.BringToFront();
btn2.BringToFront();
this.Controls.Add(pnl);
// picturebox and Panlel both handle MouseEnter
picB.MouseEnter += picAndpnl_MouseEnter;
pnl.MouseEnter += picAndpnl_MouseEnter;
pnl.MouseLeave += picB_MouseLeave;
btn1.MouseClick += btn1_MouseClick;
btn2.MouseClick += btn2_MouseClick;
然后在您的活动中:
private void picAndpnl_MouseEnter(object sender, EventArgs e)
{
// check if this is the PictureBox or the Panel
var ctl = (Control)sender;
if (ctl is PictureBox)
{
ctl = ctl.Parent;
}
Button bt1 = (Button)ctl.Controls[0];
Button bt2 = (Button)ctl.Controls[1];
bt1.Show();
bt2.Show();
}
您可以简单地测试鼠标是否 on/off 两者 PictureBox
和 您的 Buttons
:
private void picB_MouseLeave(object sender, EventArgs e)
{
PictureBox pb = (PictureBox)sender;
Button bt1 = (Button)pb.Controls[0];
Button bt2 = (Button)pb.Controls[1];
Point p = Control.MousePosition;
if (bt1.ClientRectangle.Contains(bt1.PointToClient(p)) ||
bt2.ClientRectangle.Contains(bt2.PointToClient(p))) return;
bt1.Hide();
bt2.Hide();
}