codymanix
codymanix

Reputation: 29458

How to make a control "transparent" for the mouse or Route MouseMove event to parent?

I want to create a card playing game. I the use mousemove event to drag cards through the window. The problem is if I move the mouse over another card, it is stuck because the card underneath the mouse cursor gets the mouse events, so that the MouseMove event of the window isn't fired.

This is what I do:

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

I tried the following, but there was no difference:

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

Iam looking for a way to implement an application-global event-handler or a way to implement so called event-bubbling. At least I want to make the mouse ignore certain controls.

Upvotes: 2

Views: 3482

Answers (3)

Oyvind Andersson
Oyvind Andersson

Reputation: 23

What you could do, is send the MouseDown event to the event.function you want to call.

Say you got a "Label" ontop of a "Card", but you wan't to "go-through" it:

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

Now the appropriate event-function is called, even though the bothersome label was clicked.

Upvotes: 1

Fredrik Mörk
Fredrik Mörk

Reputation: 158289

In order to do this you will need to keep track of a few things in your code:

  1. On which card the mouse is pointing when the mouse button is pressed; this is the card that you want to move (use the MouseDown event)
  2. Move the the card when the mouse is moved
  3. Stop moving the card when the mouse button is released (use the MouseUp event)

In order to just move around controls, there is no need to actually capture the mouse.

A quick example (using Panel controls as "cards"):

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 + e.X,
            _currentlyMovingCard.Top - _moveOrigin.Y + e.Y);
    }
}

private void Card_MouseUp(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left && _currentlyMovingCard != null)
    {
        _currentlyMovingCard = null;
    }
}

Upvotes: 2

Roger Lipscombe
Roger Lipscombe

Reputation: 91805

Normally you do this by, before capturing the mouse, seeing if anybody else has it...

Upvotes: 0

Related Questions