zoukankan      html  css  js  c++  java
  • C# 训练场(四)创建系统热键,并向活动窗口输入信息

     本文转载  http://blog.csdn.net/imbiz/article/details/5648682
     
     

    练习使用API。

    实现以下功能:

    1. 设定热键

    2. 检测当前活动窗口

    3. 向活动窗口发送信息

    4. 取消注册热键

    [c-sharp] view plaincopy
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.ComponentModel;  
    4. using System.Data;  
    5. using System.Drawing;  
    6. using System.Linq;  
    7. using System.Text;  
    8. using System.Windows.Forms;  
    9. using System.Runtime.InteropServices;  
    10. using System.Diagnostics;  
    11.   
    12.   
    13. namespace WindowsFormsApplication1  
    14. {  
    15.     public partial class Form1 : Form  
    16.     {  
    17.   
    18.         [System.Runtime.InteropServices.DllImport("user32.dll")]  
    19.         public static extern bool RegisterHotKey(  
    20.             IntPtr hWnd, // handle to window            
    21.             int id, // hot key identifier            
    22.             uint fsModifiers, // key-modifier options           
    23.             Keys vk // virtual-key code              
    24.             );  
    25.   
    26.         [System.Runtime.InteropServices.DllImport("user32.dll")] //申明API函数          
    27.         public static extern bool UnregisterHotKey(  
    28.         IntPtr hWnd, // handle to window            
    29.         int id // hot key identifier              
    30.         );  
    31.   
    32.   
    33.         [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]  
    34.         public static extern IntPtr GetForegroundWindow();  
    35.   
    36.   
    37.         public enum KeyModifiers  
    38.         {  
    39.             None = 0,  
    40.             Alt = 1,  
    41.             Control = 2,  
    42.             Shift = 4,  
    43.             Windows = 8  
    44.         }  
    45.   
    46.   
    47.         public Form1()  
    48.         {  
    49.             InitializeComponent();  
    50.             RegisterHotKey(Handle, 150, 4, Keys.A);// 热键一:Shift+A  
    51.             RegisterHotKey(Handle, 151, 2|4, Keys.M);//热键一:Ctrl +Shift+M  
    52.         //    RegisterHotKey(Handle, 152, 1|2, Keys.Up);// 热键一:Ctrl +Alt+光标上箭头  
    53.         //    RegisterHotKey(Handle, 153, 8, Keys.P);// 热键一:Win+P  
    54.               
    55.         }  
    56.             protected override void WndProc(ref Message m)//监视Windows消息             
    57.         {              
    58.             const int WM_HOTKEY = 0x0312;//如果m.Msg的值为0x0312那么表示用户按下了热键              
    59.             switch (m.Msg)                 
    60.             {      
    61.                 case WM_HOTKEY:                         
    62.                     ProcessHotkey(m);//按下热键时调用ProcessHotkey()函数  
    63.                 break;                 
    64.             }              
    65.               
    66.             base.WndProc(ref m); //将系统消息传递自父类的WndProc             
    67.         }  
    68.   
    69.         private void ProcessHotkey(Message m)    
    70.         {  
    71.             IntPtr id = m.WParam; //IntPtr用于表示指针或句柄的平台特定类型             
    72.             //MessageBox.Show(id.ToString());              
    73.             string sid = id.ToString();  
    74.             switch (sid)  
    75.             {  
    76.                 case "150": opennewtxt(); break;//  热键一:Ctrl+A  
    77.                 case "151": sendmsg(); break;//热键一:Ctrl +Shift+M  
    78.                 case "152"://热键一:Ctrl +Alt+光标上箭头  
    79.                     this.Visible = true;  
    80.                     break;  
    81.                 case "153"://热键一:Win+P  
    82.                     this.Visible = false;  
    83.                     break;           
    84.             }          
    85.         }  
    86.   
    87.         private void opennewtxt()  
    88.         {  
    89.             Process mytxt = new Process();  
    90.             mytxt.StartInfo.FileName = @"notepad.exe";  
    91.             mytxt.StartInfo.Arguments = @"D://newtxt.txt";  
    92.             mytxt.Start();  
    93.             mytxt.WaitForInputIdle(1000);  
    94.             SendKeys.SendWait("Hello!");  
    95.   
    96.   
    97.              
    98.         }  
    99.         private void sendmsg()  
    100.         {  
    101.             //string proname="";  
    102.             Process mypro=null;  
    103.             foreach (Process thisproc in Process.GetProcesses())  
    104.             {  
    105.                 if (thisproc.MainWindowHandle.ToInt32() == GetForegroundWindow().ToInt32())  
    106.                 {  
    107.                     //proname = thisproc.ProcessName;  
    108.                     mypro = thisproc;  
    109.                     break;  
    110.                 }  
    111.             }  
    112.             try  
    113.             {  
    114.                 //两种方法  
    115.                 //string msginfo=@"{ENTER}/sum_{{}i=0{}}{^}{{}/infty{}}{ENTER}";  
    116.                 SendKeys.Send(@"{ENTER}/begin{{}split{}}{ENTER} &= {ENTER} &= {ENTER}/end{{}split{}}{ENTER}");  
    117.                 //SendKeys.Send(msginfo);  
    118.                 //SendKeys.Send(@"/begin{{}split{}}");  
    119.                 //SendKeys.Send("{ENTER}");  
    120.                 //SendKeys.Send(@"{(}{)}");  
    121.                 //SendKeys.Send("{ENTER}");  
    122.                 //SendKeys.Send(@"/sum_0{^}{{}/infinite{}}");  
    123.                 //SendKeys.Send("{ENTER}");  
    124.                 //SendKeys.Send(@"/end{{}split{}}   haha");  
    125.                 //SendKeys.Send("{ENTER}");  
    126.                 //测试使用粘贴板  
    127.                 string msginfo2 = @"/begin{split}"+"/r/n"+@" &= "+"/r/n"+@"/end{split}";  
    128.                 Clipboard.SetDataObject(msginfo2);  
    129.                 //SendKeys.Send("^v");  
    130.   
    131.             }  
    132.             catch (System.NullReferenceException e)  
    133.             {  
    134.                // MessageBox.Show(e.Message);  
    135.             }  
    136.         }  
    137.   
    138.         private void Form1_FormClosing(object sender, FormClosingEventArgs e)  
    139.         {  
    140.             UnregisterHotKey(Handle, 150);//卸载第1个快捷键                 
    141.             UnregisterHotKey(Handle, 151); //缷载第2个快捷键  
    142.             UnregisterHotKey(Handle, 152);//卸载第3个快捷键                 
    143.             UnregisterHotKey(Handle, 153); //缷载第4个快捷键  
    144.   
    145.   
    146.         }  
    147.   
    148.   
    149.           
    150.   
    151.           
    152.           
    153.   
    154.   
    155.   
    156.         }  
    157.      
    158. }  

    运行结果:

    在WORD编辑窗口,按下Ctrl+Shift+M,会在当前光标处输入以下文字:

    /begin{/split}

     &=

     &=

    /end{/split}

  • 相关阅读:
    Javascript面向对象编程(三):非构造函数的继承
    zabbix java api
    Hbase架构与原理
    Hbase集群监控
    kafka使用场景
    Java curator操作zookeeper获取kafka
    Docker网络基础:快速指南
    JPA Advanced Mappings(映射)
    9.Spark Streaming
    7.spark共享变量
  • 原文地址:https://www.cnblogs.com/AaronYang/p/2497656.html
Copyright © 2011-2022 走看看