Ciao Ho cercato ma non riesco a trovare la risposta. Come faccio a sapere quando lo schermo si sta spegnendo o accendendo. Non SystemEvents.PowerModeChanged. Non so come recuperare i display/schermo EVENTIC# Come ottenere gli eventi quando lo schermo/display si spegne o si accende?
private const int WM_POWERBROADCAST = 0x0218;
private const int WM_SYSCOMMAND = 0x0112;
private const int SC_SCREENSAVE = 0xF140;
private const int SC_CLOSE = 0xF060; // dont know
private const int SC_MONITORPOWER = 0xF170;
private const int SC_MAXIMIZE = 0xF030; // dont know
private const int MONITORON = -1;
private const int MONITOROFF = 2;
private const int MONITORSTANBY = 1;
[DllImport("user32.dll")]
//static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
private static extern int SendMessage(IntPtr hWnd, int hMsg, int wParam, int lParam);
public void Init(Visual visual)
{
SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged;
HwndSource source = ((HwndSource)PresentationSource.FromVisual(visual));
source.AddHook(MessageProc);
Handle = source.Handle;
}
public void SwitchMonitorOff()
{ // works
SendMessage(Handle, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOROFF);
}
public void SwitchMonitorOn()
{// works
SendMessage(Handle, WM_SYSCOMMAND, SC_MONITORPOWER, MONITORON);
}
public void SwitchMonitorStandBy()
{// works
SendMessage(Handle, WM_SYSCOMMAND, SC_MONITORPOWER, MONITORSTANBY);
}
private IntPtr MessageProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (msg == WM_SYSCOMMAND) //Intercept System Command
{
// not finished yet
// notice the 0xFFF0 mask, it's because the system can use the 4 low order bits of the wParam
// value as stated in the MSDN library article about WM_SYSCOMMAND.
int intValue = wParam.ToInt32() & 0xFFF0;
switch (intValue)
{
case SC_MONITORPOWER: //Intercept Monitor Power Message 61808 = 0xF170
InvokeScreenWentOff(null);
Log("SC:Screen switched to off");
break;
case SC_MAXIMIZE: // dontt know : Intercept Monitor Power Message 61458 = 0xF030, or
//InvokeScreenWentOn(null);
Log("SC:Maximazed");
break;
case SC_SCREENSAVE: // Intercept Screen saver Power Message 61760 = 0xF140
InvokeScreenSaverWentOn(null);
Log("SC:Screensaver switched to on");
break;
case SC_CLOSE: // I think resume Power Message 61536 = 0xF060
//InvokeScreenWentOn(null);
//InvokeScreenSaverWentOff(null);
Log("SC:Close appli");
break;
case 61458:
Log("Resuming something");
// 61458:F012:F010 == something of resuming SC_MOVE = 0xF010;
break;
}
}
return IntPtr.Zero;
}
EDIT
Forse posso spiegare la mia intensione, per cui v'è forse una soluzione migliore. Ho un servizio WCF con binding Dual in esecuzione. Funziona su un archos (tablet pc portatile). Voglio che quando l'utente smette di funzionare per un periodo di inattività, la connessione si chiude immediatamente e quando il computer ritorna da inattivo, si riconnette immediatamente. L'idea di Application Idle on Code project da parte di Tom è già una buona idea. Minore è il consumo di energia, meglio è. L'avvio deve essere il più veloce possibile.
Non proprio, come si fa a recuperare gli eventi. Come sapere se è acceso o spento? - Dopo 10 minuti lo schermo si oscura. - Dopo 15 minuti il mio schermo si spegne. - Sposto il mouse, lo schermo si accende. Come ottenere questi eventi? (non gli eventi di sessione) – Manu
@Manu: Lasciami modificare questa risposta per includere un esempio di come recuperare gli eventi .... – t0mm13b
Wow, interestin. Non pensavo che fosse troppo complicato. Non sono un esperto, sicuramente non con il registro. Quindi ho un po 'paura di toccarlo. "Application Idle" è già una buona idea. – Manu