C# 训练场(四)创建系统热键,并向活动窗口输入信息
2012-05-12 23:04
274 查看
本文转载 http://blog.csdn.net/imbiz/article/details/5648682
练习使用API。
实现以下功能:
1. 设定热键
2. 检测当前活动窗口
3. 向活动窗口发送信息
4. 取消注册热键
[c-sharp] view plaincopy
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;
using System.Diagnostics;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
[System.Runtime.InteropServices.DllImport("user32.dll")]
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
);
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr GetForegroundWindow();
public enum KeyModifiers
{
None = 0,
Alt = 1,
Control = 2,
Shift = 4,
Windows = 8
}
public Form1()
{
InitializeComponent();
RegisterHotKey(Handle, 150, 4, Keys.A);// 热键一:Shift+A
RegisterHotKey(Handle, 151, 2|4, Keys.M);//热键一:Ctrl +Shift+M
// RegisterHotKey(Handle, 152, 1|2, Keys.Up);// 热键一:Ctrl +Alt+光标上箭头
// RegisterHotKey(Handle, 153, 8, Keys.P);// 热键一:Win+P
}
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
}
private void ProcessHotkey(Message m)
{
IntPtr id = m.WParam; //IntPtr用于表示指针或句柄的平台特定类型
//MessageBox.Show(id.ToString());
string sid = id.ToString();
switch (sid)
{
case "150": opennewtxt(); break;// 热键一:Ctrl+A
case "151": sendmsg(); break;//热键一:Ctrl +Shift+M
case "152"://热键一:Ctrl +Alt+光标上箭头
this.Visible = true;
break;
case "153"://热键一:Win+P
this.Visible = false;
break;
}
}
private void opennewtxt()
{
Process mytxt = new Process();
mytxt.StartInfo.FileName = @"notepad.exe";
mytxt.StartInfo.Arguments = @"D://newtxt.txt";
mytxt.Start();
mytxt.WaitForInputIdle(1000);
SendKeys.SendWait("Hello!");
}
private void sendmsg()
{
//string proname="";
Process mypro=null;
foreach (Process thisproc in Process.GetProcesses())
{
if (thisproc.MainWindowHandle.ToInt32() == GetForegroundWindow().ToInt32())
{
//proname = thisproc.ProcessName;
mypro = thisproc;
break;
}
}
try
{
//两种方法
//string msginfo=@"{ENTER}/sum_{{}i=0{}}{^}{{}/infty{}}{ENTER}";
SendKeys.Send(@"{ENTER}/begin{{}split{}}{ENTER} &= {ENTER} &= {ENTER}/end{{}split{}}{ENTER}");
//SendKeys.Send(msginfo);
//SendKeys.Send(@"/begin{{}split{}}");
//SendKeys.Send("{ENTER}");
//SendKeys.Send(@"{(}{)}");
//SendKeys.Send("{ENTER}");
//SendKeys.Send(@"/sum_0{^}{{}/infinite{}}");
//SendKeys.Send("{ENTER}");
//SendKeys.Send(@"/end{{}split{}} haha");
//SendKeys.Send("{ENTER}");
//测试使用粘贴板
string msginfo2 = @"/begin{split}"+"/r/n"+@" &= "+"/r/n"+@"/end{split}";
Clipboard.SetDataObject(msginfo2);
//SendKeys.Send("^v");
}
catch (System.NullReferenceException e)
{
// MessageBox.Show(e.Message);
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
UnregisterHotKey(Handle, 150);//卸载第1个快捷键
UnregisterHotKey(Handle, 151); //缷载第2个快捷键
UnregisterHotKey(Handle, 152);//卸载第3个快捷键
UnregisterHotKey(Handle, 153); //缷载第4个快捷键
}
}
}
运行结果:
在WORD编辑窗口,按下Ctrl+Shift+M,会在当前光标处输入以下文字:
/begin{/split}
&=
&=
/end{/split}
练习使用API。
实现以下功能:
1. 设定热键
2. 检测当前活动窗口
3. 向活动窗口发送信息
4. 取消注册热键
[c-sharp] view plaincopy
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;
using System.Diagnostics;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
[System.Runtime.InteropServices.DllImport("user32.dll")]
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
);
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr GetForegroundWindow();
public enum KeyModifiers
{
None = 0,
Alt = 1,
Control = 2,
Shift = 4,
Windows = 8
}
public Form1()
{
InitializeComponent();
RegisterHotKey(Handle, 150, 4, Keys.A);// 热键一:Shift+A
RegisterHotKey(Handle, 151, 2|4, Keys.M);//热键一:Ctrl +Shift+M
// RegisterHotKey(Handle, 152, 1|2, Keys.Up);// 热键一:Ctrl +Alt+光标上箭头
// RegisterHotKey(Handle, 153, 8, Keys.P);// 热键一:Win+P
}
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
}
private void ProcessHotkey(Message m)
{
IntPtr id = m.WParam; //IntPtr用于表示指针或句柄的平台特定类型
//MessageBox.Show(id.ToString());
string sid = id.ToString();
switch (sid)
{
case "150": opennewtxt(); break;// 热键一:Ctrl+A
case "151": sendmsg(); break;//热键一:Ctrl +Shift+M
case "152"://热键一:Ctrl +Alt+光标上箭头
this.Visible = true;
break;
case "153"://热键一:Win+P
this.Visible = false;
break;
}
}
private void opennewtxt()
{
Process mytxt = new Process();
mytxt.StartInfo.FileName = @"notepad.exe";
mytxt.StartInfo.Arguments = @"D://newtxt.txt";
mytxt.Start();
mytxt.WaitForInputIdle(1000);
SendKeys.SendWait("Hello!");
}
private void sendmsg()
{
//string proname="";
Process mypro=null;
foreach (Process thisproc in Process.GetProcesses())
{
if (thisproc.MainWindowHandle.ToInt32() == GetForegroundWindow().ToInt32())
{
//proname = thisproc.ProcessName;
mypro = thisproc;
break;
}
}
try
{
//两种方法
//string msginfo=@"{ENTER}/sum_{{}i=0{}}{^}{{}/infty{}}{ENTER}";
SendKeys.Send(@"{ENTER}/begin{{}split{}}{ENTER} &= {ENTER} &= {ENTER}/end{{}split{}}{ENTER}");
//SendKeys.Send(msginfo);
//SendKeys.Send(@"/begin{{}split{}}");
//SendKeys.Send("{ENTER}");
//SendKeys.Send(@"{(}{)}");
//SendKeys.Send("{ENTER}");
//SendKeys.Send(@"/sum_0{^}{{}/infinite{}}");
//SendKeys.Send("{ENTER}");
//SendKeys.Send(@"/end{{}split{}} haha");
//SendKeys.Send("{ENTER}");
//测试使用粘贴板
string msginfo2 = @"/begin{split}"+"/r/n"+@" &= "+"/r/n"+@"/end{split}";
Clipboard.SetDataObject(msginfo2);
//SendKeys.Send("^v");
}
catch (System.NullReferenceException e)
{
// MessageBox.Show(e.Message);
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
UnregisterHotKey(Handle, 150);//卸载第1个快捷键
UnregisterHotKey(Handle, 151); //缷载第2个快捷键
UnregisterHotKey(Handle, 152);//卸载第3个快捷键
UnregisterHotKey(Handle, 153); //缷载第4个快捷键
}
}
}
运行结果:
在WORD编辑窗口,按下Ctrl+Shift+M,会在当前光标处输入以下文字:
/begin{/split}
&=
&=
/end{/split}
相关文章推荐
- C# 训练场(四)创建系统热键,并向活动窗口输入信息
- C#学习笔记之——学生信息输入系统(Dictionary)
- C#基础-033 创建一个学员类,并设计三个字段用于表示学生的成绩(语文、数学、英语);然后定义一个数组表示一个班的学生(10人),依次输入每个学生的信息和成绩,输入的同时将学员的每科成绩划分等级
- C#软件开发实例.私人订制自己的屏幕截图工具(二)创建项目、注册热键、显示截图主窗口
- C#软件开发实例.私人订制自己的屏幕截图工具(二)创建项目、注册热键、显示截图主窗口
- C# 注册窗口热键(Winform&WPF)和系统热键
- C# 创建系统服务并定时执行
- C#实现窗口最小化到系统托盘
- C#创建并连接学生数据库(SqlServer数据库)、创建学生信息表、向表中插入学生信息
- c# 活动窗口 键盘监听
- 创建百度地图,设置地图缩放,设置标注,添加信息窗口
- C#读取IE窗口信息
- 取系统当前活动窗口标题
- C#程序设计之学生信息管理系统
- C#获取当前计算机的系统信息
- js中的3种弹出式消息提醒(警告窗口,确认窗口,信息输入窗口)的命令是什么?
- 适合初学者看的学生信息系统,C#小白编写
- C# 用Linq的方式实现对Xml文件的基本操作(创建xml文件、增删改查xml文件节点信息)zz
- C# 实现对XML文件的基本操作(创建xml文件,增、删、改、查 xml节点信息)
- C#实现快捷键(系统热键)响应