16,556
社区成员
发帖
与我相关
我的任务
分享
'用来实现热键呼出效果的
Public Const WM_HOTKEY = &H312
Public Const MOD_ALT = &H1
Public Const MOD_CONTROL = &H2
Public Const MOD_SHIFT = &H4
Public Const GWL_WNDPROC = (-4)
Public Declare Auto Function RegisterHotKey Lib "user32.dll" Alias _
"RegisterHotKey" (ByVal hwnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As Integer, ByVal vk As Integer) As Boolean
Public Declare Auto Function UnRegisterHotKey Lib "user32.dll" Alias _
"UnregisterHotKey" (ByVal hwnd As IntPtr, ByVal id As Integer) As Boolean
Dim intTimerMax As Integer
Dim TimeOut As Boolean
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'用来实现热键呼出效果的
Dim isResult As Boolean
isResult = RegisterHotKey(Handle, 0, MOD_ALT, 0)
If isResult = False Then
MsgBox("注册热键Ctrl+F失败。", , "零拾")
End If
TimeOut = True
Debug.Write("start ")
End Sub
Protected Overrides Sub WndProc(ByRef m As Message) '用来实现热键呼出效果的
If m.Msg = WM_HOTKEY Then
Debug.Write(m.Msg.ToString & " ")
If TimeOut = True Then
TimeOut = False
intTimerMax = 0
Timer_HotKey.Interval = 1
Timer_HotKey.Start()
Else
If Me.Visible = True Then
Debug.Write("hide ")
'Me.ShowInTaskbar = False
Me.Hide()
'Me.WindowState = FormWindowState.Minimized
Else
Debug.Write("show ")
Me.ShowInTaskbar = True
Me.Show()
Me.WindowState = FormWindowState.Normal
End If
TimeOut = True
End If
End If
MyBase.WndProc(m)
End Sub
Private Sub Timer_HotKey_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer_HotKey.Tick
intTimerMax = intTimerMax + 1
If intTimerMax = 100 Then
Timer_HotKey.Stop()
TimeOut = True
End If
End Sub
'用来实现热键呼出效果的
Public Const WM_HOTKEY = &H312
Public Const MOD_ALT = &H1
Public Const MOD_CONTROL = &H2
Public Const MOD_SHIFT = &H4
Public Const GWL_WNDPROC = (-4)
Public Declare Auto Function RegisterHotKey Lib "user32.dll" Alias _
"RegisterHotKey" (ByVal hwnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As Integer, ByVal vk As Integer) As Boolean
Public Declare Auto Function UnRegisterHotKey Lib "user32.dll" Alias _
"UnregisterHotKey" (ByVal hwnd As IntPtr, ByVal id As Integer) As Boolean
Dim intTimerMax As Integer
Dim TimeOut As Boolean
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'用来实现热键呼出效果的
Dim isResult As Boolean
isResult = RegisterHotKey(Handle, 0, MOD_ALT, 0)
If isResult = False Then
MsgBox("注册热键Ctrl+F失败。", , "零拾")
End If
TimeOut = True
Debug.Write("start ")
End Sub
Protected Overrides Sub WndProc(ByRef m As Message) '用来实现热键呼出效果的
If m.Msg = WM_HOTKEY Then
Debug.Write(m.Msg.ToString & " ")
If TimeOut = True Then
TimeOut = False
intTimerMax = 0
Timer_HotKey.Interval = 1
Timer_HotKey.Start()
Else
If Me.Visible = True Then
Debug.Write("hide ")
'Me.ShowInTaskbar = False
Me.Hide()
'Me.WindowState = FormWindowState.Minimized
Else
Debug.Write("show ")
Me.ShowInTaskbar = True
Me.Show()
Me.WindowState = FormWindowState.Normal
End If
TimeOut = True
End If
End If
MyBase.WndProc(m)
End Sub
Private Sub Timer_HotKey_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer_HotKey.Tick
intTimerMax = intTimerMax + 1
If intTimerMax = 100 Then
Timer_HotKey.Stop()
TimeOut = True
End If
End Sub
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.DirectoryServices;
using System.Runtime.InteropServices;
namespace test
...{
public partial class Form1 : Form
...{
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 Form1()
...{
InitializeComponent();
HotKey.RegisterHotKey(this.Handle, 100, 0, Keys.F1);
}
protected override void WndProc(ref Message m)
...{
const int WM_HOTKEY = 0x0312;
//按快捷键
switch (m.Msg)
...{
case WM_HOTKEY:
switch (m.WParam.ToInt32())
...{
case 100:
MessageBox.Show( "Press F1 ");
break;
}
break;
}
base.WndProc(ref m);
}
}
}