Mouse Events
Mouse Events
Mouse Events
Mouse Events
Mouse Events
Mouse Events Mouse Events Mouse Events Mouse Events Mouse Events Mouse Events Mouse Events Mouse Events
Mouse Events Mouse Events
Mouse Events
Go Back  Xtreme Visual Basic Talk > > > Mouse Events


Reply
 
Thread Tools Display Modes
  #1  
Old 04-02-2004, 05:33 PM
Sippy Cup's Avatar
Sippy Cup Sippy Cup is offline
Freshman
 
Join Date: Mar 2004
Location: TN, USA
Posts: 39
Default Mouse Events


I'm planning to attempt a card game. My first step was to be able to actually move the card. I succeeded, but I had to use form-level mouse events, meaning I had to actually click on the form itself, not the card, in order to get it to move. Here's the functioning code.

Private Sub frmCardMoveTest_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
m_MouseisDown = True
End Sub

Private Sub frmCardMoveTest_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
m_MouseisDown = False
End Sub

Private Sub frmCardMoveTest_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
If m_MouseisDown = True Then
m_objPoint.X = e.X - 100
m_objPoint.Y = e.Y - 148
pbxCard.Location = m_objPoint
End If
End Sub

I tried changing all those events from frmCardMoveTest to pbxCard (and of course changing "MyBase" to "pbxCard") but when I do, I get a very strange effect. I click on the card and drag, and it produces two card images which flicker very quickly. I can't explain this. Can anyone tell me the correct way to code the clicking and dragging of a control, like a picture box? The effect I want is as follows: if a specific picture box is clicked, I want to enable the box to be moved with the mouse. If it isn't clicked, I don't want a mousemove event to fire.
Reply With Quote
  #2  
Old 04-02-2004, 07:46 PM
rpgnewbie's Avatar
rpgnewbie rpgnewbie is offline
Contributor
 
Join Date: Aug 2003
Location: Portland OR
Posts: 581
Default Simple card movement demo

Quote:
Originally Posted by Sippy Cup
I'm planning to attempt a card game. My first step was to be able to actually move the card. I succeeded, but I had to use form-level mouse events, meaning I had to actually click on the form itself, not the card, in order to get it to move. Can anyone tell me the correct way to code the clicking and dragging of a control, like a picture box? The effect I want is as follows: if a specific picture box is clicked, I want to enable the box to be moved with the mouse. If it isn't clicked, I don't want a mousemove event to fire.
It sounds like you need some working code...like a simple car movement demo. Please check out "DealCardsDemo.zip", my post #10, this thread:
http://www.xtremevbtalk.com/t112514.html

Note: this doesn't uses the cards32.dll. Instead the card images are in picboxes, so can be customized!
Reply With Quote
  #3  
Old 04-02-2004, 08:40 PM
passel's Avatar
passelMouse Events passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,024
Default

The Code works on the form because the relative X and Y values are consistent (the form is not moving). When you click on a picturebox, and then move it, your next X, and Y values are relative to the new position of the window. So if you moved the window to the left, the mouse "moved" right relative to the left edge of the window.
So you can end up with a window jumping back and forth as you drag on it since the mouse X,Y values are changing as a combination of the mouse and the window movement.

One way to handle this is to just intial two variables with the X and Y value of the mouse in the mouse down (say Xi, Yi). Then in the Mouse move, just move the object using the delta between the intial position and the current position of the mouse.
This will "move" the relative X,Y back under mouse (so no need to adjust the Xi and Yi values). So the object will "follow" the mouse. Since you're using the API I'll just leave you with the description, rather than try and provide an example directly applicable. What rpgnewbie provided could very well have an example of what I'm describing.
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
  #4  
Old 04-03-2004, 01:16 AM
Sippy Cup's Avatar
Sippy Cup Sippy Cup is offline
Freshman
 
Join Date: Mar 2004
Location: TN, USA
Posts: 39
Default

I really need a clarification. I can't check rpgnewbie's code because I'm using VB .NET. Anyway.. are you saying I need to initialize the variables INSIDE the MouseDown event? And does one need to be for X, and another for Y? Then, I need to move the object using the delta between those variables and the mouse's position in the MouseMove event? I think I understand what you're saying (though I still don't really understand what's causing that flickering), but I don't know exactly how to do this. It makes sense that I should subtract the original mouse position from the current mouse position and add that value to the original location of the picture box (which I'd also need to store in a variable, I guess), and then put the final result into the location of the picture box. That way, the mouse is moving around a specific point on the form, and I can base everything I say around that point. I just don't know the specifics. I modified the code to be as close as possible to waht I thought you meant, and got the same result as before:

Code:
    Private Sub pbxCard_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pbxCard.MouseDown
        m_MouseisDown = True

        m_Mouse.X = e.X
        m_Mouse.Y = e.Y
        m_Pic.X = pbxCard.Location.X
        m_Pic.Y = pbxCard.Location.Y
    End Sub

    Private Sub pbxCard_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pbxCard.MouseUp
        m_MouseisDown = False
    End Sub

    Private Sub pbxCard_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pbxCard.MouseMove
        If m_MouseisDown = True Then
            m_objPoint.X = (e.X - m_Mouse.X) + m_Pic.X
            m_objPoint.Y = (e.Y - m_Mouse.Y) + m_Pic.Y
            pbxCard.Location = m_objPoint
        End If
    End Sub

Last edited by Sippy Cup; 04-03-2004 at 01:23 AM.
Reply With Quote
  #5  
Old 04-03-2004, 03:58 AM
Mathijsken's Avatar
Mathijsken Mathijsken is offline
Contributor
 
Join Date: May 2003
Location: Oostkamp - Belgium
Posts: 730
Default

You should put this in the MouseDown event:
Code:
    Private Sub pbxCard_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pbxCard.MouseDown
        If m_MouseisDown = False Then
           m_MouseisDown = True

           m_Mouse.X = e.X
           m_Mouse.Y = e.Y
           m_Pic.X = pbxCard.Location.X
           m_Pic.Y = pbxCard.Location.Y
        End If
    End Sub
I'm not sure this will help, but you could give it a try

Mathijsken
__________________
Fear is the first step towards intolerance.
Reply With Quote
  #6  
Old 04-03-2004, 09:50 AM
Sippy Cup's Avatar
Sippy Cup Sippy Cup is offline
Freshman
 
Join Date: Mar 2004
Location: TN, USA
Posts: 39
Default

Same result. The only thing that did is enable the MouseisDown variable using an if statement, so the result was no different. I think I'm going to try putting e.X and e.Y in a label, and putting the X, Y coordinates of the picture box in another label to see what they are. Hopefully that will provide some useful information.
Reply With Quote
  #7  
Old 04-03-2004, 10:08 AM
Iceplug's Avatar
IceplugMouse Events Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

Did you see my answer in this thread?
Card games
Also, try to post in the VB.NET forums if you are using VB.NET .
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #8  
Old 04-03-2004, 08:50 PM
Sippy Cup's Avatar
Sippy Cup Sippy Cup is offline
Freshman
 
Join Date: Mar 2004
Location: TN, USA
Posts: 39
Default

Thanks. It's been so long, I forgot I had posted anything else. It was supposed to E-mail me if I got a reply. Oh well! Thanks, I'm gonna try this out.
Reply With Quote
  #9  
Old 04-04-2004, 10:40 AM
Sippy Cup's Avatar
Sippy Cup Sippy Cup is offline
Freshman
 
Join Date: Mar 2004
Location: TN, USA
Posts: 39
Default

It was my understanding that e.X and e.Y have an origin at the top left of the form, as does the Location property of the picture box. Not so, as I just discovered. When I use a MouseMove event from pbxCard (can anyone help me word that better? Handles pbxCard.MouseMove?), the mouse's location has an origin at the top left of the picturebox, not of the form, so that if I click and move on the top left of the card, the form flickers an image of the card in the top left of the form. So, the question is... how do I get the coordinates of the mouse relative to the form? I need to store the picturebox's location into a variable, add the mouse's location (relative to the top left of the picturebox) to that variable, and then set that value minus the mouse's location relative to the card as the picturebox's new location. So... That works. No flicker. The only problem now is that the card's top left corner snaps to the pointer. I want to be able to grab the cards and move them around from the very POINT where I clicked them. Exactly like in Solitaire. The problem here is that if I take pbx.Location + e.X - e.X, obviously the mouse's movement is eliminated, and the picturebox remains stationary. Here's the functioning code:


Code:
    Private Sub pbxCard_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pbxCard.MouseMove
        If m_MouseisDown = True Then
            m_objPoint.X = pbxCard.Location.X + e.X
            m_objPoint.Y = pbxCard.Location.Y + e.Y
            pbxCard.Location = m_objPoint
            lblCard.Text = pbxCard.Location.X & "," & pbxCard.Location.Y
            lblMouse.Text = e.X & "," & e.Y
        End If
    End Sub
I set the card's location to the current location PLUS the mouse's location relative to the card. Well, I want the card's location to move with the mouse's position relative to the form. For instance, if I click the card in the center and move the mouse to the right one pixel, I want the card's current X value to have one pixel added to it. So, in other words, I need to be able to grab the mouse's position relative to the form and use that in the code, to say something like this...

Code:
    Private Sub pbxCard_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pbxCard.MouseMove
        If m_MouseisDown = True Then
            m_objPoint.X = mouseForm.X - e.X
            m_objPoint.Y = mouseForm.Y -  e.Y
            pbxCard.Location = m_objPoint
            lblCard.Text = pbxCard.Location.X & "," & pbxCard.Location.Y
            lblMouse.Text = e.X & "," & e.Y
        End If
    End Sub
Where mouseForm is the mouse's location relative to the form. Any ideas? Could I somehow use a MyBase.MouseMove instead of a pbxCard.MouseMove? I tried that, but the card remains stationary yet again.

Last edited by Sippy Cup; 04-04-2004 at 01:23 PM.
Reply With Quote
  #10  
Old 04-04-2004, 01:53 PM
Iceplug's Avatar
IceplugMouse Events Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

Controls come with two methods:
PointToClient and PointToScreen...
try using PointToClient of your picturebox to get a point on your Form.
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #11  
Old 04-04-2004, 11:06 PM
Sippy Cup's Avatar
Sippy Cup Sippy Cup is offline
Freshman
 
Join Date: Mar 2004
Location: TN, USA
Posts: 39
Default

Ummm...

Code:
    Private Sub pbxCard_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pbxCard.MouseMove
        If m_MouseisDown = True Then
            Dim objPoint2 As System.Drawing.Point
            m_objPoint.X = e.X
            m_objPoint.Y = e.Y
            m_objPoint1 = pbxCard.PointToClient(m_objPoint)
            objPoint2.X = m_objPoint1.X - m_objPoint.X
            objPoint2.Y = m_objPoint1.Y - m_objPoint.Y
            pbxCard.Location = objPoint2
            lblCard.Text = pbxCard.Location.X & "," & pbxCard.Location.Y
            lblMouse.Text = m_objPoint1.X & "," & m_objPoint1.Y
        End If
    End Sub
My card disappeared. PointToClient obviously did something very, very interesting to my mouse's X, Y coordinates. I clicked on the card and moved it a little, and my picturebox's location ended up at 160, 8, while m_objPoint1 ended up at 618, 231. m_objPoint, which contained my mouse's X and Y, should've been really close to 1, 1, so somehow that method converted it to 618, 231. Could you explain to me exactly what PointToClient does?
Reply With Quote
  #12  
Old 04-05-2004, 02:28 PM
Iceplug's Avatar
IceplugMouse Events Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

Oh... never mind... that converts a screen point back to client coordinates.
What's wrong with this?
m_objPoint.X = pbxCard.Location.X + e.X
m_objPoint.Y = pbxCard.Location.Y + e.Y

Then in MouseMove:
dx = e.X - objpoint.X
dy = e.Y - objpoint.Y
where dx and dy are the amounts to add to the picturebox coordinates.
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!

Last edited by Iceplug; 04-05-2004 at 02:38 PM.
Reply With Quote
  #13  
Old 04-06-2004, 11:16 AM
Sippy Cup's Avatar
Sippy Cup Sippy Cup is offline
Freshman
 
Join Date: Mar 2004
Location: TN, USA
Posts: 39
Default

Quote:
Originally Posted by Iceplug
Oh... never mind... that converts a screen point back to client coordinates.
What's wrong with this?
m_objPoint.X = pbxCard.Location.X + e.X
m_objPoint.Y = pbxCard.Location.Y + e.Y

Then in MouseMove:
dx = e.X - objpoint.X
dy = e.Y - objpoint.Y
where dx and dy are the amounts to add to the picturebox coordinates.
e - m_objPoint will be zero, I think. No, it'll be negative.. Okay, say pbxCard's location is (100, 100), and e is (10, 10). I add those together in m_objPoint, and I have (110, 110). Then I say e.X - m_objPoint.X, which is 10 - 110 = -100. Same thing for dy. So if I add that to the picturebox coordinates... which are (100, 100), won't that just be zero? I think I need another way to go about doing this.. I want to move the picture box's location up and to the left.. that means I have to subtract, and I want to subtract the exact value of e.X, e.Y... but is there another way to do it? Alright, I got a step further by simply adding parentheses.

Code:
            m_objPoint.X = (pbxCard.Location.X + e.X) + e.X
            m_objPoint.Y = (pbxCard.Location.Y + e.Y) + e.Y
That allows me to grab it by whatever point I want.. but it introduces another flicker, which appears to take the pbx's location and flicker another image of it down and to the right at twice the vector of (e.X, e.Y). It's very interesting. For instance, if I click right in the middle of the card, another image is flickered with its top left corner exactly on the bottom right corner of the card I grabbed. If I grab the card in the middle, but on the left side, the flicker is directly beneath the original card. Hm.. how to resolve.
Reply With Quote
  #14  
Old 04-06-2004, 01:12 PM
Iceplug's Avatar
IceplugMouse Events Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

So, we're doing the MouseMove on the picture box... in that case:
oldX = e.X
oldY = e.X
in MouseDown
In the MouseMove:
pbx.Left = pbx.Left + (e.X - oldX)
pbx.Top = pbx.Top + (e.Y - oldY)
Try that.
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #15  
Old 04-08-2004, 11:10 PM
Sippy Cup's Avatar
Sippy Cup Sippy Cup is offline
Freshman
 
Join Date: Mar 2004
Location: TN, USA
Posts: 39
Default

MouseDown is for the card too, so those coordinates are relative to the card as well, meaning MouseDown's e.X is the same as MouseMove's e.X. This is going to be harder than I thought. I did learn about .Left and .Top, though. I didn't know about those.
Reply With Quote
  #16  
Old 04-09-2004, 06:27 AM
Iceplug's Avatar
IceplugMouse Events Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

But when you move the mouse around, you'll get different values for e.X?
If you click at (40, 40) on your card and then move to (42, 42),
pbx.Left and pbx.Top each increase by 2.
Also, add this:
In the MouseMove:
pbx.Left = pbx.Left + (e.X - oldX)
pbx.Top = pbx.Top + (e.Y - oldY)
oldX = e.X
oldY = e.Y
Try that.
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #17  
Old 04-09-2004, 01:10 PM
Sippy Cup's Avatar
Sippy Cup Sippy Cup is offline
Freshman
 
Join Date: Mar 2004
Location: TN, USA
Posts: 39
Default

Well holy crap, that DID work. Thanks a lot. Now, see if you can answer this question for me. How do I start a form using another form's "Load" event? Here's the scenario... card game starts, and I want a tiny dialogue box to pop up and say "Enter your name and amount of money," then I want to pass those values back to the original form. Name should go in a listbox, and I think what I want to do (and I don't know if I CAN do this) is have each player's amount of money displayed in a label when their name is selected in the listbox (no buttons involved). I might decide to do that with a ComboBox for the purpose of simplicity. I think it would be fairly easy to associate names with money amounts with enumeration, but I don't know if I can use the selection of a listbox item to change something.
Reply With Quote
  #18  
Old 04-09-2004, 01:48 PM
Iceplug's Avatar
IceplugMouse Events Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

I just did an example of this recently but didn't upload it:
What you would do is (well, I guess you just want to show the dialog box):
add a ReadOnly Property to your form class... in this property, return the value that you want to return.
Additionally, you could declare a function elsewhere that will show the form:
Dim F As Form = New yourform
F.ShowDialog()
and will return the value that was in the readonly property .
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #19  
Old 04-12-2004, 11:01 AM
Sippy Cup's Avatar
Sippy Cup Sippy Cup is offline
Freshman
 
Join Date: Mar 2004
Location: TN, USA
Posts: 39
Default

Do you have any links to good DirectX tutorials? Specifically DirectPlay. That's what I plan to use to make this card game networkable, but it's kinda difficult to know where to start considering I've never done this. What I really need is a book that explains how to use at least DirectX 8.0 with VB .NET. Know of any?

Last edited by Sippy Cup; 04-12-2004 at 11:58 AM.
Reply With Quote
  #20  
Old 04-12-2004, 02:23 PM
Iceplug's Avatar
IceplugMouse Events Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

Well, here is a website for using DirectX 8.0 with VB6. http://www.directx4vb.com/.
I wouldn't think that there'd be many tutorials for DirectX8 for .NET, but DirectX9.
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
simulating mouse click events yss API 3 11-27-2003 07:17 AM
Sendmessage (Mouse events) coordinate problems Hena API 3 11-19-2003 05:42 AM
Having trouble understanding SendInput for mouse events. MrMcGoo API 4 02-12-2003 11:47 PM
Ignore Certain Mouse Events in Usercontrol Infector General 2 01-16-2003 05:33 AM
mouse down / mouse up xmen64 Interface and Graphics 5 10-09-2002 10:36 AM

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Mouse Events
Mouse Events
Mouse Events Mouse Events
Mouse Events
Mouse Events
Mouse Events Mouse Events Mouse Events Mouse Events Mouse Events Mouse Events Mouse Events
Mouse Events
Mouse Events
 
Mouse Events
Mouse Events
 
-->