Dans mon projet, j'ai besoin d'un verrou d'application (comme verrouillent fenêtres). Si l'application est inactive pendant une période donnée, l'application doit être verrouillée, c'est-à-dire que la fenêtre de connexion de l'application apparaîtra. Comment puis-je le faire dans une application WPF C#?Vérifiez si une application est inactif pendant une période de temps et le verrouiller
Répondre
Vous pouvez utiliser ces fonctions
voir ce code, vous devez ajouter une minuterie à votre formulaire et définissez this.timer1.Enabled = true;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication9
{
internal struct LASTINPUTINFO
{
public uint cbSize;
public uint dwTime;
}
public partial class Form1 : Form
{
[DllImport("User32.dll")]
public static extern bool LockWorkStation();
[DllImport("User32.dll")]
private static extern bool GetLastInputInfo(ref LASTINPUTINFO Dummy);
[DllImport("Kernel32.dll")]
private static extern uint GetLastError();
public static uint GetIdleTime()
{
LASTINPUTINFO LastUserAction = new LASTINPUTINFO();
LastUserAction.cbSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(LastUserAction);
GetLastInputInfo(ref LastUserAction);
return ((uint)Environment.TickCount - LastUserAction.dwTime);
}
public static long GetTickCount()
{
return Environment.TickCount;
}
public static long GetLastInputTime()
{
LASTINPUTINFO LastUserAction = new LASTINPUTINFO();
LastUserAction.cbSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(LastUserAction);
if (!GetLastInputInfo(ref LastUserAction))
{
throw new Exception(GetLastError().ToString());
}
return LastUserAction.dwTime;
}
public Form1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (GetIdleTime() > 10000) //10 secs, Time to wait before locking
LockWorkStation();
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Start();
}
}
}
Définir un délai d'attente de la charge, et chaque fois qu'une action « active » se produit (vous devrez brancher les), remis à zéro le minuteur de retour au début.
y at-il des exemples d'applications disponibles? – Sauron
OMI, la réponse acceptée est pas aussi bon que cette méthode:
http://www.codeproject.com/Articles/30345/Application-Idle
L'article CodeProject utilise des messages Windows qui provoque le composant à considérer l'application ne chôme pas, par exemple
public enum ActivityMessages : int
{
/// <summary>
/// Cursor moved while within the nonclient area.
/// </summary>
WM_NCMOUSEMOVE = 0x00A0,
/// <summary>
/// Mouse left button pressed while the cursor was within the nonclient area.
/// </summary>
WM_NCLBUTTONDOWN = 0x00A1,
/// <summary>
/// Mouse left button released while the cursor was within the nonclient area.
/// </summary>
WM_NCLBUTTONUP = 0x00A2,
/// <summary>
Savez-vous comment obtenir une application MVC? Ce – alice7
détecte lorsque le système dans son ensemble est inactif, pas quand une application spécifique est inactif. –