Solved

Windows API class overriding - WM_COMMAND problem.

Posted on 2004-10-07
2
583 Views
Last Modified: 2008-01-09
I've been playing around with the Windows API controls a bit, and I started creating some basic wrappers for them out of boredom. I did this by getting the standard class (BUTTON, STATIC, etc), change the name of the class, and changing the WNDPROC to that of my own.
Inside my own WNDPROC, I'd intercept the WM_LBUTTONDOWN, WM_RBUTTONDOWN, etc messages, do what I wanted (call a function pointer if applicable) and then run it through the original classes WNDPROC so it would act and display like normal.
At first it was a bit of a problem, doing things like "button->on_click(function)" type things, since the default standard class (BUTTON, STATIC, etc) sends the WM_COMMAND messages to the parent, and not my WNDPROC, so as a run around, I used the WM_LBUTTONDOWN: call_on_lbd(); inside my WNDPROC etc.
Then after I'd finished playing around with BUTTON, STATIC, COMBOBOX, and EDIT, I started on LISTBOX. The problem with this is that there are no specific messages being sent to my WNDPROC that tell me when a new item is being selected. Yea, it sends things like WM_LBUTTONDOWN, etc. But nothing specific towards what index in the list box is being selected (unlike the combo box where for some reason I receive some WM_COMMAND messages (specifically when something new is being selected) in my WNDPROC and the parents proc... Wierd...).
So my question (finally :P) is this:
Is there any way to redirect, or some how force the WM_COMMAND messages to my WNDPROC function? The parent doesn't need to know when something it being clicked, I want to know that inside my custom class.
A very, very basic code example:

class Control
{
...
      //my override WNDPROC thingy
      static LRESULT CALLBACK wnd_proc(...)
      {
            Control *c = (Control *)GetWindowLong(...);
            WNDCLASS wc;
            GetClassInfo(h_inst, c->base_class(), &wc);

            switch(message) {
            case WM_LBUTTONDOWN:
                  c->call_on_lbd();
            case WM_RBUTTONDOWN:
                  c->call_on_rbd();
            case...
      }

      return wc.lpfnWndProc(...); //call the base class’s WNDPROC
      }
}
0
Comment
Question by:Larn0s
2 Comments
 
LVL 11

Accepted Solution

by:
KurtVon earned 250 total points
ID: 12248994
No, unfortunately there is none.  There is a trick called "reflection" that MFC uses.  All MFC windows send these messages to the control window that generated them to see if they are handled there.  I can't remember if the window tries to handle the message bfore or after it reflects them to the control, but I'm sure if there was an easy or standard way to do this without reflection, the MFC programmers would have done it.

Hope this helps.
0
 

Author Comment

by:Larn0s
ID: 12249947
Ah, dang.
Oh well, thanks for the info KurtVon. It helped me do what I wanted (figured it out n' such).
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now