• 12
• 12
• 9
• 10
• 13

# Moving GUI by mouse.

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

## Recommended Posts

Hello GD, So for the past few days, I've been working on a GUI in directX, how ever I seem to have come across a snag that I can't seem to figure out. You see my problem is moving the GUI by mouse. But it's not just moving the GUI that's the issue, it's the fact that when I attempt to move the GUI, the GUI as a whole quickly snaps to where my cursor position is. Meaning if I where to click on the title bar of the GUI some where with in the middle, and then try to move it from that middle position. It would simply as I said before snap to the left of my cursor or in other words the X Axis of the menu would be exactly where my cursor position is. That or it would move uncontrollably. So in part what I'm looking for, is a easy way to keep both the menu and cursor coordinates in the correct area while I move my menu. Similar to when you move your Internet browser window. The browser it self don't instantly move to the left of your mouse, instead it stays where it was originally click on. And doesn't suddenly snap to exactly where the mouse is located. Thanks, -A2H

##### Share on other sites
If I understand you correctly all you need to do is in the mouse down event is figure out how far the cursor is from the upper left corner of your GUI item. Then in your mouse move event add that offset to the mouse position and give that as the new position for your GUI item. The GUI item will always be the same relative position to the mouse whereever you move it to.

##### Share on other sites
This is where I'm having the trouble at. I can't seem to figure out a good clean way of calculating the correct position. Every method I attempt, fails quite badly.

##### Share on other sites
Look up GetCursorPos() and ScreenToClient() then subtract the GUI item position from that mouse position and it will give you the offset. Thats the x and y values to add to the mouse position when moving the mouse. If you are using the upper left of your GUI items that number will be negative. If you think about it the upper left should be to the left and up of the mouse cursor (ie smaller numbers then the actual mouse position).

##### Share on other sites
I suggest you to use delta values:

Calculate the mouse deltas:
dx = MouseX-PrevMouseX;dy = MouseY-PrevMouseY;PrevMouseX = MouseX;PrevMouseY = MouseY;PositionOfGui_x += dx;PositionOfGui_y += dy;
I use that, and I hope you asked that.

##### Share on other sites
Quote:
 Original post by szecsI suggest you to use delta values:Calculate the mouse deltas:dx = MouseX-PrevMouseX;dy = MouseY-PrevMouseY;PrevMouseX = MouseX;PrevMouseY = MouseY;PositionOfGui_x += dx;PositionOfGui_y += dy;I use that, and I hope you asked that.

The problem with that is, that my menu positions are predefined. Meaning if my menu Y Axis is 100. And my cursor's Y axis is at 110; It would simply add that to the predefined value of 100.0f. Simply telling it to equal ether dx or dy ends up jumping upwards.

Quote:
 Original post by jtagge75Look up GetCursorPos() and ScreenToClient() then subtract the GUI item position from that mouse position and it will give you the offset. Thats the x and y values to add to the mouse position when moving the mouse. If you are using the upper left of your GUI items that number will be negative. If you think about it the upper left should be to the left and up of the mouse cursor (ie smaller numbers then the actual mouse position).

As I said before, it adds values to the Y or X axis of the menu that seem to distort it from where I'd like to place it.

Right now, I'm just simply so confused. This seems almost like a open and shut case, yet every time I attempt to move a menu by it's title bar. It just doesn't want to position it self in the correct position.