我正在创建一个管理本地网络的程序,但我遇到了一些问题。 表单启动时,它开始隐藏。
我使用此代码隐藏它:
Protected Overrides Sub SetVisibleCore(ByVal value As Boolean)
If Not Me.IsHandleCreated Then
Me.CreateHandle()
value = False
End If
MyBase.SetVisibleCore(value)
End Sub
我想要的是在我点击 ALT + X 时显示表格。
答案 0 :(得分:2)
使用全球热键。这样您就不必担心您的应用程序具有焦点。这是VB.Net的一个很好的例子:
http://www.kirsbo.com/How_to_add_global_hotkeys_to_applications_in_VB.NET
总结一下。定义HotKey类:
Public Class Hotkey
#Region "Declarations - WinAPI, Hotkey constant and Modifier Enum"
''' <summary>
''' Declaration of winAPI function wrappers. The winAPI functions are used to register / unregister a hotkey
''' </summary>
Public Declare Function RegisterHotKey Lib "user32" _
(ByVal hwnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As Integer, ByVal vk As Integer) As Integer
Public Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer) As Integer
Public Const WM_HOTKEY As Integer = &H312
Enum KeyModifier
None = 0
Alt = &H1
Control = &H2
Shift = &H4
Winkey = &H8
End Enum 'This enum is just to make it easier to call the registerHotKey function: The modifier integer codes are replaced by a friendly "Alt","Shift" etc.
#End Region
#Region "Hotkey registration, unregistration and handling"
Public Shared Sub registerHotkey(ByRef sourceForm As Form, ByVal triggerKey As String, ByVal modifier As KeyModifier)
RegisterHotKey(sourceForm.Handle, 1, modifier, Asc(triggerKey.ToUpper))
End Sub
Public Shared Sub unregisterHotkeys(ByRef sourceForm As Form)
UnregisterHotKey(sourceForm.Handle, 1) 'Remember to call unregisterHotkeys() when closing your application.
End Sub
Public Shared Sub handleHotKeyEvent(ByVal hotkeyID As IntPtr)
MsgBox("The hotkey was pressed")
End Sub
#End Region
End Class
然后将以下Sub添加到主窗体中:
'System wide hotkey event handling
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = Hotkey.WM_HOTKEY Then
Hotkey.handleHotKeyEvent(m.WParam)
End If
MyBase.WndProc(m)
End Sub
然后在表单的启动代码中注册HotKey:
Hotkey.RegisterHotKey(Me, "X", Hotkey.KeyModifier.Alt)
第一个参数是表单,第二个是要处理的键,第三个是修饰键。一旦注册了该热键,它将触发HotKey.handleHotKeyEvent
中的代码。
如果您愿意,可以使用某种回调来触发调用表单中的方法。