[SOLVED]WindowProc for multiple windows?

Started by
14 comments, last by VanillaSnake21 15 years, 4 months ago
I totally forgot how to handle multiple windows within the WinProc. I'm doing something like if(hWnd == MyWindow) { switch(uMsg) ... } but it's not working. [Edited by - VanillaSnake21 on December 15, 2008 1:42:43 PM]

You didn't come into this world. You came out of it, like a wave from the ocean. You are not a stranger here. -Alan Watts

Advertisement
Why would you handle two different windows with the same WndProc? Common sense would suggest to me that if the windows are different, use a different WndProc...
Quote:Original post by Codeka
Why would you handle two different windows with the same WndProc? Common sense would suggest to me that if the windows are different, use a different WndProc...


What if I have a window created at runtime (which I do, an error message dialog box)?

You didn't come into this world. You came out of it, like a wave from the ocean. You are not a stranger here. -Alan Watts

Quote:Original post by VanillaSnake21
What if I have a window created at runtime (which I do, an error message dialog box)?


Oh, I see what you mean. You might have multiple instances of the same window sharing the same WndProc. That makes sense.

The way I implement such things (and I think this is a common pattern) is you have a C++ class that encapsulates your window so you have one instance of this class per visible window, right?

You simply use the SetWindowLongPtr function to set the GWLP_USERDATA property to a pointer to your class. Then in the WndProc, use GetWindowLongPtr to get that pointer back and call the methods on your class to handle the message.
Quote:Original post by Codeka
Quote:Original post by VanillaSnake21
What if I have a window created at runtime (which I do, an error message dialog box)?


Oh, I see what you mean. You might have multiple instances of the same window sharing the same WndProc. That makes sense.

The way I implement such things (and I think this is a common pattern) is you have a C++ class that encapsulates your window so you have one instance of this class per visible window, right?

You simply use the SetWindowLongPtr function to set the GWLP_USERDATA property to a pointer to your class. Then in the WndProc, use GetWindowLongPtr to get that pointer back and call the methods on your class to handle the message.



Maybe I wasn't clear of what I'm trying to do. I want multiple windows created from the same WINDOWCLASS structure (meaning WndProc handles all of their messages) to be able to lets say paint in their client area.

So inside WindowProc I have

switch(uMsg)
{
case WM_PAINT: TextOut(...)
}

but this outputs the Text on all the windows, and even if I specify the correct window in TextOut(m_MyDialog...) that would still be called even when don't want to draw to this window.

I remeber doing this and it was pretty easy. What I basically want to know is which window the message is being passed to (this is contained in the first param of WindowProc) and then compare that handle to the handle of the window that I have, if they are the same paint in the client of that window.

You didn't come into this world. You came out of it, like a wave from the ocean. You are not a stranger here. -Alan Watts

if it's a dialog box, they have their own type of window procedure
--------------------------------------Not All Martyrs See Divinity, But At Least You Tried
Quote:Original post by godsenddeath
if it's a dialog box, they have their own type of window procedure


I'm implementing my own dialgog box so I'm registering it as a normal window. How can no-one know how this could be done, this is beyond trivial.

You didn't come into this world. You came out of it, like a wave from the ocean. You are not a stranger here. -Alan Watts

Quote:Original post by VanillaSnake21
How can no-one know how this could be done, this is beyond trivial.
You just check the hWnd parameter. I don't quite follow how this isn't working - a HWND refers to one window, unless you're updating all windows with the same data, there shouldn't be a problem.

If you have:
switch(uMsg){case WM_PAINT: TextOut(...)}
then that will work fine, provided you pass the HDC from BeginPaint() or the HDC from GetDC(hWnd). If that's not working, we'll need to see more code.

EDIT:
Quote:Original post by VanillaSnake21
What I basically want to know is which window the message is being passed to (this is contained in the first param of WindowProc) and then compare that handle to the handle of the window that I have, if they are the same paint in the client of that window.
If you get a WM_PAINT, you need to paint something. If you ignore it and only handle particular window handles, your windows won't update properly (Not sure if you're doing this already though).

The way I'd do this is by using the method Codeka suggests. That should work fine. Here's some example code that works fine with multiple windows for one class.
Might be guessing here but read up on how messages are propagated to child windows. I think Windows will keep pasing the "paste" message to all child windows and the parent until it figures out that one of the child windows handled the event (so you'll need to return the correct value from the WinProc once you find the window that handled the message.)

Also, how are you keeping track of that "MyWindow" variable?
@EvilSteve
LRESULT  CALLBACK WindowManagerExtension::m_MessageProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam){ 	switch(uMsg)	{	case WM_CLOSE: PostQuitMessage(0); break;	case WM_PAINT:	{                //*******                //DOESNT WORK                //**********		HDC hDC = GetDC(m_Dialog);		TextOut(hDC, 20, 20, "Hello", 5);		ReleaseDC(m_Dialog, hDC);                //WORKS***                hDC = GetDC(hWnd);               	TextOut(hDC, 20, 20, "Hello", 5);		ReleaseDC(m_hWnd, hDC);	} break;	}	return DefWindowProc(hwnd, uMsg, wParam, lParam); }

Thats my code. Basically when I use m_Dialog (window for the dialog stored in my class) to get the DC there is no output. When I use the window that is passed to WinProc (hWnd) then it works but outputs on both the dialog window and the main window.

You didn't come into this world. You came out of it, like a wave from the ocean. You are not a stranger here. -Alan Watts

This topic is closed to new replies.

Advertisement