MSWMessageHandler example

Do you have a typical platform dependent issue you're battling with ? Ask it here. Make sure you mention your platform, compiler, and wxWidgets version.
Post Reply
DjSt3rios
Earned a small fee
Earned a small fee
Posts: 17
Joined: Thu Mar 11, 2021 4:41 pm

MSWMessageHandler example

Post by DjSt3rios »

I've been trying to register a windows message handler with no luck. I think it needs a pointer but I can't make it work, and there seems to be no examples in google either. Can someone give me an example how to use it?
New Pagodi
Super wx Problem Solver
Super wx Problem Solver
Posts: 465
Joined: Tue Jun 20, 2006 6:47 pm
Contact:

Re: MSWMessageHandler example

Post by New Pagodi »

If you want to handle messages on windows, you should derive a class from whatever wwWidgets object you are using and override MSWOnNotify or MSWHandleMessage.
DjSt3rios
Earned a small fee
Earned a small fee
Posts: 17
Joined: Thu Mar 11, 2021 4:41 pm

Re: MSWMessageHandler example

Post by DjSt3rios »

New Pagodi wrote: Fri Mar 19, 2021 8:36 pm If you want to handle messages on windows, you should derive a class from whatever wwWidgets object you are using and override MSWOnNotify or MSWHandleMessage.
Thanks for your input, I made a little function like this:

Code: Select all

bool MainWindow::MSWHandleMessage(WXLRESULT res, WXUINT msg, WXWPARAM wParam, WXLPARAM lParam)
{
	wxLogDebug(wxString("Message ID: ") + wxString(std::to_string(msg)));
	return false;
}
but it doesn't do anything, I guess I need to register it using MSWRegisterMessageHandler, but I don't know what to pass as second parameter which asks for a "wxWindow::MSWMessageHandler" object. I tried creating a MSWMessageHandler with no success :(
User avatar
doublemax
Moderator
Moderator
Posts: 19103
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: MSWMessageHandler example

Post by doublemax »

but I don't know what to pass as second parameter which asks for a "wxWindow::MSWMessageHandler" object
It expects a function pointer with the following signature:

Code: Select all

    typedef bool (*MSWMessageHandler)(wxWindowMSW *win,
                                      WXUINT nMsg,
                                      WXWPARAM wParam,
                                      WXLPARAM lParam);
Alternatively you can override wxWindow::MSWWindowProc

Code: Select all

WXLRESULT mainFrame::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
{
  if( nMsg == WM_DEVICECHANGE )
  {
    wxLogDebug(wxT("WM_DEVICECHANGE %x %x"), wParam, lParam);
    if( wParam==DBT_DEVICEARRIVAL || wParam==DBT_DEVICEREMOVECOMPLETE )
    {
      // update device list
    }
  }

  return wxFrame::MSWWindowProc(nMsg, wParam, lParam);
}
Use the source, Luke!
DjSt3rios
Earned a small fee
Earned a small fee
Posts: 17
Joined: Thu Mar 11, 2021 4:41 pm

Re: MSWMessageHandler example

Post by DjSt3rios »

doublemax wrote: Sat Mar 20, 2021 12:29 pm
but I don't know what to pass as second parameter which asks for a "wxWindow::MSWMessageHandler" object
It expects a function pointer with the following signature:

Code: Select all

    typedef bool (*MSWMessageHandler)(wxWindowMSW *win,
                                      WXUINT nMsg,
                                      WXWPARAM wParam,
                                      WXLPARAM lParam);
Alternatively you can override wxWindow::MSWWindowProc

Code: Select all

WXLRESULT mainFrame::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
{
  if( nMsg == WM_DEVICECHANGE )
  {
    wxLogDebug(wxT("WM_DEVICECHANGE %x %x"), wParam, lParam);
    if( wParam==DBT_DEVICEARRIVAL || wParam==DBT_DEVICEREMOVECOMPLETE )
    {
      // update device list
    }
  }

  return wxFrame::MSWWindowProc(nMsg, wParam, lParam);
}
Thank you! I tried something like this with no luck:
Image
I can't understand how to pass a function pointer correctly so I used MSWWindowProc and now it works perfectly now!
User avatar
doublemax
Moderator
Moderator
Posts: 19103
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: MSWMessageHandler example

Post by doublemax »

As i'm seeing the WM_POWERBROADCAST in your code, maybe you missed wxPowerEvent ?
https://docs.wxwidgets.org/trunk/classw ... event.html
Use the source, Luke!
DjSt3rios
Earned a small fee
Earned a small fee
Posts: 17
Joined: Thu Mar 11, 2021 4:41 pm

Re: MSWMessageHandler example

Post by DjSt3rios »

doublemax wrote: Sat Mar 20, 2021 1:07 pm As i'm seeing the WM_POWERBROADCAST in your code, maybe you missed wxPowerEvent ?
https://docs.wxwidgets.org/trunk/classw ... event.html
Oh I had no idea about this! Thank you very much! :D
Post Reply