bobble14988
bobble14988

Reputation: 1756

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?

Upvotes: 4

Views: 714

Answers (3)

Yahia
Yahia

Reputation: 70369

To get the Handle for a WPF Window use:

  new WindowInteropHelper(YourWPFWindow).Handle

MSDN reference

Upvotes: 5

CShark
CShark

Reputation: 1563

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.

Upvotes: 3

SLaks
SLaks

Reputation: 887453

Write new WindowInteropHelper(someWindow).Handle

Upvotes: 3

Related Questions