Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Linq;
Using System.Text;
Using System.Windows.Forms;
Using System.Runtime.InteropServices;
Namespace Openmonitor
{
public partial class Form1:form
{
Public Form1 ()
{
InitializeComponent ();
Hotkey.registerhotkey (this. Handle, 0, keys.f4);
Hotkey.registerhotkey (this. HANDLE,101,0,KEYS.F5);
}
Class HotKey
{
If the function executes successfully, the return value is not 0.
If the function fails to execute, the return value is 0. To get the extended error message, call GetLastError.
[DllImport ("user32.dll", SetLastError = True)]
public static extern bool RegisterHotKey (
IntPtr hWnd,//Handle to the window to define the hotkey
int ID,//define Hotkey ID (cannot be duplicated with other ID)
Keymodifiers fsmodifiers,//identifies if the hotkey will take effect when pressing ALT, Ctrl, Shift, Windows, and other keys
Keys VK//define the contents of the hotkey
);
[DllImport ("user32.dll", SetLastError = True)]
public static extern bool Unregisterhotkey (
IntPtr hWnd,//Handle to the window to cancel the hotkey
int ID//ID of the hotkey to cancel
);
Defines the name of the secondary key (converts the number to characters for easy memory, or removes the enumeration and uses the value directly)
[Flags ()]
public enum Keymodifiers
{
None = 0,
Alt = 1,
Ctrl = 2,
Shift = 4,
WindowsKey = 8
}
}
protected override void WndProc (ref Message m)
{
const int wm_hotkey = 0x0312;
Press the shortcut key
Switch (m.msg)
{
Case Wm_hotkey:
Switch (M.wparam.toint32 ())
{
Case 100:
Monitorhelper.turnon ();
Break
Case 101:
Monitorhelper.turnoff ();
Break
}
Break
}
Base. WndProc (ref m);
}
Class Monitorhelper
{
public static void TurnOn ()
{
SendMessage (Hwnd_broadcast, Wm_syscommand, Sc_monitorpower,-1);
}
public static void Turnoff ()
{
SendMessage (Hwnd_broadcast, Wm_syscommand, Sc_monitorpower, 2);
}
[DllImport ("User32.dll")]
public static extern int SendMessage (IntPtr hWnd, uint Msg, int wParam, int lParam);
private static readonly IntPtr hwnd_broadcast = new IntPtr (0XFFFF);
Private Const UINT WM_SYSCOMMAND = 0x0112;
Private Const int sc_monitorpower = 0xf170;
}
}
}
C # Control the display switch with hotkeys