111,129
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Threading;
using System.Management;
using System.Runtime.InteropServices;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[DllImport("user32.dll")]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, KeyModifiers fsModifiers, Keys vk);
[DllImport("user32.dll")]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
private const int WM_HOTKEY = 0x0312;
[Flags()]
private enum KeyModifiers
{
None = 0,
Alt = 1,
Control = 2,
Shift = 4,
Windows = 8
}
private void Form1_Load(object sender, EventArgs e)
{
RegisterHotKey(this.Handle, 1001,KeyModifiers.Control, Keys.S);
RegisterHotKey(this.Handle, 1002, KeyModifiers.Control, Keys.C);
}
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_HOTKEY)
{
if (m.WParam.ToInt32() == 1001)
{
MessageBox.Show("显示窗体");
}
else if (m.WParam.ToInt32() == 1002)
{
MessageBox.Show("关闭窗体");
}
return;
}
base.WndProc(ref m);
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
UnregisterHotKey(this.Handle, 1001);
UnregisterHotKey(this.Handle, 1002);
}
}
}