简体   繁体   中英

How can I put the monitor to sleep when using C#/WPF (Not WinForms!)

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.

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

To get the Handle for a WPF Window use:

  new WindowInteropHelper(YourWPFWindow).Handle

MSDN reference

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM