Original: WPF Window Auto-hide for long time unattended mouse
In software development, there are times when you can hide the mouse after a period of inactivity, possibly for the following reasons:
1. For security, especially those who need to use the user name and password to log on to the service side of the program, often consider long-term unmanned operation, the program automatically jump to the user login interface;
2. The software needs to hide the mouse for a better playback effect.
Here is the second case, when WPF does play, it needs to hide the mouse.
The idea is: if the 3s mouse does not move the hidden, designed the timer interval of 1s, and add the mouse did not move the counter, the counter reached 3 to execute the program. Implementation is this: every 1s detect whether the mouse is moved, if not move the counter plus 1, if the mouse moves halfway, the counter to zero, to reach the counter count of 3, then 3 mouse detection in the mouse does not move, so from the mouse to stop moving, to the counter to 3, just 3s, can achieve 3s mouse does not move the hidden program execution;
A well-encapsulated class file to monitor mouse movement:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Windows;usingSystem.Runtime.InteropServices;namespaceEbs.common { Public classMousemonitorhelper {Private StaticPoint MousePosition;//the position of the mouse Public Static intCheckcount;//number of mouse positions detected//determine if the mouse is moving Public Static BOOLhaveusedto () { point point=Getmousepoint (); if(Point = = mouseposition)return false; MousePosition= point;return true; } [StructLayout (layoutkind.sequential)]Private structMpoint { Public intX; Public intY; PublicMpoint (intXinty) { This. X =x; This. Y =y; }} [DllImport ("user32.dll", CharSet =CharSet.Auto)]Private Static extern BOOLGetCursorPos ( outmpoint MPT); //Gets the current screen mouse position Public StaticPoint Getmousepoint () {mpoint MPT=NewMpoint (); GetCursorPos ( outMPT); Point P=NewPoint (MPT. X, MPT. Y); returnp; } }}
View Code
Second, the program calls the timer event:
PrivateDispatcherTimer Timer_mousemove;Private voidWindow_Loaded (Objectsender, RoutedEventArgs e) { //set the mouse to hide, display This. Timer_mousemove =NewDispatcherTimer (); This. Timer_mousemove.tick + =NewEventHandler (Timer_mousemove_tick); This. Timer_mousemove.interval =NewTimeSpan (0,0,1); This. Timer_mousemove.start (); }Private voidTimer_mousemove_tick (Objectsender, EventArgs e) { Try { if(!mousemonitorhelper.haveusedto ()) {Mousemonitorhelper.checkcount++; if(Mousemonitorhelper.checkcount = =3) {Mousemonitorhelper.checkcount=0; //Close button Hide, mouse hide This. cnsexist.visibility =Visibility.hidden; Mouse.overridecursor=Cursors.none; } } ElseMousemonitorhelper.checkcount =0; } Catch { Throw Newnotimplementedexception (); } }Private voidDockpanel_mousemove (Objectsender, MouseEventArgs e) { This. cnsexist.visibility =visibility.visible; Mouse.overridecursor= Cursors.arrow;
View Code
In addition, if you have design-to-mouse focus, refer to the WPF program for a long time unattended operation
WPF Windows Auto-hide for long time unattended mouse