2

I'm a bit confused as to what NativeWindow should be used for.

What I would like to do is instantiate a NativeWindow-derived object and use it to listen to certain Windows events - rather than listening to them in one of my forms. However, I seem to understand that, in order to have my window's message pump receive messages, I need to assign the window a handle. The tutorials I could find all refer to assigning the window the handle of a "parent form", which would partly defeat my goal (ok, I'd get to separate the message-listening concern, but I would still need to attach that concern to one of my forms rather than having it run on its own).

So, to the question:

Is there a way to construct a NativeWindow that is capable of receiving system events in its message pump without having to make it "a child" of another form?

Simone
  • 1,260
  • 1
  • 16
  • 27
  • Sounds like [XY problem](https://meta.stackexchange.com/q/66377/299295). I don't know the purpose of `NativeWindow` class (to register own window class?), but can you tell to which events you want to listen and for what? There are hooks for global keyboard/mouse events. – Sinatr May 10 '17 at 07:34
  • @Sinatr I got the feeling that `NativeWindow` was my solution because of this comment (http://stackoverflow.com/questions/16245706/check-for-device-change-add-remove-events#comment23243821_16245901). I need to listen to USB device connection/disconnection. – Simone May 10 '17 at 07:51
  • Have you been [here](http://stackoverflow.com/q/935608/1997232)? – Sinatr May 10 '17 at 08:20
  • @Sinatr Thanks for the tip. However, my window is still not receiving system events :( – Simone May 10 '17 at 09:17
  • Yes, NativeWindow is good enough to get the job done. You have to derive your own class so you can override WndProc(). And call its CreateHandle() method to create the window. The CreateParams you pass is not critical here, defaults are fine. Call DestroyHandle() when you no longer need it. – Hans Passant May 10 '17 at 09:33
  • @Sinatr Thanks for pointing me in the right direction! My problem with USB connections is that I need to register to those events using the window's handle, which is only available right AFTER the `CreateHandle()` call. If you would please post an answer, I'll gladly mark it as accepted. – Simone May 10 '17 at 09:49
  • @HansPassant Thanks for the confirmation. I just needed to get the handle in the right moment, i.e., after it was created. I need it to register to USB device change events. – Simone May 10 '17 at 09:50
  • If you know the answer, then please [post it](https://stackoverflow.com/help/self-answer) for future readers. – Sinatr May 10 '17 at 10:25
  • @Sinatr Should I post it myself? I didn't solve it on my own. – Simone May 10 '17 at 10:34

1 Answers1

2

I was finally able to solve my problem. My issues were twofold: with the NativeWindow itself and with USB notifications.

Correctly implement a NativeWindow

Thanks to Sinatr for pointing me in the right direction. This thread has a great solution for the message-only window problem. I used that as the base class for my window.

Receive USB notification messages - or any other message that requires a handle, for that

The messages I wanted to listen to are USB notifications, for which I followed this answer. My problem was that I had to register to those messages passing the handle as the parameter... but I didn't pay attention, and ended up doing that before CreateWindow() was called, so with no valid handle.

So, make sure you do something like this in your derived class:

public override bool CreateWindow()
{
    bool retval = base.CreateWindow();
    USBNotification.RegisterUSBDeviceNotification(this.Handle);
    // or the registration to any notification you need
    // or any use you have for the handle
    return retval;
}
Community
  • 1
  • 1
Simone
  • 1,260
  • 1
  • 16
  • 27