Jump to content
  • Advertisement
Sign in to follow this  
gellin

C# mouse capture problem

This topic is 4635 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

First off I'm using C# 2.0 and I'm pretty new to C#. Now on to my problem. I have 20 picturebox controls all in a line. What I would like is to click on the first picturebox, and while holding the mouse button down still, move the pointer across all of the other pictureboxes. The problem with doing this is that while I still have the mouse button held down, no other controls besides the first picturebox I click on receive any events. I particullary want the pictureboxes to all receive mouseEnter events even when the button is held down. Can anyone help me out with this? Thanks

Share this post


Link to post
Share on other sites
Advertisement
You can just PInvoke the ReleaseCapture, Win32 API call. Note: I think that the Form class may have a built-in ReleaseCapture method in .NET 2.0, but in 1.1 I still need to use the API call. You could call ReleaseCapture from within the MouseMove event of the first picturebox (the one that captures the mouse).

Share this post


Link to post
Share on other sites
PictureBox.Capture is what you want.

Here is some code I just wrote that will do what you want:


public partial class Form1 : Form
{
bool m_mouseDownInPicBox;

public Form1()
{
m_mouseDownInPicBox = false;
InitializeComponent();
}

private void onMDown1(object sender, MouseEventArgs e)
{
m_mouseDownInPicBox = true;
}

private void onMUp1(object sender, MouseEventArgs e)
{
m_mouseDownInPicBox = false;
}

private void onMMove1(object sender, MouseEventArgs e)
{
pictureBox1.Capture = false;
}

private void onMEnterAndHover2(object sender, EventArgs e)
{
if (m_mouseDownInPicBox)
{
// Whatever you want here
}
}
}



The 1/2 after a function name corresponds to which picture box the callback is associated with (I have 2 picture boxes in this example).

The basic idea is as follows:

- When the mouse is clicked down within a picture box, set the bool to true. If the mouse button is ever released, set it to false.
- Assign a MouseMove callback to each picture box. Within this function, get the mouse coordinates and the size of your picture box. If the mouse ever moves outside of the picture box, you want to set pictureBox.Capture = false. This is the key, as it will now allow your other picture boxes to received events.
- Now, if the mouse button is still down (the bool is still true) and you move to within a new picture box (assign a callback to MouseEnter), you're all set to do whatever you want to do.

The code doesn't do 'exactly' what is needed, but it illustrates the point.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!