Helder AC
Helder AC

Reputation: 376

Winapi Multiple Windows same WindowProc

I am developing a GUI with WINAPI and I've a question. I made a custom progress bar with the respective Procedure for handling it's messages. I paint the progress bar myself. For the progress bar percentage I use a static variable that I update using a custom message and then I repaint the progress bar by using InvalidateRect. Now how could I optimize my code so I could create multiple windows of my ProgressBar class. The problem is that I can't use that same static percentage variable for all of them! So each instance should have it's own percentage variable.

Thank you

Upvotes: 1

Views: 221

Answers (2)

Jonathan Potter
Jonathan Potter

Reputation: 37122

All windows have at least one pointer-sized user data variable that you can use for whatever purpose you like - it is accessed via GetWindowLongPtr/SetWindowLongPtr with the index GWLP_USERDATA.

Additionally, when you register a window class, you can specify additional user data to be allocated for each window in your class, using the WNDCLASS member cbWndExtra. For example, if you set this to sizeof(DWORD_PTR) when you registered your class, you could also store a DWORD_PTR-sized value using SetWindowLongPtr with index 0.

Depending on how much data you want to store per-window, you can store it directly using the above methods, or allocate your own structure and store a pointer to it (remembering to free the data when the window is destroyed).

An additional method of storing data per-window is using window properties via the SetProp and GetProp functions, which let you store one or more pointer-sized name/value pairs.

Upvotes: 3

stamhaney
stamhaney

Reputation: 1314

Dont make the percentage variable static. Make it part of the class and read/write from getter/setters

Upvotes: 0

Related Questions