Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass extra wParam/lParam parameters?

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?

like image 978
GILGAMESH Avatar asked Feb 21 '23 08:02

GILGAMESH


1 Answers

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.

like image 187
Viktor Latypov Avatar answered Feb 22 '23 23:02

Viktor Latypov