using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication2
{
public enum KeyModifiers //组合键枚举
{
None = 0,
Alt = 1,
Control = 2,
Shift = 4,
Windows = 8
}
public partial class Form1 : Form
{
[System.Runtime.InteropServices.DllImport("user32.dll")] //申明API函数
public static extern bool RegisterHotKey(
IntPtr hWnd, // handle to window
int id, // hot key identifier
uint fsModifiers, // key-modifier options
Keys vk // virtual-key code
);
[System.Runtime.InteropServices.DllImport("user32.dll")] //申明API函数
public static extern bool UnregisterHotKey(
IntPtr hWnd, // handle to window
int id // hot key identifier
);
public Form1()
{
InitializeComponent();
}
private void ProcessHotkey(Message m) //按下设定的键时调用该函数
{
string typeStr = "";
IntPtr id = m.WParam; //IntPtr用于表示指针或句柄的平台特定类型
//MessageBox.Show(id.ToString());
string sid = id.ToString();
switch (sid)
{
case "101":
SendKeys.SendWait("{F6}" + "^C");
typeStr = System.Windows.Forms.Clipboard.GetText();
getUrlType(typeStr);
break;
}
}
private void getUrlType(string type)
{
type.Split('.');
string type1 = type.Substring(type.IndexOf('.') + 1);
string typeStr = type1.Substring(0,type1.IndexOf('.'));
this.button1.Text = typeStr;
}
private void Form1_Load(object sender, EventArgs e)
{
RegisterHotKey(Handle, 101, 0, Keys.F9);
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e) //退出程序时缷载热键
{
UnregisterHotKey(Handle, 100);//卸载第1个快捷键
UnregisterHotKey(Handle, 200); //缷载第2个快捷键
}
//重写WndProc()方法,通过监视系统消息,来调用过程
protected override void WndProc(ref Message m)//监视Windows消息
{
const int WM_HOTKEY = 0x0312;//如果m.Msg的值为0x0312那么表示用户按下了热键
switch (m.Msg)
{
case WM_HOTKEY:
ProcessHotkey(m);//按下热键时调用ProcessHotkey()函数
break;
}
base.WndProc(ref m); //将系统消息传递自父类的WndProc
}
}
}