How to route a button's messages to main msgproc?

Started by
10 comments, last by Skizz 19 years, 5 months ago
Hi, I've got a 'button' whos parent is a 'static' who's parent is the main window. mainWindow<-static<-button How would I get the "button's" messages to goto the main msgproc that has been assigned to the main window? Thanks
Advertisement
Maybe you have to Attach the button to the main window ? You will need to Detach the button first.

If it isn't working, take a bath, have a think and try again...

When you create the button, make its parent mainWindow, then do:
SetParent (button, static);

Skizz
Quote:Original post by Skizz
When you create the button, make its parent mainWindow, then do:
SetParent (button, static);

Skizz


i have, i say:
buttonHwnd = createwindow("button".....mainHwnd....);
SetParent(buttonHwnd,staticHwnd);

It works fine with the "edit" control, but when I use buttons, it sends no messages.
Hmmm. Nothing like a bit of consistancy there then. I guess you're left with either subclassing the static control or subclassing the button itself. I'd go with the former.

It does say in the SDK that buttons are designed to be used in dialog boxes and that their use anywhere else may lead to 'behaviour in a nonstandard fashion'.

Skizz
You wouldn't have any spare example programs that you have done with that?

I've tried so many examples on the net, and they are never complete or explain it well enough.
It's not that difficult really:

1) Creation of window
a) swnd = CreateWindow ("static");
b) oldproc = GetWindowLong (swnd, GWL_WNDPROC)
c) SetWindowLong (swnd, GWL_WNDPROC, static_proc_handler)
d) SetWindowLong (wndmain, GWL_USERDATA, oldproc) // or some other window related variable. A global may not be enough.

2) The static_proc_handler
LRESULT CALLBACK static_proc_handler (HWND wnd, UINT m, WPARAM w, LPARAM l){    HWND parent = get the parent window    switch (m)    {    case BN_CLICK:    // and any other messages        SendMessage (parent, m, w, l);        break;    default:        proc = GetWindowLong (parent, GWL_USERDATA);        CallWindowProc (proc, wnd, m, w, l);        break;    }}

3) Tidying up:
a) old_proc = GetWindowLong (wndmain, GWLUSERDATA)
b) SetWindowLong (swnd, GWL_WNDPROC, old_proc)

Skizz
I'm just curious why its necessary to restore the old msgproc?
Errr, what is everyone smoking today?

First, why is the button's parent the static control? The parent of a control receives messages relating to children. You make the parent be the containing window so you *don't* have to subclass the intermediary windows! It is ridiculous to subclass a static control for this rason. The parent window acts as the mediator between the various UI objects. (Yes, just like the pattern, we can dance around knowing that we used a design pattern!) It is even more ridiculous to make windows children of the static control when a static control is usually just used as a place holder or a label. Make the main window the parent of the control and things will be a lot more sane.

Second, the reason there aren't many tutorials on this is cause its covered *very* well by Petzold's Win32 Programming book. Don't even touch Win32 without it nearby.

Third, this is in MSDN:
Contents:
User Interface Design and Development
- Windows Controls
- - Individual Control Information
- - - Button Controls

Fourth, the notification message is a WM_COMMAND with a notification code of BN_CLICKED. I'll let you look up how to determine where it is coming from.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
im using the static controls as panels to hold screens, which i then just hide when they arnt used

This topic is closed to new replies.

Advertisement