MFC CButton runtime problem

Hi,


I have a problem using the MFC CButton class.

I want to create n instances of CButton; n Buttons on my window. N is declared during runtime, so I need a way of creating it, obviusly:P

I've tried pointers.

I've created a CButton * buttons and used it like this:

buttons=new CButton[y*x*z];

where x,y,z are variables declaring number of buttons, all integreters.

So, than I used a for loop to 'manufacture' those pesky buttons...well, it works fine. FOR THE FIRST BUTTON - button[0], any other button wont draw to the screen.

And now, the sourcecode:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
 ...
 CONTAINER *container;
 ALGORITEM *bot;
 CButton *buttons;

public:
     BOOL InitInstance()
     {
	container = new CONTAINER(3,3,1);
	container->clearField('X');
	bot = new ALGORITEM(container->getWidth(), container->getHeight(), container->getDeepth());

         wnd = new MFC_Window();
	buttons = new CButton[container->getDeepth()*container->getHeight()*container->getWidth()];

	int xPos=10;
	int yPos=10;

	for(int z=0; z<container->getDeepth(); z++){
		for(int y=0; y<container->getHeight(); y++){
			for(int x=0; x<container->getWidth(); x++){
				buttons[z*container->getDeepth()+y*container->getHeight()+x].Create(container->getLocationW3d(x,y,z),
																					WS_VISIBLE|BS_PUSHBUTTON,
																					CRect(xPos,yPos,50,50),
																					wnd,
																					(z*container->getDeepth()+y*container->getHeight()+x));
				xPos+=60;
			}
			yPos+=60;
		}
	}
        ...


Many thanks,


Gregor
Last edited on
The solution:

Crate multiple CRect classes. One for each button.

Somthing like this:

1
2
3
4
5
6
7
8
9
10
CButton *buttons;
CRect *bPos;

buttons = new CButton[SIZE
bPos = new CRect[SIZE];

for(int i=0; i<SIZE; i++{
	bPos[i].SetRect(x1,y1,x2,y2);			//calculate the button position
	buttons[i].Create(TEXT, STYLE, bPos[i], WND, ID)	//create the button
}
Topic archived. No new replies allowed.