Hide UWP mouse cursor on program load - c#

Is there a way to hide the mouse cursor in a windows store application in code so that the mouse is hidden immediately on application start?
I run the following command as my page starts and nothing happens until I physically move the mouse. As soon as I move the mouse it hides. If I don't move it it stays on screen indefinitely. As I'm displaying graphics I need to hide the mouse immediately without any input from the user.
Window.Current.CoreWindow.PointerCursor = null;

As Colin mentions in the comments the mouse pointer disappears if you hide it when the window fires the Loaded event.
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
Window.Current.CoreWindow.PointerCursor = null;
Window.Current.CoreWindow.PointerPosition = new Point(42, 42);
}
My problem is the WebView element, it briefly flickers the mouse pointer once when moving it even with this code.

Related

Mouse Cursor Flickering while peforming DragOver event on Panel control in winform in C#

I am trying to implement a DRAG DROP event in windows form. The Drag event starts when I select some text in text box, click and hold the right mouse button and start dragging the text from the text box, passes over a panel(Right mouse button still hold/clicked) and then the dragged text has to be dropped over a tree view(release right mouse button).
I want my mouse cursor to change when i am dragging over the panel control. The Cursor do change to my expected cursor icon as of now but it flickers a lot and cursor image is not constant.
I am implementing the logic in Drag Over event handler which i think gets fired again and again automatically while dragging and this results in flickering or some sort of refreshing.
My implementation is as follows:
ABC.Desginer.cs:
this.splitterPanel.DragOver += new System.Windows.Forms.DragEventHandler(this.splitterPanel_DragOver);
ABC.cs:
private void splitterPanel_DragOver(object sender, System.Windows.Forms.DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Text))
{
Bitmap bmp = new Bitmap(ABC.DefInstance.ImageList1.Images[3]);
Cursor c = new Cursor(new Bitmap(bmp, 32, 32).GetHicon());
frmFlat2Obj.DefInstance.splitterPanel.Cursor = c;
DestroyIcon(c.Handle);
}
}
My question is: How exactly shall I stop or reduce the mouse cursor flickering while dragging over the Panel??
THANKS IN ADVANCE :)

Run code after the user has finished moving the window?

I've made a WPF application and one of my windows is required to go fullscreen, however I'm trying to get it so that if the user drags the window to another monitor it will automatically resize to suit that monitor.
I've tried the previewmouse up down events, mouse up down events to set a bool that the window is being dragged to prevent the code from executing, however this does not appear to work and the window is still trying to resize itself as its being dragged and the mouse is down (as if mousedown is being set to false)
The code that resizes the window is in the window located changed event
Is there any other ways that can effectively detect that the mouse is down and dragging the window to another monitor before executing the code to resize it to suit?
Whilst the user is dragging the window I dont want the code to resize it to be executed, once the user has finished dragging the window, the code to resize the window to the new screen should be executed.
Currently I cant get the code to run after the window has finished moving.
Upon further investigation, no mouse up events are fired after the mouse is released on the title border.
You can override OnPreviewMouseMove for any UI element.
protected override void OnPreviewMouseMove(MouseEventArgs e)
Then using the event args of that handler (of type MouseEventArgs) - you check if the left mouse button is pressed (indicating a drag).
e.LeftButton == MouseButtonState.Pressed
When you start dragging a maximized Window, the size changes. This is how Windows behaves and is natural to a user. On end of drag you can just set the state back to maximized.
Application.Current.MainWindow.WindowState = WindowState.Maximized;
Did you checked this event ? Sounds like what you need.
https://msdn.microsoft.com/en-us/library/system.windows.window.locationchanged%28v=vs.110%29.aspx

How can I make an imitation of a button control with a picture box, that behaves exactly like a button?

Buttons do that: if the user preses and holds he left mouse button over a button control and moves the cursor out of the control, the button control changes it's appearance back to default, and if the mouse button is still being held, and the cursor enters a button control, the control changes it's appearance to it's pressed version. I'm trying to imitate a button using a PictureBox, but when the mouse leaves the PictureBox before the left mouse button is released, the PictureBox's picture doesn't change until the mouse button is released.
I'm trying to do this because the button control can't look the way I want to.
How can I make an imitation of a button control with a picture box, that behaves exactly like a button?
This is by design, the control sets the Capture property to true so it will keep receiving mouse messages while the button is held down when you move the mouse outside of the control rectangle.
You could turn it off when you see it moving out:
private void pictureBox1_MouseMove(object sender, MouseEventArgs e) {
var box = (PictureBox)sender;
if (!box.DisplayRectangle.Contains(e.Location)) box.Capture = false;
}
Try using the DragLeave event as opposed to the MouseLeave event.

WPF Forcing MouseDevice to be pressed

I have a windows form in a wpf window, and I'm trying to use DragMove when I click on the windows form, it's a picturebox so I want to be able to drag the window around just by clicking the picture.
I catch my form's mouse down, and raise the wpf window's mouseleftbuttondown event with:
if (e.Button == MouseButtons.Left)
{
MouseDevice mouseDev = InputManager.Current.PrimaryMouseDevice;
MouseButtonEventArgs mouseEvent = new MouseButtonEventArgs(mouseDev, 0, MouseButton.Left)
{
RoutedEvent = MouseLeftButtonDownEvent
};
RaiseEvent(mouseEvent);
}
However whenever I check the InputManager.Current.PrimaryMouseDevice from my handler (or my form's MouseMove handler), the LeftButton's state is "released".
Why is this? I can't figure out a way to force it to be "pressed" since all the properties are read-only.
Or is my approach simply wrong and is not possible? I did also try setting the location of my window on mouse move, but some weird stuff happens where my mouse values keep going back to the previous position.
Thanks!
edit: So I'm manually adjusting the window location, but still hope someone can enlighten me as to why MouseDevice doesn't get pressed on a windows form. The "weird stuff happens..." was just a dumb mistake on my part, I kept resetting the mouse coordinates on mouse move, but realized that my mouse never moves relative to the window since the window is moving too, duh!
A similar issue stumped me for a while: the ButtonState property of MouseButtonEventArgs reflects the real-time state of that button, not a state snapshot taken when the event was raised. I wonder if the same holds true re your accessing LeftButton's state.
Hope this helps,
Ben

How can I detect a held down mouse button over a PictureBox?

I need to fire an event when the mouse is above a PictureBox with the mouse button already clicked and held down.
Problems:
The MouseDown and MouseEnter event handlers do not work together very well.
For instance once a mouse button is clicked and held down, C# will fire the MouseDown event handler, but when the cursor moves over the PictureBox the MouseEnter event does not fire, until the mouse button is realeased.
When the mouse is pressed down most controls will then Control.Capture the mouse input. This means that all MouseMove events are sent to the original control that captured rather than the control the mouse happens to be over. This continues until the mouse loses capture which typically happens on the mouse up.
If you really need to know when the mouse is over your control even when another control has captured mouse input then you only really have one way. You need to snoop the windows messages destined for other controls inside your application. To do that you need add a message filter ...
Application.AddMessageFilter(myFilterClassInstance);
Then you need to implement the IMessageFilter on a suitable class...
public class MyFilterClass : IMessageFilter
{
public bool PreFilterMessage(ref Message m)
{
if (m.Msg == WM_MOUSEMOVE)
// Check if mouse is over my picture box!
return false;
}
}
Then you watch for mouse move events and check if they are over your picture box and do whatever it is you want to do.
Mouse events
Use the MouseDown event to just detect a down press of a mouse button and set this.Capture to true so that you then get other mouse events, even when the mouse leaves the control (i.e. you won't get a MouseLeave event because you captured the mouse). Release capture by setting this.Capture to false when MouseUp occurs.
Just checking the state of the mouse
This may not be relevant, but you can check System.Windows.Control.MousePosition and see if it is in the PictureBox.ClientRectangle, then check the Control.MouseButtons static property for which buttons might be down at any time.
As in:
if (pictureBox.ClientRectangle.Contains(pictureBox.PointToClient(Control.MousePosition)))
{
if ((Control.MouseButtons & MouseButtons.Left) != 0)
{
// Left button is down.
}
}
Set up a MouseMove event within the PictureBox control:
this.myPictureBox.MouseMove += new System.Windows.Forms.MouseEventHandler(this.myPictureBox_MouseMove);
Then, within your MouseMove event handler, check to see if the left mouse button (or whatever) is pressed:
private void myPictureBox_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
// Do what you want to do
}
If you're trying to implement a drag-and-drop operation of some sort, the Drag... events (DragEnter, DragDrop etc.) on the receiving picture box are what you want to use. Basically, you start the drag operation using the DoDragDrop method of the source control, and then any control that you drag over will have its Drag... events raised.
Search "DoDragDrop" on MSDN to see how to implement this.
You can use the Preview Events
For example say I want to detect a mousedown event on my button. The MouseDown event is not going to work because as one of the answers here, the mouse capture is sent to the main control, however what you can do is use the mouse preview event.
Here is a code example
I want to check when the Left Mouse Button is pressed on my Button, hence I use the PreviewMouseLeftButtonDown
private void MyButton_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
// code here
}
WPF has preview events for alot of other events, you can read about them here
Preview Events - It particular talks about Buttons and how the mouse events interacts with it, So I highly recommend you read it
The best way to move a Form based on mouse position and control relative position is similar to what Ian Campbell posted.
private void imgMoveWindow_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Form1.ActiveForm.Left = Control.MousePosition.X - imgMoveWindow.Left - (imgMoveWindow.Size.Width/2);
Form1.ActiveForm.Top = Control.MousePosition.Y - imgMoveWindow.Top - (imgMoveWindow.Size.Height/2);
}
}
Where imgMoveWindow is a PictureBox Control.
Bruno Ratnieks
Sniffer Networks
You should try MouseMove of the picture box instead of MouseEnter, MouseMove will normally fire regardless mouse button state.
set a flag or a state on mouse down. release it on mouse up.
When on mouse over fires for the picture box check your state.
Now you can detect when a person is dragging something.

Categories