C# 快捷键/hotkey简单例子
生活随笔
收集整理的這篇文章主要介紹了
C# 快捷键/hotkey简单例子
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1.導(dǎo)入dll
[System.Runtime.InteropServices.DllImport("user32.dll")] //申明API函數(shù)public static extern bool RegisterHotKey(IntPtr hWnd, // handle to windowint id, // hot key identifieruint fsModifiers, // key-modifier optionsKeys vk // virtual-key code);[System.Runtime.InteropServices.DllImport("user32.dll")] //申明API函數(shù)public static extern bool UnregisterHotKey(IntPtr hWnd, // handle to windowint id // hot key identifier);2.定義hotkey
RegisterHotKey(Handle, 100, 0, Keys.Home); //注冊(cè)熱鍵為home//關(guān)閉程序前,需要卸載熱鍵 UnregisterHotKey(Handle, 100);//卸載快捷鍵?
3.重載WndProc函數(shù)
protected override void WndProc(ref Message m) // 重載WndProc函數(shù){const int WM_HOTKEY = 0x0312; //判斷hotkey標(biāo)志try{if (m.Msg != WM_HOTKEY || m.WParam.ToInt32() != 100){goto EndFunction;}//獲取當(dāng)前窗口信息IntPtr hWnd = GetForegroundWindow();uint procId = 0;GetWindowThreadProcessId(hWnd, out procId);var proc = Process.GetProcessById((int)procId);string titleName = proc.MainWindowTitle; int fileLen = titleName.LastIndexOf('-');string fileFullPath = titleName.Substring(0,fileLen).Trim();this.textBoxFileNme.Text = fileFullPath;//執(zhí)行函數(shù)......}catch (Exception ex){this.textBoxLog.AppendText("Error!!!"+ex.Message);}EndFunction:base.WndProc(ref m);}?
?
?
總結(jié)
以上是生活随笔為你收集整理的C# 快捷键/hotkey简单例子的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: C# 对程序窗口进程和进程ID
- 下一篇: powershell XML操作