Sie sind auf Seite 1von 3

Description

The example that we're going to implement it through the Visual Studio 2010, target framework 4.0.Tutto this is feasible as mentioned above using the library user32.dll present natively in the operating system Microsoft Windows.Il following code uses this library usingthe function GetLastInputInfo . GetLastInputInfo is a function only of the applicable software for Windows, (not on a website).GetLastInputInfo returns a Boolean value, if the function fails it returns zero or any value other than zero. This value is in millisecondsin the end the user's idle time with the application. The application on which you will perform the example of how technology likeWPF, Microsoft VisualBasic language development Net.Diamo only a brief introduction to the technology we will use to create ourexample, WPF stands for Windows Presentation Foundation and was introduced in. NET Framework 3.0 was released in January 2007with Windows Vista. WPF you can create desktop applications with attractive graphics using XAML, which is nothing but an XML (the base is) with the addition of many new objects including tags, styles, templates, and triggers a very altro.Per further study of this language online documentation on MSDN Library.Tornando to our example, once we open Microsoft Visual Studio 2010 and createdthe type of WPF application, will be created automatically by VisualStudio two rows, and MainWindow MainWindow.xaml . xaml.vb.Ilfirst is the file that contains the markup, ie the graphics created using XAML, XAML, and said to remember that even declarative code,while the second is called code-behind file, with which we manage events objects created using XAML and other functions as needed. Visual Basic

'Recall the framework dll Imports System.Runtime.InteropServices Imports System.Windows.Threading 'MainWindow Class Partial Public Class MainWindow 'At the module level declare a variable of type integer, we call totalTime wi th initialized to zero. 'TotalTime we will indicate how many seconds we do not interact with key board and mouse. Dim totaltime As Integer = 0 'Declare a new instance of the structure LASTINPUTINFO contained in GetLastIn putInfo. Dim lastInputInf As New LASTINPUTINFO() 'The following code calls the library user32.dll GetLastInputInfo and functio n. <DllImport("user32.dll")> Shared Function GetLastInputInfo(ByRef plii As LASTINPUTINFO) As Boolean End Function 'The DllImport attribute is very useful when reusing existing unmanaged code in a managed application. 'How can we find out within the term structure LASTINPUTINFO MarshalAs. 'Marshal is responsible for marshal data between managed and unmanaged c ode. <StructLayout(LayoutKind.Sequential)> Structure LASTINPUTINFO <MarshalAs(UnmanagedType.U4)> Public cbSize As Integer <MarshalAs(UnmanagedType.U4)>

Public dwTime As Integer End Structure 'Loaded event of the MainWindow class. 'In Loaded event is declared a new object DispatcherTimer 'combined with a Tick event, this event attracts every intervention meth od DisplayTime which updates an object label thus displaying the user's idle time with the application. 'It should be noted that in WPF does not exist the Timer control, such c ontrol is available in the type applications WindowsForm, but as explained ab ove being an application of type WPF, manage the whole with the control Dispa tcherTimer integrated in the namespace System.Windows. threading. Private Sub MainWindow_Loaded(sender As Object, e As System.Windows.Route dEventArgs) Handles Me.Loaded Dim dt As DispatcherTimer = New DispatcherTimer() AddHandler dt.Tick, AddressOf dispatcherTimer_Tick dt.Interval = New TimeSpan(0, 0, 1) dt.Start() End Sub 'Tick Event object DispatcherTimer 'Method is invoked DisplayTime () every second, it depends on the value range set in the Interval property of DispatcherTimer. Public Sub dispatcherTimer_Tick(ByVal sender As Object, ByVal e As EventA rgs) DisplayTime() End Sub 'Function GetLastInputTime. 'This function returns the variable idletime and assigns the value of id le time spent, 'Everything converted in seconds because initially the value retrieved f rom this intruzione 'code = idletime Environment.TickCount lastInputInf.dwTime and expressed in milliseconds. 'Within the class MainWindows, also declare a variable of type integer a nd call idletime which will be enhanced by Function GetLastInputTime. Public Function GetLastInputTime() As Integer Dim idletime As Integer idletime = 0 lastInputInf.cbSize = Marshal.SizeOf(lastInputInf) lastInputInf.dwTime = 0 If GetLastInputInfo(lastInputInf) Then idletime = Environment.TickCount - lastInputInf.dwTime End If If idletime <> 0 Then Return idletime \ 1000 Else Return 0 End If End Function 'Sub DisplayTime () 'This Sub does nothing but display inside the value in seconds

'the time in which you interact with your application. 'All ()' inside of this sub can handle it according to your needs and ho w to perform certain situations in the application, for example if one has pr eviously performed a login by the user after a certain period of inactivity b y developer, you log back in again and resume the application which had been left by the user display of last time. Private Sub DisplayTime() totaltime = GetLastInputTime() If GetLastInputTime().Equals(1) Then Label1.Content = "idle time equal to" & " " & GetLastInputTime.ToString & " " & "secondo" Else Label1.Content = "idle time equal to" & " " & GetLastInputTime.ToString & " " & "second" End If End Sub 'Label1 visualizes the user's idle time. 'In a real application, the label will never be used as the count should never be shown to the user, this has been implemented only to show the actua l operation of the application. After a period of time the application is dis connected and then the user must re login again to resume intragire with the application. 'For this purpose, with the Mouse Move event on Form MainWindow running inside the control variable totalTime, if more than five seconds is called Lo ginForm1 form and displays it as Modal Forms user, otherwise everything proce eds without normanlemte the application requires the user to login. Private Sub Window_MouseMove(sender As System.Object, e As System.Windows .Input.MouseEventArgs) Handles MyBase.MouseMove If totaltime > 5 Then Dim log As New FrmLogin log.ShowDialog() End If End Sub End Class

Das könnte Ihnen auch gefallen