您的位置:首页 > 编程语言 > C#

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}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐