嗨,我一直在尋找,但我找不到答案。我如何知道屏幕何時開啓或關閉。不是SystemEvents.PowerModeChanged。 我不知道如何來檢索顯示器/屏事件c#當屏幕/顯示屏關閉或打開時,如何獲取事件?
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;
}
編輯
也許我可以解釋我的意圖,所以有可能是一個更好的解決方案。我有一個雙綁定WCF服務運行。它運行在一個檔案館(便攜式平板電腦)上。我希望當用戶停止空閒工作時,連接立即關閉,並且當計算機從閒置返回時,他立即重新連接。來自Tom的Application Idle on Code project的想法已經是一個好主意。功耗越低越好。啓動必須儘可能快。
不是真的,你怎麼找回事件。如何知道它是通電還是斷電? - 10分鐘後,我的屏幕變暗。 - 15分鐘後我的屏幕熄滅。 - 我移動鼠標,屏幕變爲ON。 如何獲取此事件? (不是會話事件) – Manu 2010-02-06 06:31:43
@Manu:讓我編輯這個答案,包括如何檢索事件的樣本.... – t0mm13b 2010-02-06 18:13:54
哇,interestin。我沒想到它變得如此複雜。我不是專家,當然不是註冊管理機構。所以我有點害怕觸及那個。 「應用程序空閒」已經是一個好主意。 – Manu 2010-02-07 17:14:48