Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

wpf

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?

like image 666
bobble14988 Avatar asked Dec 22 '22 07:12

bobble14988


2 Answers

To get the Handle for a WPF Window use:

  new WindowInteropHelper(YourWPFWindow).Handle

MSDN reference

like image 120
Yahia Avatar answered Feb 16 '23 00:02

Yahia


Write new WindowInteropHelper(someWindow).Handle

like image 22
SLaks Avatar answered Feb 16 '23 00:02

SLaks