I see how to get the coordinates of the mouse relative to the entire screen when doing Drag & Drop events, but I'm a bit unclear on the best way to get the mouse coordinates relative to the control the drop occurs in. Do I have to calculate it based on the controls position in the form and the form's position in the screen or is there a more straightforward way of doing it?
You can use Control.PointToClient method:
yourTargetControl.PointToClient(screenPoint);
Related
I want to be able to drag around a 100% zoomed picture in a picturebox: http://spunit.tk/x/dragpic1.png.
I want it to work exactly like the Windows Photo Viewer: http://spunit.tk/x/dragpic2.png.
How is this possible?
I believe you need to maintain the coordinates of that picturebox, also set its view style to full-image, without any stretching.
Then, you will need three mouse events: mouse down, mouse up and mouse move, where you can get the mouse coordinates and capture or release mouse to translate the picture box according to mouse delta translation.
I have a silverlight application in which I have to click on some pictures, if I hover over them for 3 seconds approx.
The problem is that if the pictures are a bit small in size, and the mouse moves a little, it moves out of the respective picture clicking area and selects another picture.
I have tried using a custom image in place of the default mouse cursor, but can this mouse be enlarged in some way so that it has a larger clicking area under it and not only the tip of the mouse pointer?
I think you're thinking about this the wrong way around. The mouse pointer simply defines a coordinate on the screen, rather than an area. If you want mouseover/click etc to be more generous, and give a wider area of interaction, you should make the target area larger.
So in the case of some small image, you can surround it with a larger area to handle the mouseover or click events, for example by surrounding it by a transparent border (note that elements with a transparent background will receive mouse events, unlike elements with no background).
I'm trying to display a context menu where the user clicks the mouse in a WPF application. I've handled the OpenContextMenu event and the handler has a pair of doubles, e.CursorLeft and e.CursorTop which are the coordinates of the mouse click relative to the control that was clicked (in this case a DataGridCell). If I display the context menu using those coordinates it appears relative to the application window offset by the cursor amounts.
How can I convert those DataGridCell-relative Cursor coordinates into window coordinate space?
You can use the UIElement.TranslatePoint(Point, Visual) method to convert coordinates from one control's coordinate space to another. The following code should do what you want (not tested!):
Point target =
myDataGridCell.TranslatePoint(new Point(e.CursorLeft, e.CursorTop), Application.Current.MainWindow);
However, if you only want to display a context menu, you could also simply assign the FrameworkElement.ContextMenu property for the control which should show the context menu. This way, the position will be positioned at the mouse cursor automatically. If you have a more complicated scenario, you might still use the method above.
How can i create a canvas of this kind - http://www.silverdiagram.net/Projects/SilverDiagram/SilverDiagram_Demo.aspx? I want to position the controls exactly aligned to each other. I want to help users to align it properly using auto-alignment (like visual studio) and by providing user the scale.
The Canvas is the right container to use as you can position its child control by coordinates. Your problem is more about moving elements around, which is in fact drag & drop.
You'll have to handle mouse events: MouseDown to select an item, MouseMove to detect a drag and MouseUp to drop it. During the move or at the drop, you can change the element's coordinates to auto-align it on a grid or compared to surrounding elements.
I successfully made such a project in Silverlight and used Adorners to add resize handlers to the elements.
I solved the panning and zooming question by putting the Canvas into a Viewbox, which was in a ScrollViewer. If you want to let the user control the zoom factor, apply a LayoutTransform to the Canvas (available in the Toolkit).
I have a wpf application that has some shapes on a canvas I want to allow the user to click on a shape and then the shape gets stuck the the mouse until they click again.
So far I know very little about WPF so go easy on me ;)
Hopefully this is what you're looking for.
"Mouse dragging logic is fairly straightforward: In the OnMouseDown handler, you save the position of both the object you want to drag and the mouse pointer, and you call CaptureMouse. In OnMouseMove, you calculate the difference between the coordinates of the current mouse pointer position and the saved position, and add that to the original object position. (If you're on a Canvas, you can move the object by calling Canvas.SetLeft and Canvas.SetTop for the object; otherwise you can adjust a TranslateTransform object set to the object's RenderTransform property.) In OnMouseUp, you call ReleaseCapture.
Because your app can lose the mouse capture in other ways (such as the appearance of a system modal message box), you'll also want to override OnLostMouseCapture to abort the dragging operation (if it hasn't terminated with OnMouseUp) and perform cleanup. You might also want to override OnTextInput to abort the drag if the user presses the Escape key."
Copied from http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b6c51eef-269e-4c85-96af-b5b1e4cb9bd5/ there's also code up on this site for how to do it.
Check out this Thread - http://silverlight.net/forums/t/68889.aspx
Since your 'Stick' is on the Canvas, keep setting the Canvas.Left and Canvas.Top on MouseMove with MousePositions