4

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?

Otiel
  • 18,404
  • 16
  • 78
  • 126
bobble14988
  • 1,749
  • 5
  • 26
  • 38
  • possible duplicate of [Finding the handle to a WPF window](http://stackoverflow.com/questions/1556182/finding-the-handle-to-a-wpf-window) – Hans Passant Oct 30 '11 at 18:57

3 Answers3

5

To get the Handle for a WPF Window use:

  new WindowInteropHelper(YourWPFWindow).Handle

MSDN reference

Yahia
  • 69,653
  • 9
  • 115
  • 144
3

Write new WindowInteropHelper(someWindow).Handle

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
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.

CShark
  • 1,413
  • 15
  • 25