C # Control the display switch with hotkeys

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.