国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

To CNET:全局熱鍵的例子,不知道有沒有用

2024-07-21 02:27:53
字體:
供稿:網(wǎng)友
類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");
  }
}
}



商業(yè)源碼熱門下載www.html.org.cn

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 万盛区| 金华市| 敦煌市| 无为县| 普陀区| 静海县| 高雄市| 雅江县| 沾化县| 望谟县| 汝阳县| 麟游县| 泊头市| 潼关县| 彰化县| 河曲县| 铁岭市| 绥化市| 日土县| 巩义市| 大英县| 洮南市| 平泉县| 巨鹿县| 故城县| 宜良县| 庆元县| 酒泉市| 黑山县| 威海市| 武穴市| 江西省| 新建县| 多伦县| 紫阳县| 黄陵县| 和林格尔县| 砚山县| 军事| 日照市| 昂仁县|