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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With