How do I convert ContextMenuEventArgs cursor location to window coordinates - c#

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.

Related

Change pointer cursor when hovering over map elements

My UWP application contains a map with several POI. I am trying to change the mouse cursor from an arrow to a hand when hovering over specific poi to indicate its clickable.
This would change the cursor as soon as it enters the map still, as a simple test, I added a PointerEntered event for the mapcontrol and within it I have the following to change the cursor:
Window.Current.CoreWindow.PointerCursor = new Windows.UI.Core.CoreCursor(Windows.UI.Core.CoreCursorType.Hand, 0);
It appears though the cursor does change however immediately gets overridden back to the pointer cursor.
Edit: Just realised When a poi is clicked (i.e. is selected) the cursor changes to a hand even when not over the map control until the poi is unselected. No good as I would like the cursor to change dynamically when hovering over a poi and revert back to cursor when moved away.
Change pointer cursor when hovering over map elements
I'm afraid you can't edit the default cursor for map element, Because it has handled internally, it will not fired, even you has listen PointerEntered event, it consumed by the control and not passed up the control chain. If you do want this feature, the better way is post this feature with windows feed backhub app.
I don't know if it works just like WinForms, I had to do something like this to click on labels (couldn't use link-labels), what I used was in the Mouse_Move event of the label and it was basically
if (Cursor.Current == Cursors.Default)
{
Cursor.Current = Cursors.Hand;
}
and similar changes and behaviors due to the various conditions. This however got me a small issue: this statement changes the mouse graphic anytime you move on the control, but personally on Windows settings I use the trail graphic function for the mouse (leaving a trail of pointers whenever I move the mouse on the screen), what I suggested you disables this function, or better, it conceals it, since it "recreates" the mouse graphic for every move you do onto the control, and thus it "undoes" the graphic for the mouse and recreates it as a Hand (in my instance). If it doesn't concern you though, it works just fine.
Just I repeat myself: I use this on WinForms, but since it's C# I suppose it just will work(?)

Getting mouse coordinates relative to target control in drag/drop operations

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);

How to make ComboBox Dropdown to draw in Upward direction

In Winform, i have a ComboBox at the bottom of the form.
when i run the application, ComboBox draws dropdown list in downward direction which goes out of the form borders.
how can i make ComboBox Dropdown list to draw in Upward direction ?
Thanks in advance.
It's not immediate but you can do it, I'll just outline the steps you need:
Attach an event handler to ComboBox.DropDown.
Convert the Left/Bottom location of the ComboBox to screen cordinates and add them an offset (+1 for example to both values).
Use use WindowFromPoint() to get the handle of the dropped down window (it's below the control, that's why +1).
Get the bounds of the dropped window (you can use CB_GETDROPPEDCONTROLRECT or GetWindowRect(), as you prefer).
Move the window to the new location (ComboBox top - dropped down window height) using MoveWindow().
That's all
EDIT
Note that you may merge point 2 and 4, with a single SendMessage with CB_GETDROPPEDCONTROLRECT you can get the bounds of that window and the location to use as parameter for WindowFromPoint(). In this way you do not assert that the dropped down window is always downward (it's not sure when the window touches the screen bounds and it may even change in future versions).

What is a Cursor File in .NET?

I saw the Cursor File in the dialog box when adding a new item. Actually what it will do and what is the use of it ?...Thanks..
It contains the Image(s) to draw a Cursor (mousepointer) on your screen.
From MSDN:
A cursor is a small picture whose
location on the screen is controlled
by a pointing device, such as a mouse,
pen, or trackball. When the user moves
the pointing device, the operating
system moves the cursor accordingly.
You can use Visual Studio to create Cursor Files (.cur) which can then be loaded using the Windows Forms Cursor class or Win32 API calls such as LoadImage.
Start a new project from the Windows Forms Application template. Project + Add New Item, select Cursor File. Draw something. Project + Properties, Resources tab. Drag and drop the cursor onto the resource window so a resource is added. Make your form constructor look like this:
public Form1() {
InitializeComponent();
this.Cursor = new Cursor(new System.IO.MemoryStream(Properties.Resources.Cursor1));
}
Press F5 and have a look-see at your new mouse cursor.
If you want to change the cursor when it hovers above an object on your user interface and you are not satisfied with the builtin cursor you can create a custom cursor. Custorm cursors is like a small image, but has some extra properties:
Transparency mask so the cursor can have a shape that is not rectangular.
Hotspot location used to determine the mouse coordinates when the cursor is moved. Some cursors have the hotspot in the upper left corner (e.g. the arrow) while other have in the middle (e.g. the cross-hair) etc.

How do I "Stick" a shape to the mouse in WPF?

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

Categories