适用于多个标签的MousePressed事件

时间:2018-01-11 19:23:17

标签: c#

我正在做一些像素电影编辑器,我的问题是我使用标签,我必须单独点击改变颜色,我想通过按住鼠标按钮改变颜色悬停在标签上方。

是否有任何活动或此类活动?我找不到一个。目前我正在使用Mouse_Down。

A look on the Editor

我的Mouse_Down事件:

private void Mouse_Down(object sender, MouseEventArgs e)
{
    var ActiveLabel = sender as Label;
    if (ActiveLabel != null)
    {
        ActiveLabel.BackColor = ActiveColor.BackColor;
    }
}

所以我希望它像GIMP或其他东西一样,不要点击任何单个“像素”,而是单击并按住鼠标按钮并移动它并为我按下鼠标时移动的所有像素着色。

1 个答案:

答案 0 :(得分:0)

以下是这个想法:

你有一个标志(布尔值),表示用户当前是否正在绘画。如果鼠标悬停在标签上(事件MouseEnter),则检查是否应该绘制。如果要涂漆,则应用您已有的代码。

bool paintmode = false;

// Assign this to all labels, since you don't know
// where the user will start painting
private void Mouse_Down(object sender, MouseEventArgs e)
{
   paintmode = true;
}

// Make sure you assign this to all items, even the Form
// as you'll never know where the user will release the mouse
private void Mouse_Up(object sender, MouseEventArgs e)
{
   paintmode = false;
}

// Assign this only to labels which can get painted
private void Mouse_Enter(object sender, MouseEventArgs e)
{
    if (paintmode)
    {
        var ActiveLabel = sender as Label;
        if (ActiveLabel != null)
        {
            ActiveLabel.BackColor = ActiveColor.BackColor;
        }
    }
}