C# Winform 글로벌 단축키 추가(보스 키)
10016 단어 WinForm
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace drmaple
{
class HotKey
{
// , 0。
// , 0。 , GetLastError。
[DllImport("user32.dll", SetLastError = true)]
public static extern bool RegisterHotKey(
IntPtr hWnd, //
int id, // ID( ID )
KeyModifiers fsModifiers, // Alt、Ctrl、Shift、Windows
Keys vk //
);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool UnregisterHotKey(
IntPtr hWnd, //
int id // ID
);
// ( , )
[Flags()]
public enum KeyModifiers
{
None = 0,
Alt = 1,
Ctrl = 2,
Shift = 4,
WindowsKey = 8
}
}
}
// :
//“public static extern bool RegisterHotKey()” 。 user32.dll ,
//user32.dll , , “DllImport” 。
//“[DllImport("user32.dll", SetLastError = true)]” 。
//“public static extern bool UnregisterHotKey()” , DllImport user32.dll 。
//“public enum KeyModifiers{}” , , 。
// 。
//(2) FormA , HotKey
// FormA Activate , Shift+S,Ctrl+Z,Alt+D 。 Id , 。
private void Form_Activated(object sender, EventArgs e)
{
// Shift+S,Id 100。HotKey.KeyModifiers.Shift 4 。
HotKey.RegisterHotKey(Handle, 100, HotKey.KeyModifiers.Shift, Keys.S);
// Ctrl+B,Id 101。HotKey.KeyModifiers.Ctrl 2 。
HotKey.RegisterHotKey(Handle, 101, HotKey.KeyModifiers.Ctrl, Keys.B);
// Alt+D,Id 102。HotKey.KeyModifiers.Alt 1 。
HotKey.RegisterHotKey(Handle, 102, HotKey.KeyModifiers.Alt, Keys.D);
}
// FormA Leave 。
private void FrmSale_Leave(object sender, EventArgs e)
{
// Id 100
HotKey.UnregisterHotKey(Handle, 100);
// Id 101
HotKey.UnregisterHotKey(Handle, 101);// http://ike.126.com
// Id 102
HotKey.UnregisterHotKey(Handle, 102);
}
FromA WndProc
///
/// Windows
/// WndProc ,
///
///
protected override void WndProc(ref Message m)
{
const int WM_HOTKEY = 0x0312;
//
switch (m.Msg)
{
case WM_HOTKEY:
switch (m.WParam.ToInt32())
{
case 100: // Shift+S
//
break;
case 101: // Ctrl+B
//
break;
case 102: // Alt+D
//
break;
}
break;
}
base.WndProc(ref m);
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
C# Excel 템플릿 내보내기(Word 등)프로젝트에서 Excel, Word, pdf 등 템플릿 내보내기를 자주 사용해야 합니다. 다음은 자신이 정리한 간단한 버전의 템플릿 내보내기입니다. 복잡한 내보내기와 유사합니다. 조금만 수정하면 됩니다.다음 내보내기 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.