Community discussion forum

VB.NET Windows: Getting Windows Message from external class without overriding WndProc

  • 1 year ago
    I want to ask out of asp.net box. It is about windows form The purpose is I want to get Windows message, i know the way is like this: Public Class MainForm Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message) ' Do something here MyBase.WndProc(m) End Sub End Class however in my case, not that simple, this is about OOP and events. let say I use this code: Public Class HotKeyManager Private TheMainForm as Form Public Sub New(MainForm as Form) Me.TheMainForm = MainForm End Sub ' user of this class need to call this sub in it Form WndProc Public Sub WndProc(ByRef m As Message) ' do something to m End Sub ' other code here End Class and will be implemented to form code: Public Class MainForm Dim aHotKeyManager as HotKeyManager = New HotKeyManager(Me) Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message) aHotKeyManager.WndProc(m) MyBase.WndProc(m) End Sub End Class we can see that, I can get the Windows message from my HotKeyManager class. This is the way I do now. But then, I am the designer of HotKeyManager class and other users may use my HotKeyManager to their Form code. My expected is user should just write code just like this: Public Class MainForm Dim aHotKeyManager as HotKeyManager = New HotKeyManager(Me) End Class The problem is I don't know how to capture the windows message ONLY from my HotKeyManager class There is no event handler for WndProc related in the System.Windows.Form for HotKeyManager to attach. I don't want my user touch about WndProc. In other words ---> Is there any way for my HotKeyManager class to trace and get a message when Form.WndProc called by OS without overriding the WndProc sub?
  • 1 year ago
    Nope, you should force your users to use your own Form derivate, which externs event for WndProc...
  • 1 year ago
    Yeah, so many times I googling, No option. I need to derive my own form, to override the WndForm and capture Windows message. However user (me also as a user) may need to derive their standard form to Ribbon Form add-in such as from DevExpress. So I don't want to force user use my form, instead force user to override WndProc just like my 3rd code in my first post. It still not a best practice, but no choice.
  • 11 months ago
    Hm, but I am not very sure, that you have option to access WndProc in symbiotic behaviour class (#3). For example If I created WndProc "injection" to a form for clipboard scanner, It was possible to create dummy window in my class, which provided its WndProc byEvent to my symbiot class. But here, as you need to assign hotkeys to window, there is no option of dummy window. Probably prove user32 [RegisterHotKey](http://www.pinvoke.net/default.aspx/user32.RegisterHotKey), it provides you operating above specified window without need to "inject" its WndProc in base code.

Post a reply

Enter your message below

Sign in or Join us (it's free).

We'd love to hear what you think! Submit ideas or give us feedback