4
votes

A standard window procedure function takes this prototype:

LRESULT CALLBACK WndProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);

When a message such as WM_MOUSEMOVE or WM_CHAR, the WndProc function will receive the window the message originated from, and any extra parameters, which will be with msg and wParam/lParam.

What I have now is a class. Say

class Random
{
    public:
        void Initialize ();

    private:
        void Draw ();
        HWND hWnd;

    friend LRESULT CALLBACK RandomProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
};

After the hWnd is initialized and created by Initialize (), it will send messages such as WM_LBUTTONDOWN to RandomProc. Once a message is received, I would like RandomProc to use Draw () to redraw the window of the class Random.

The thing is, I will have multiple Random variables, and each will have a window. All the windows will send their messages to RandomProc, and will want it to redraw the corresponding windows of the hWnd. I do not know which random variable sent the message based upon the hWnd parameter nor the msg/wParam/lParam, and so cannot access the appropriate Draw () function and cannot redraw the correct window.

Is there a way to pass a pointer to the class of the window to the Procedure every time a message is sent or is there another way to access the Random class whose hWnd sent the message?

1
Messages are meant to be sent on a per-window basis. What's in your hWnd parameter? If you want the windows to use the same procedure, make them use the same class, or specify the procedure as the appropriate one for each Windows class you use.chris
In my case, the classes are sort of like "buttons". I need to declare several of them. They would all have the same procedure, as they are all the same type of "button", but I would like them to be in separate classes for easy control and declaration of the buttons. Is there a way to achieve this?GILGAMESH
But it's not like each button has the same HWND. That's how you tell them apart usually. Is there a huge problem with Draw taking a HWND?chris
Yes, I was looking for a way to tell the class from the HWND. I think someone has answered that already. Thanks for your help!GILGAMESH

1 Answers

8
votes

Aren't you looking for the GetWindowLongPtr/SetWindowLongPtr functions?

This function assigns/retrieves arbitrary pointer to/from the window handle. You might assign the pointer to your Random class instance to each window you create. In the RandomProc you just use the GetWindowLongPtr and cast the pointer to Random*.

As Chris says in a comment, use the GWLP_USERDATA attribute to assign the pointer.