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
  • 17,975
  • 14
  • 74
  • 124
bobble14988
  • 1,741
  • 5
  • 26
  • 37
  • 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
  • 68,257
  • 8
  • 107
  • 138
3

Write new WindowInteropHelper(someWindow).Handle

SLaks
  • 837,282
  • 173
  • 1,862
  • 1,933
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,238
  • 15
  • 23