使用C#/ WPF时,如何让显示器进入睡眠状态(非WinForms!)

时间:2022-05-11 01:38:56

The trouble I'm having is with using...

我遇到的麻烦就是使用......

[DllImport("user32")]
public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

...and then...

SendMessage(???, WM_SYSCOMMAND, (IntPtr)SC_MONITORPOWER, (IntPtr)MONITOR_OFF);

SendMessage wants the Form's Handle but I'm not using Forms so cannot get the Handle.

SendMessage想要Form的Handle,但我没有使用Forms,所以无法获得Handle。

Is there any other way I can put the monitor to sleep OR get the Handle in WPF?

有没有其他方法可以让显示器进入睡眠状态或获取WPF中的句柄?

3 个解决方案

#1


5  

To get the Handle for a WPF Window use:

要获取WPF窗口的句柄,请使用:

  new WindowInteropHelper(YourWPFWindow).Handle

MSDN reference

#2


3  

Write new WindowInteropHelper(someWindow).Handle

编写新的WindowInteropHelper(someWindow).Handle

#3


3  

Or use

HwndSource source = (HwndSource)HwndSource.FromVisual(this);
source.Handle...

to get a handle from a single element in the form, also runs for the whole window.

从表单中的单个元素获取句柄,也运行整个窗口。

#1


5  

To get the Handle for a WPF Window use:

要获取WPF窗口的句柄,请使用:

  new WindowInteropHelper(YourWPFWindow).Handle

MSDN reference

#2


3  

Write new WindowInteropHelper(someWindow).Handle

编写新的WindowInteropHelper(someWindow).Handle

#3


3  

Or use

HwndSource source = (HwndSource)HwndSource.FromVisual(this);
source.Handle...

to get a handle from a single element in the form, also runs for the whole window.

从表单中的单个元素获取句柄,也运行整个窗口。