首页 > 开发 > 综合 > 正文

To CNET:全局热键的例子,不知道有没有用

2020-02-03 13:37:45
字体:
来源:转载
供稿:网友
类hotkey.cs
using system;
namespace durius.generics
{
public delegate void hotkeyeventhandler(int hotkeyid);
/// <summary>
/// system wide hotkey wrapper.
///  
/// robert jeppesen
/// send bugs to [email protected]
/// </summary>
public class hotkey : system.windows.forms.imessagefilter
{
  system.collections.hashtable keyids = new system.collections.hashtable();
  intptr hwnd;
  /// <summary>
  /// occurs when a hotkey has been pressed.
  /// </summary>
  public event hotkeyeventhandler onhotkey;
  public enum keyflags
  {
   mod_alt = 0x1,
   mod_control = 0x2,
   mod_shift = 0x4,
   mod_win = 0x8
  }
  [system.runtime.interopservices.dllimport("user32.dll")]
  public static extern uint32 registerhotkey( intptr hwnd, uint32 id,
   uint32 fsmodifiers, uint32 vk);
  [system.runtime.interopservices.dllimport("user32.dll")]
  public static extern uint32 unregisterhotkey( intptr hwnd, uint32 id);
  [system.runtime.interopservices.dllimport("kernel32.dll")]
  public static extern uint32 globaladdatom( string lpstring );
  [system.runtime.interopservices.dllimport("kernel32.dll")]
  public static extern uint32 globaldeleteatom( uint32 natom );
  /// <summary>
  /// constructor.  adds this instance to the messagefilters so that this class can raise hotkey events
  /// </summary>
  /// <param name="hwnd">a valid hwnd, i.e. form1.handle</param>
  public hotkey(intptr hwnd)
  {
   this.hwnd = hwnd;
   system.windows.forms.application.addmessagefilter(this);
  }
  /// <summary>
  /// register a system wide hotkey.
  /// </summary>
  /// <param name="hwnd">form1.handle</param>
  /// <param name="key">your hotkey</param>
  /// <returns>id integer for your hotkey. use this to know which hotkey was pressed.</returns>
  public int registerhotkey(system.windows.forms.keys key, keyflags keyflags)
  {
    uint32 hotkeyid = globaladdatom(system.guid.newguid().tostring());
    registerhotkey( (intptr)hwnd, hotkeyid, (uint32)keyflags, (uint32)key);
    keyids.add(hotkeyid, hotkeyid);
    return (int)hotkeyid;
  }
  /// <summary>
  /// unregister hotkeys and delete atoms.
  /// </summary>
  public void unregisterhotkeys()
  {
   system.windows.forms.application.removemessagefilter(this);
   foreach (uint32 key in keyids.values)
   {
    unregisterhotkey(hwnd, key);
    globaldeleteatom(key);
   }
  }
  public bool prefiltermessage(ref system.windows.forms.message m)
  {
   if (m.msg == 0x312) /*wm_hotkey*/
   {
    if(onhotkey != null)
    {
     foreach (uint32 key in keyids.values)
     {
      if((uint32)m.wparam == key)
      {
       onhotkey((int)m.wparam);
       return true;
      }
     }
    }
   }
   return false;
  }
}
}

测试程序form1.cs
using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.data;
using durius.generics;
namespace testgenerics
{
/// <summary>
/// summary description for form1.
/// </summary>
public class form1 : system.windows.forms.form
{
  hotkey hotkey;
  int hotkey1;
  int hotkey2;
  private system.windows.forms.label label1;
  private system.windows.forms.label label2;
  private system.windows.forms.label label3;
  private system.windows.forms.button button1;
  /// <summary>
  /// required designer variable.
  /// </summary>
  private system.componentmodel.container components = null;
  public form1()
  {
   //
   // required for windows form designer support
   //
   initializecomponent();
   /*
    * initialize our hotkeys. pass in a handle to the main form in the constructor,
    *  then call registerhotkey for each of our hotkey combinations and wire up
    * the event
    */
   hotkey = new hotkey(this.handle);
   hotkey1 = hotkey.registerhotkey(system.windows.forms.keys.d1, hotkey.keyflags.mod_win);
   hotkey2 = hotkey.registerhotkey(system.windows.forms.keys.d2, hotkey.keyflags.mod_control);
   hotkey.onhotkey += new hotkeyeventhandler(onhotkey);
  }
  /// <summary>
  /// the hotkey event handler. if you have several hotkeys, you will have to check
  /// which one was pressed using hotkeyid.
  /// registerhotkey returns the hotkeyid that was assigned to your hotkey.
  /// </summary>
  /// <param name="hotkeyid"></param>
  public void onhotkey(int hotkeyid)
  {
   this.activate();
   if(hotkeyid == hotkey1)
   {
    messagebox.show("win+1 pressed.");
   }
   else if(hotkeyid == hotkey2)
   {
    messagebox.show("ctrl+2 pressed.");
   }
  }
  /// <summary>
  /// clean up any resources being used.
  /// </summary>
  protected override void dispose( bool disposing )
  {
   if( disposing )
   {
    hotkey.unregisterhotkeys();
    if (components != null)
    {
     components.dispose();
    }
   }
   base.dispose( disposing );
  }
  #region windows form designer generated code
  /// <summary>
  /// required method for designer support - do not modify
  /// the contents of this method with the code editor.
  /// </summary>
  private void initializecomponent()
  {
   this.label1 = new system.windows.forms.label();
   this.label2 = new system.windows.forms.label();
   this.label3 = new system.windows.forms.label();
   this.button1 = new system.windows.forms.button();
   this.suspendlayout();
   //
   // label1
   //
   this.label1.location = new system.drawing.point(16, 8);
   this.label1.name = "label1";
   this.label1.size = new system.drawing.size(200, 16);
   this.label1.tabindex = 0;
   this.label1.text = "hotkeys:";
   //
   // label2
   //
   this.label2.location = new system.drawing.point(16, 32);
   this.label2.name = "label2";
   this.label2.size = new system.drawing.size(208, 16);
   this.label2.tabindex = 1;
   this.label2.text = "win + 1";
   //
   // label3
   //
   this.label3.location = new system.drawing.point(16, 56);
   this.label3.name = "label3";
   this.label3.size = new system.drawing.size(208, 16);
   this.label3.tabindex = 2;
   this.label3.text = "ctrl + 2";
   //
   // button1
   //
   this.button1.flatstyle = system.windows.forms.flatstyle.flat;
   this.button1.location = new system.drawing.point(88, 80);
   this.button1.name = "button1";
   this.button1.tabindex = 3;
   this.button1.text = "about";
   this.button1.click += new system.eventhandler(this.button1_click);
   //
   // form1
   //
   this.autoscalebasesize = new system.drawing.size(5, 13);
   this.clientsize = new system.drawing.size(168, 123);
   this.controls.addrange(new system.windows.forms.control[] {
                    this.button1,
                    this.label3,
                    this.label2,
                    this.label1});
   this.name = "form1";
   this.text = "hotkey";
   this.resumelayout(false);
  }
  #endregion
  /// <summary>
  /// the main entry point for the application.
  /// </summary>
  [stathread]
  static void main()
  {
   application.run(new form1());
  }
  private void button1_click(object sender, system.eventargs e)
  {
   messagebox.show(this, @"system wide hotkey wrapper
- robert jeppesen
http://www.durius.com", "hotkey sample");
  }
}
}



商业源码热门下载www.html.org.cn

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表