请问如何能实现 热键呼出 功能?

wildgunhuang 2007-11-23 10:54:27
要做到的功能是:双击Alt键显示窗口,再次双击Alt键隐藏窗口。

最好是用类库,而不用API或者钩子什么的……

谢谢各位了~
...全文
197 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
wildgunhuang 2007-11-28
  • 打赏
  • 举报
回复
怎么做到“连续按2次alt就显示,再连续按2次alt就隐藏”呢……

我现在只能显示,而且只能显示一次……



'用来实现热键呼出效果的
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



上面是部分代码,intTimerMax是用来计时的,控制2次Alt之间不能超过100毫秒,否则不算做连续按Alt
wildgunhuang 2007-11-28
  • 打赏
  • 举报
回复
怎么做到“连续按2次alt就显示,再连续按2次alt就隐藏”呢……

我现在只能显示,而且只能显示一次……



'用来实现热键呼出效果的
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



上面是部分代码,intTimerMax是用来计时的,控制2次Alt之间不能超过100毫秒,否则不算做连续按Alt
it_gz_xi 2007-11-27
  • 打赏
  • 举报
回复
留个脚印
jinjazz 2007-11-26
  • 打赏
  • 举报
回复
http://blog.csdn.net/jinjazz/archive/2007/11/15/1886055.aspx
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);
}



}
}
jinjazz 2007-11-26
  • 打赏
  • 举报
回复
类库也是从api来的
Msconfig_001 2007-11-26
  • 打赏
  • 举报
回复
呵呵!!!也想知道
关注!
lwl0376 2007-11-25
  • 打赏
  • 举报
回复
关注
曲滨_銘龘鶽 2007-11-25
  • 打赏
  • 举报
回复
是 RegisterHotKey 这个API函数 在msdn索引里输入 RegisterHotKey 可以看到详细的说明

如果要代码在 www.google.com 输入 VB.net RegisterHotKey

可以直接点击

liuyun1987 2007-11-23
  • 打赏
  • 举报
回复
不晓滴..路过帮顶.....

16,556

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧