Pergunta

I'm creating a program to manage a local network but Im having a little problem. When the form starts, it starts hidden.

I used this code to hide it:

Protected Overrides Sub SetVisibleCore(ByVal value As Boolean)
    If Not Me.IsHandleCreated Then
        Me.CreateHandle()
        value = False
    End If
    MyBase.SetVisibleCore(value)
End Sub

What I want exactly is to show the form when I click ALT+X for example.

Foi útil?

Solução

Use a Global Hotkey. That way you don't have to worry about your Application having focus. A good example for VB.Net is here:

http://www.kirsbo.com/How_to_add_global_hotkeys_to_applications_in_VB.NET

To summarise. Define a HotKey class:

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

Then add the following Sub to your main form:

'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 

Then register a HotKey in the startup code for your form:

Hotkey.RegisterHotKey(Me, "X", Hotkey.KeyModifier.Alt)

The first parameter is the form, the second is the key to be handled and the third is the modifier key. Once this Hotkey is registered it will trigger code in HotKey.handleHotKeyEvent.

You can then use some sort of Callback to trigger a method in the calling form itself if you so desire.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top