标签:
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 CSharp_Message
{
[Flags()]
public enum KeyModifiers
{
None=0,
Alt=1,
Ctrl=2,
Shift=4,
WindowKey=5
}
public partial class Form3 : Form
{
[DllImport("user32.dll", SetLastError = true)]
public static extern bool RegistryHotKey(IntPtr hWnd, int id, KeyModifiers fsModifiers, Keys vk);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
public Form3()
{
InitializeComponent();
RegistryHotKey(this.Handle, 100, KeyModifiers.None, Keys.Escape);
RegistryHotKey(this.Handle, 101, KeyModifiers.Ctrl, Keys.F);
RegistryHotKey(this.Handle, 102, KeyModifiers.Ctrl, Keys.A);
}
protected override void WndProc(ref Message m)
{
try
{
const int WM_HOTKEY = 0x0312;
switch (m.Msg)
{
case WM_HOTKEY:
switch(m.WParam.ToInt32())
{
case 101:
//...
break;
case 100:
//...
break;
}
break;
case 104:
break;
}
}
catch (System.Exception ex)
{
Console.WriteLine(ex.Message);
}
base.WndProc(ref m);
}
private void Form3_FormClosed(object sender, FormClosedEventArgs e)
{
UnregisterHotKey(this.Handle, 100);
UnregisterHotKey(this.Handle, 101);
UnregisterHotKey(this.Handle, 102);
}
}
}
一种比较少见的C#代码段
标签:
原文地址:http://www.cnblogs.com/QQ122252656/p/4293706.html