在PictureBox中显示/隐藏按钮

时间:2016-08-25 07:57:30

标签: c# show-hide picturebox mouseenter mouseleave

当我在PictureBox中显示/隐藏一些按钮时,我有一个问题,它们显示/隐藏但是我无法点击它们,如果我在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.");
    }

2 个答案:

答案 0 :(得分:1)

只要您的鼠标悬停在按钮上,MouseLeave事件就会触发(按钮会收到MouseEnter。因此,在您点击它之前,它们会被隐藏。

我不会将这些按钮添加到图片框中,而是创建一个同时具有PictureBox和按钮的面板。

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

答案 1 :(得分:1)

您只需测试鼠标是否开启 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();
}