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

首頁 > 開發 > 綜合 > 正文

c#中設置快捷鍵

2024-07-21 02:26:39
字體:
來源:轉載
供稿:網友
  • 本文來源于網頁設計愛好者web開發社區http://www.html.org.cn收集整理,歡迎訪問。
  • 要設置快捷鍵必須使用user32.dll下面的兩個方法。

    bool registerhotkey(
     hwnd hwnd,
     int id,
     uint fsmodifiers,
     uint vk
    );

      和

    bool unregisterhotkey(
     hwnd hwnd,
     int id
    );
    轉換成c#代碼,那么首先就要引用命名空間system.runtime.interopservices;來加載非托管類user32.dll。于是有了:

    [dllimport("user32.dll", setlasterror=true)]
    public static extern bool registerhotkey(
     intptr hwnd, // handle to window
     int id, // hot key identifier
     keymodifiers fsmodifiers, // key-modifier options
     keys vk // virtual-key code
    );

    [dllimport("user32.dll", setlasterror=true)]
    public static extern bool unregisterhotkey(
     intptr hwnd, // handle to window
     int id // hot key identifier
    );


    [flags()]
    public enum keymodifiers
    {
     none = 0,
     alt = 1,
     control = 2,
     shift = 4,
     windows = 8
    }

      這是注冊和卸載全局快捷鍵的方法,那么我們只需要在form_load的時候加上注冊快捷鍵的語句,在formclosing的時候卸載全局快捷鍵。同時,為了保證剪貼板的內容不受到其他程序調用剪貼板的干擾,在form_load的時候,我先將剪貼板里面的內容清空。

      于是有了:

    private void form1_load(object sender, system.eventargs e)
    {
     label2.autosize = true;

     clipboard.clear();//先清空剪貼板防止剪貼板里面先復制了其他內容
     registerhotkey(handle, 100, 0, keys.f10);
    }

    private void form1_formclosing(object sender, formclosingeventargs e)
    {
     unregisterhotkey(handle, 100);//卸載快捷鍵
    }

      那么我們在別的窗口,怎么讓按了快捷鍵以后調用我的主過程processhotkey()呢?

      那么我們就必須重寫wndproc()方法,通過監視系統消息,來調用過程:

    protected override void wndproc(ref message m)//監視windows消息
    {
     const int wm_hotkey = 0x0312;//按快捷鍵
     switch (m.msg)
     {
      case wm_hotkey:
       processhotkey();//調用主處理程序
       break;
     }
     base.wndproc(ref m);
    }


    發表評論 共有條評論
    用戶名: 密碼:
    驗證碼: 匿名發表
    主站蜘蛛池模板: 怀安县| 宜丰县| 容城县| 美姑县| 镇江市| 克山县| 陇南市| 仁寿县| 台南县| 龙泉市| 新密市| 五华县| 贞丰县| 南部县| 恩平市| 新宁县| 天全县| 永顺县| 蕲春县| 广安市| 洮南市| 瑞丽市| 溆浦县| 横山县| 许昌县| 温州市| 新源县| 饶平县| 湟源县| 青岛市| 彭山县| 巴塘县| 米脂县| 丰都县| 南川市| 昆明市| 新巴尔虎右旗| 大丰市| 泰来县| 吉木萨尔县| 肃北|