Click to See Complete Forum and Search --> : Problems with Dynamic Button


October 7th, 1999, 02:02 PM
Hi,

I cannot get my button to Display. I am creating the Button in the OnInitialUpdate funtion of a CFormView Class. I looked through code and I cannot get this button to display. I get a nozero return value, so I know it is created correctly. any ideas??



//in .h file
CButton *ctrlCancelButton;
//in .cpp file
ctrlCancelBtn = new CButton;
RECT rect;
CString msg;
BOOL b;

rect.left = 210;
rect.top = 14;
rect.right = 60;
rect.bottom = 15;

b = ctrlCancelBtn->Create("Cancel", WS_CHILD |
WS_VISIBLE , rect,this,
ID_FIND);

msg.Format("Value of Creation %d", b);
AfxMessageBox(msg);

bandukoa
October 7th, 1999, 03:35 PM
First of all, your dimensions look suspicious. Do you intend your button's height to be 1 pixel (bottom - top)? Why is your right coordinate is less than left coordinate? Assuming that upper left corner of the screen has coordinates (0, 0) this does not make much sense.

After you fix your button dimensions try ScreenToClient function to adjust coordinates relative to your window like:

ScreenToClient(&rect);

and then call Create.

October 8th, 1999, 09:42 AM
When I was creating the size of the button, I looked at the resource file the coodinates were as follows:
210, 140, 60,15. I assumed it was left, top, bottom, right. Or is it suppose to be the other way around??

bandukoa
October 8th, 1999, 10:27 AM
I am not really sure, but it looks like coordinates in the resource file include width and height instead of bottom and right.

October 8th, 1999, 11:49 AM
I have the code working, but it is not displaying the same as if I created it in a dialog box. The position of the button is not the same either. Do you know if the mapping mode changes when you are in a dialog/resource??

bandukoa
October 8th, 1999, 12:16 PM
I don't really know internals of how it works. I would suggest you to do the following. Create a static button exactly where you want it to appear in the dialog box. Add a function (for example, responding to the button click) that would print coordinates of the button. Use these coordinates, when you create your button dynamically. You can use GetWindowRect functions to get coordinates (it must be called like pButton->GetWindowRect(&rect)). DO NOT FORGET TO USE ScreenToClient FUNCTION TO CONVERT COORDINATES WHEN IT IS NEEDED (you definitely need it when reading coordinates of the static button; not sure whether you need it when you create a button dynamically). This function will adjust screen coordinates relative to your dialog. I haven't done it in a while so I can't tell you exactly how to use it, so try to experiment. Good luck.