Hiding window loses window's functionality
I have a vb.net application that, when a short cut is pressed, a window pops up. I also would like to make it completely invisible, unless the short cut is pressed. In the default class that houses my code for noticing the short cut and coordinating the events of the short cut, I have placed the following code:
Me.ShowInTaskbar = False
I have isolated this code as the issue. The issue is that my application does not work when it is not shown in the taskbar. This is only the default form - for the pop up window I have a separate c开发者_Python百科lass. How can I create a workaround for hiding the window in the taskbar and hiding it in general?
Thanks.
Btw, this is my hotkey code:
Public Const MOD_ALT As Integer = &H1
Public Const WM_HOTKEY As Integer = &H312
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
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = WM_HOTKEY Then
'Stuff do do when Alt-Q is pressed
End If
MyBase.WndProc(m)
End Sub
You didn't post enough of your code. I can guess at the cause though. Changing the ShowInTaskbar property has a big side effect. It is one of the properties that are actually window style flags under the hood. Specified in the CreateWindowsEx() call. Which poses rather a problem, changing the property requires the native window to be re-created.
That is implemented in Winforms, but it can cause trouble. Note the RegisterHotKey() function declaration, the first argument is the window handle. Problem is, when Winforms recreates the native window, the window handle will be different. Or in other words, your hot key isn't registered any more.
The workaround is simple, you need to re-register your hotkey when the window gets recreated. Move the RegisterHotKey() call from, I need to guess again, the Load event handler to this method:
Protected Overrides Sub OnHandleCreated(ByVal e As System.EventArgs)
MyBase.OnHandleCreated(e)
RegisterHotKey(me.Handle, etc...)
End Sub
There should be a attribute like Me.object.Visible or form2.visible which you can set it to true and false. Just make sure it is also closed on exit of the main program.
精彩评论