0

I am making a bare bone application and I would like to avoid adding a form class to my project.

Here is what I have done so far

First my new project is a "Empty Project (.NET framework" enter image description here

Then I added a single empty class file enter image description here

Then I added the system.windows.forms reference to my project enter image description here

Lastly I add the following code

Imports System.Windows.Forms
Public Class AppCore
    Inherits ApplicationContext

    Shared Sub main()
        Dim myAppCore As AppCore
        myAppCore = New AppCore
        System.Windows.Forms.Application.Run(myAppCore)
    End Sub

End Class

From that point on, I have a barebone working app that does nothing but stays running forever.

Now I want to registers the keys combo "ALT+F6" and "ALT+F7"

I add the following code

Public Const MOD_CONTROL As Integer = &H11
Public Const MOD_SHIFT As Integer = &H10
Public Const MOD_ALT As Integer = &H1
Public Const WM_HOTKEY As Integer = &H312
Public Declare Function RegisterHotKey Lib "user32.dll" Alias "RegisterHotKey" (ByVal hwnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As Integer, ByVal vk As Integer) As Integer
Public Declare Function UnregisterHotKey Lib "user32.dll" Alias "UnregisterHotKey" (ByVal hwnd As IntPtr, ByVal id As Integer) As Integer

Sub New()
    RegisterHotKey(Me.Handle, 100, MOD_ALT, Keys.F6)
    RegisterHotKey(Me.Handle, 200, MOD_ALT, Keys.F7)
End Sub

Protected Overrides Sub DefWndProc(ByRef m As System.Windows.Forms.Message)
    MyBase.DefWndProc(m)
    Dim x As Long

    If m.Msg = WM_HOTKEY Then
        Select Case CType(m.WParam, Integer)
            Case 100
                Beep()
            Case 200
                Beep()
        End Select
    End If
End Sub

This is where I run into trouble

First in Sub New(), the IDE is telling me that Handle is not part of appcore/applicationcontext enter image description here

Second, I get the following error enter image description here

At this point I am stuck !

How can I create hotkeys without adding a form ?

For DefWndProc, maybe I can get away with only removing the "Overrides" keywork and dropping "MyBase.DefWndProc(m)" ?

But I think the bigger problem is how to get a handle ? Maybe I should look at the CreateWindow API ?


EDIT :

I have modified this project to include a new class of type NativeWindow

as follows

Imports System.Windows.Forms
Public Class clsHotkey
    Inherits NativeWindow

    Public Const WM_HOTKEY As Integer = &H312

    Protected Overloads Sub DefWndProc(ByRef m As System.Windows.Forms.Message)
        If m.Msg = WM_HOTKEY Then
            Select Case CType(m.WParam, Integer)
                Case 100
                    Console.WriteLine("ID 100")
                Case 200
                    Console.WriteLine("ID 200")
            End Select
        End If
    End Sub

End Class

This provides the bare minimum to have a window handle for RegisterHotkey. I have also modified the AppCore class to work with it.

It compiles and runs without error but the hotkeys do nothing

I believe this is because I have replaced the override keywork in the definition of the DefWndProc subroutine by overloads

If I try to use overrides, I get the following error

BC31086 'Protected Overrides Sub DefWndProc(ByRef m As Message)' cannot override 'Public Overloads Sub DefWndProc(ByRef m As Message)' because it is not declared 'Overridable'.

At this point I am stuck. I would rather not use a form class because of the added complexity (especially the addition of a designer file for it which will make manual compiling more complicated)

Is there a way to make it work with NativeWindow class ? Or some other alternative class that can obtain a window handle ?

Thanks !

Shodan
  • 1,065
  • 2
  • 13
  • 35
  • A `WndProc` is attached to a window (thus the `Wnd` portion of the name). Hotkey messages are sent to the thread that owns the window. No window, no message handler, no messages. `Me.Handle` is for apps that use forms (windows), and represents that window's handle. No window, no `Me.Handle`. – Ken White Sep 10 '17 at 01:03
  • I'm not sure exactly how, but you may be able to use the `NativeWindow` class. That's something you might look into. – jmcilhinney Sep 10 '17 at 03:20
  • Native class is a great idea and it seems to fit the bill. I have modified the appcore class as per https://pastebin.com/mUtmAntb and I have added a new empty class of type NativeWindow as per https://pastebin.com/UD4DS890 – Shodan Sep 10 '17 at 19:30
  • This gives no error in the IDE and it runs, but it does not register the hotkeys – Shodan Sep 10 '17 at 20:02
  • I had to modify the DefWndProc procedure from "overrides" to "overloads" because, according to the IDE, DefWndProc is not declared overridable. Apparently, overrides doe not work – Shodan Sep 10 '17 at 20:04
  • Seems to me that it should be WndProc and not DefWndProc that you are overriding? – Craig Sep 11 '17 at 14:23
  • Hi, I will look into this, I have based my code on the following example and they used DefWndProc, maybe this name is different in NativeWindow. https://stackoverflow.com/questions/14168258/registering-3-hotkeys-possible – Shodan Sep 11 '17 at 22:30
  • Ok, with WndProc I can now use overrides and call MyBase.WndProc(m) , this compiles and run without error, however the ALT+F6 // ALT+F7 does not work. Here are the two classes as they exist now https://pastebin.com/qfnznQQD – Shodan Sep 11 '17 at 22:41
  • I just checked, WndProc never gets called – Shodan Sep 11 '17 at 22:52

0 Answers0