gpt4 book ai didi

c# - 如果控件已更改,如何中断 mouseUp 事件

转载 作者:太空宇宙 更新时间:2023-11-03 13:13:14 26 4
gpt4 key购买 nike

我刚刚在 Windows 窗体中完成了一个扫雷游戏,这是为了练习。

一切运行良好,但我发现,如果您单击并按住一个按钮,它就会展开,即使鼠标不再指向该按钮也是如此。

您对我如何解决这个问题有简单的想法吗?

代码:

/// <summary>
/// Build up a new Gamefield with the number of buttons, the user has selected
/// </summary>
private void DrawGameField()
{
_buttons = new Button[_xLength, _yLength];
for (int yPos = 0; yPos < _yLength; yPos++)
{
for (int xPos = 0; xPos < _xLength; xPos++)
{
var btn = new Button()
{
Tag = new Point(xPos, yPos),
Location = new Point(xPos * 30, yPos * 30),
Size = new Size(30, 30),
};

_buttons[xPos, yPos] = (Button)btn;
btn.MouseUp += btn_Click;
_gamePanel.Controls.Add(btn);
}
}
}

/// <summary>
/// Is called when a field is left-clicked
/// </summary>
private void LeftMouseClick(object sender, MouseEventArgs e)
{
var btn = sender as Button;
Point pt = (Point)btn.Tag;

// If there's already a flag on the field make it unclickable for left mouseclick
if (btn.Image != null)
return;

_game.UnfoldAutomatically(pt.X, pt.Y);
btn.Text = (_game.ReturnNumberInField(pt.X, pt.Y)).ToString();

// If clicked field was not a bombfield
if (btn.Text != "-1")
UnfoldFields();

else
LooseGame();
}

每个按钮在 DrawGameField - 方法中都有一个 mouseUp 事件。每个按钮还在该方法中获得一个标签,因此可以识别它。一旦用户 (mouseUp) 单击游戏区域按钮之一,LeftMouseClick 就会被调用。

如果释放鼠标左键的按钮与实际单击的按钮不同,我想取消这一点。

这应该给用户改变主意的可能性......他可能实际上点击了一个字段,然后意识到他不想展开那个字段,但在我的解决方案中他无法撤消他的点击......

最佳答案

伙计们,在一点帮助下,我得到了问题的正确解决方案,如下所示:

使用“MouseClick”而不是“MouseUp”或“MouseDown”。

为什么?

MouseClick 在内部使用 MouseDown 然后执行 MouseCapture(通知,哪个控件在鼠标指针下并检查鼠标是否仍指向该控件)如果为真 -> 点击事件如果为假 -> 返回

工作代码:

/// <summary>
/// Build up a new Gamefield with the number of buttons, the user has selected
/// </summary>
private void DrawGameField()
{
// _xLength is the length of the field in x-direction
// _yLength is the length of the field in y-direction
var buttons = new Button[_xLength, _yLength];

// Filling the buttonArray
for (int yPos = 0; yPos < _yLength; yPos++)
{
for (int xPos = 0; xPos < _xLength; xPos++)
{
var btn = new Button()
{
Tag = new Point(xPos, yPos),
Location = new Point(xPos * 30, yPos * 30),
Size = new Size(30, 30)
};
// Apply a clickEvent to every button
btn.MouseClick += Button_MouseClick; //first change here: Use MouseClick!!!
buttons[xPos, yPos] = btn;
}
}
AddGameButtonsToPanel(buttons);
}

/// <summary>
/// Adds Buttons to the gamepanel
/// </summary>
private void AddGameButtonsToPanel(Button[,] buttons)
{
_buttons = buttons;
_gamePanel.SuspendLayout();
try
{
foreach (var btn in buttons)
_gamePanel.Controls.Add(btn);
}
finally
{
_gamePanel.ResumeLayout();
}
}

/// <summary>
/// Checks which mouse-button was clicked and calls the correct method for that button
/// </summary>
private void Button_MouseClick(object sender, MouseEventArgs e)
{
// If it is the firs time a button is clicked, the stopwatch is started
if (_firstClick)
StartStopWatch();

var btn = sender as Button;
Point pt = (Point)btn.Tag;

if (e.Button == MouseButtons.Left)
Button_LeftClick(btn, pt);
else
Button_RightClick(btn, pt);
}

关于c# - 如果控件已更改,如何中断 mouseUp 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27563064/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com