如何使鼠标或Route MouseMove事件的控件“透明”到父级?

我想创建一个纸牌游戏。 我使用mousemove事件通过窗口拖动卡片。 问题是如果我将鼠标移动到另一张卡上,它会卡住,因为鼠标光标下面的卡会获得鼠标事件,因此不会触发窗口的MouseMove事件。

这就是我做的:

private void RommeeGUI_MouseMove(object sender, MouseEventArgs e) { if (handkarte != null) { handkarte.Location = this.PointToClient(Cursor.Position); } } 

我试过以下,但没有区别:

 SetStyle(ControlStyles.UserMouse,true); SetStyle(ControlStyles.EnableNotifyMessage, true); 

我正在寻找一种方法来实现一个应用程序全局事件处理程序或一种实现所谓的事件冒泡的方法。 至少我想让鼠标忽略某些控件。

为此,您需要跟踪代码中的一些内容:

  1. 按下鼠标按钮时鼠标指向哪个卡; 这是你要移动的卡(使用MouseDown事件)
  2. 移动鼠标时移动卡
  3. 释放鼠标按钮时停止移动卡(使用MouseUp事件)

为了只是移动控件,没有必要实际捕获鼠标。

一个简单的例子(使用Panel控件作为“卡片”):

 Panel _currentlyMovingCard = null; Point _moveOrigin = Point.Empty; private void Card_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { _currentlyMovingCard = (Panel)sender; _moveOrigin = e.Location; } } private void Card_MouseMove(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left && _currentlyMovingCard != null) { // move the _currentlyMovingCard control _currentlyMovingCard.Location = new Point( _currentlyMovingCard.Left - _moveOrigin.X + eX, _currentlyMovingCard.Top - _moveOrigin.Y + eY); } } private void Card_MouseUp(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left && _currentlyMovingCard != null) { _currentlyMovingCard = null; } } 

你可以做的是将MouseDown事件发送到你想要调用的event.function。

假设你在“卡片”上有一个“标签”,但你不想“通过”它:

 private void Label_MouseDown( object sender, MouseEventArgs) { // Send this event down the line! Card_MouseDown(sender, e); // Call the card's MouseDown event function } 

现在调用适当的事件函数,即使单击了令人烦恼的标签。

通常你在捕获鼠标之前执行此操作,看看是否有其他人拥有它…