How to handle mouse motion events in GTK3? - c

I am trying to implement the following feature using C/GTK3/Cairo:
-Left click on an GtkDrawingArea Widget and printf the coordinates Xo and Yo.
-While keeping the left button down, move the mouse and draw a line conecting (Xo,Yo) to the current mouse position.
-Release the left mouse button and printf("something")
How do I do this? Anyone knows of a good tutorial showing how to handle mouse clicl-move events?
So far, the best I found was this zetcode lines (which shows how to handle mouse click events but not button-down/move/button-up and this , which explains how to change the mouse cursor when hovering over a Widget.
Thanks

Did you see this GtkDrawingArea demo from the Gtk people? This one is written in C, but there is a Python version of the same program (links updated - thanks #kyuuhachi).
Anyway, in the constructor (__init__), calls are connected to the motion_notify_event.
You also need to connect to the button_press_event and the button_release_event.
Then, on button press, you save the coordinates of the start point. (and save it to the end point too, which are the same for now).
On each motion_notify_event, you delete the previous line (by overwriting), and redraw it to the new end point.
Finally, when the button is released, the line is final.
It's much easier if you use a canvas widget, for example GooCanvas, which takes care of most of the updating. You can just update the coordinates of the line object, and it will move itself. Also you can easily remove lines. The 'algorithm' is similar as above:
Connect button_press_event, button_release_event, and motion_notifyevent to the canvas,
When a button press occurs, create a GooCanvas.polyline object, and set begin and endpoint,
Update the endpoint on each motion_notify_event
Finalize with a button_release_event.

Related

Draw a vertical line on Left Click MouseDownEvent

I'm using the Oxyplot library to plot some graphs based on real-time data. I know there is a feature called Tracker that shows some tooltip-like information (coordinates, for example) based on the Position where the user left-clicks. The information is only shown while the left-button is pressed.
I'm currently testing this functionality on a line graph, not sure if it works for other types.
What I wanted to do is to keep this info on screen and making it disapper only if the user tries to set a new one, clicking somewhere else on the graph and replacing the older information. Adding a vertical bar where the user clicked would be nice too.
I checked the Oxyplot documentation and I know that it is possible to customize this 'Tracker', but I failed to find a solution for my situation.
EDIT:
I've found this example, it seems helpful, but I'm struggling to extract the usefulness of it. Please help!
UPDATE:
Let me go a little further on the explanation of what I'm trying to achieve. I have many graphs, each of them placed on a separated panel (or tab). If I left-click in a position of a graph, I want to show the Tracker info on that specific position on each and every graph while the user doesn't click on another position (setting a new Tracker and removing the old one). In other words, it is allowed one Tracker at a time, and this is set on every existing graph at the same X-position.
UPDATE 2:
Alright, so after giving it a thought, I decided not to maintain the Tracker when left-clicking. Instead, I just want to draw a vertical line on every graph on the position of the click. Just one line at a time is permitted, it can be treated as a "temporary marker". I believe this is more doable than the other approach and will suffice my needs.
You can unbind the left button and replace it by just hovering the mouse, almost always showing the tracker:
public PlotController customController { get; private set; }
customController = new PlotController();
customController.UnbindMouseDown(OxyMouseButton.Left);
customController.BindMouseEnter(PlotCommands.HoverSnapTrack);
Xaml:
<oxy:PlotView Controller="{Binding customController}"/>
EDIT >>>>>>
TrackerManipulator code: https://github.com/ylatuya/oxyplot/blob/master/Source/OxyPlot/Manipulators/TrackerManipulator.cs
I think you should create a custom TrackerManipulator, and override the Complete method to not to do the "this.PlotControl.HideTracker();" line.

Mouse move on WPF image control using Kinect

I am trying to move the mouse using the kinect . I achieved using the interopservices in c#.
Now i want to move mouse only inside the image control . so mouse should not move on other layouts. Is there any way to achieve the mouse movement without using the interopservices.
Cursor.Position = new Point()
Will let you move the cursor. You can restrict where in the code too.
Is that what you're looking for, or am I missing something? There really isn't anything specific to the Kinect that I can see.
EDIT:
You can find the tracking function I use in the following post:
how to use skeletal joint to act as cursor using bounds (No gestures)
In it, I assign the position of the hand to a "RightHandX" and "RightHandY" parameter. These are basically the mouse position -- you could replace them with a call to Cursor.Position.
If you only want to move the mouse around the image, you can get the bounds of the image and then just add another 'if' statement that does or doesn't send a Cursor.Position based on those bounds and the calculated position of the hand.

How to capture mouse coordinates from another program

I'm trying to write a WinForms program that will capture mouse coordinates upon pressing and (more importantly) releasing the middle mouse button.
My form has topmost set to true (so that text in it can always be visible even when it doesn't have focus).
What I'm aiming for is being able to hover the mouse over a game window after my program starts, hit the middle mouse button, and have it record the mouse position for later use.
I can get it to detect when the middle mouse button is clicked inside the form using the MouseUp event (bound to the form itself) but have no clue what I need to do to have it detect when the mid mouse is clicked outside my form.
Thanks for any help guys.
I believe what you are after are called low level hooks. A quick google brings up this: Erroneous Mouse Coordinates Returned from Low Level Mouse Hook C#
A microsoft example of how to do can be found here: http://support.microsoft.com/kb/318804

WinAPI mouse move notification for full desktop

In WinAPI is there a mouse move notification for the full desktop (full screen) and not for a window only?
I would like to receive mouse screen coordinates in my main window procedure.
Edit:
What I try to do is getting the coordinates from the mouse when dragging from a button in my window to outside that window.
Not as such, no. If you wanted to do something anywhere on the desktop from within your program, e.g. point somewhere or draw something anywhere, you could capture the mouse and then follow the movement until the mouse button is released. See SetCapture for this.
For an example, see this article on MSDN: Drawing Lines with the Mouse
Otherwise you can always use Windows hooks to follow mouse movements anywhere.
You can set a mouse hook to be notified about all mouse events.
You can use GetCursorPos, or GetMessagePos that contains coordinate of the last message

Form does not restore to previous size after minimize all

The form is set up to have no maximize button. When Win+D is pressed, all windows minimize including the form. When clicking on the application's taskbar entry, the form shows up but does not restore to the previous size. The form is reduced to just showing the title bar and the borders, w/o the client area.
I tried listening for WM_SIZE message's SIZE_RESTORED value, but couldn't seem to get the right size from RestoreBounds or GetWindowPlacement.
I'll try to do it the other way and look for SIZE_MINIMIZED and capture the rect, then restore from that rect on SIZE_RESTORED, when I come back.
In the meantime, any of you have an idea why the form does not restore correctly in the first place so I can apply a proper fix. Or suggest a better approach, if the proper fix is not possible.
You are doing something weird with your form - try creating an application from scratch, and observe the behavior on empty default form.
Then, go from there - add changes to the new form/project and see when the problem will start to occur.

Resources