EnumChildProc with data from a Class

I have 6 child windows which are to display the results of a dice throw and a button to throw the dice. Simple enough on it's own:
1
2
3
4
5
6
7
8
BOOL CALLBACK EnumChildProc(HWND hWnd, LPARAM lParam)
{
  DiceLogic dice;  // A class for random numbers
  TCHAR rndNum[16];
  short int roll = dice.roll(6);
  SendMessage(hWnd, WM_SETTEXT, NULL, (LPARAM)myItoa(rndNum, roll));
  return TRUE;
}


However, these roles are actually stored in a class. What would be a good way to access these values? I've made a global idx variable, but this is already getting a little out of hand.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// When the button is pressed
BOOL onButton(HWND hWnd, LPARAM lParam)
{
	newRoll();  // This updates the classes 6 dice rolls
	idx = 0;    // Set the index to zero 
	EnumChildWindows(GetWindow(hWnd, GW_CHILD), EnumChildProc, lParam);
	return TRUE;
}

BOOL CALLBACK EnumChildProc(HWND hWnd, LPARAM lParam)
{
	TCHAR rndNum[16];
	short int roll = myClass.getRoll(idx++);
	SendMessage(hWnd, WM_SETTEXT, NULL, (LPARAM)myItoa(rndNum, roll));
	return TRUE;
}


It works, so I am at least happy to move on. Another complication though, I've sort of bound myself to the class holding arrays. I can't have private variables maxFromRole, minFromRole, I would have to have dataAboutRoles[/*some size*/]; and just be forced to remember everything.

Basically, I need some way to know what window I am looking at during the Enumerate function. I see there is a GetWindowInfo() function, but nothing in the WINDOWINFO struct seems to be of any value for figuring this out.
Your complication seems to rely in the fact that your button triggers dice rolling based on the user interface as opposed to your class system. Said differently: You are programming in function of your user interface instead of programming a user interface in function of your classes.

If you need to roll 6 dice, then have a std::vector<dice> and roll each one. Then use EnumChildWindows() if that's what you want, and pass as lParam a simple "string provider" class. A std::queue<std::string> or std::stack<std::string> probably work beautifully.
Trying to put what you said into my own words. Dice makes its rolls, and can then return a nicely ordered std::stack<std::string> to pass as the Lparam. EnumChildWindows simply pops the stack, so there is no need for an index. This sounds like it will work.

I'm having trouble picturing your first paragraph though. I think basically what you say is Dice should be taking care of the itoa itself (as well as other things) so I can just throw it into the windows functions. Perhaps you mean Dice should be holding/manipulating the actual windows?
Last edited on
My first paragraph is saying: The child windows should not be considered the masters of the dice. The dice exist independently and so happens that you wish to have these child windows to simply show what the dice say.

In other words: Your coding should be easily de-coupled from one user interface and be placed into another user interface. If your models can do that, then you're in good shape

And yes, you understood how the stack would simplify the issue by giving the responsiblilty to roll the dice to a central action (like the click of a button) and then simply delivering the result to the child windows.
Thanks.
In other words: Your coding should be easily de-coupled from one user interface and be placed into another user interface. If your models can do that, then you're in good shape

I think I'm in good shape :). I'm doing the beginner thing of moving from console to Win32. All of these classes are made already, there's just too much info to try to format properly in console.

Topic archived. No new replies allowed.