 |
Visual C++ Questions specific to Microsoft's Visual C++. For questions not specific to this Microsoft version, use the C++ Programming forum instead. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Visual C++ section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|

August 28th, 2003, 08:28 AM
|
Authorized User
|
|
Join Date: Jun 2003
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Dynamic Control array
Hi folks,
I have to write a C++ program that needs to display a different number of push button controls within a dialog window each time it runs. Using the following fonction, I am able to do this when I use a modeless dialog window.
But I want to use a standard modal dialog and I have trouble properly calling this function within my code. I could call it within the OnInitDialog() function, but that does not make the controls visible. I guess I need to repaint the window at some point but I don't know where and how. Can someone help me?
Here's the function I use to create the control array:
void AccessDlg::CreateButtons()
{
int i;
int y1 = 10;
int y2 = 30;
CWnd* hwndOwner;
CString StrLabel;
hwndOwner = this;
CButton MyButtons[10];
for(i=1; i<=4; i++)
{
StrLabel = i + ") Button #" + i;
MessageBox(StrLabel);
MyButtons[i].Create(StrLabel.GetBuffer(StrLabel.GetLength()), WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
CRect(10, y1, 100, y2), hwndOwner, i);
y1 = y1 + 20;
y2 = y2 + 20;
}
}
|

August 29th, 2003, 12:59 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 453
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Just put your CButton array into global scope, you can make that a member variable of the class.
Call this function in OnIinitDialog only.
Also have a look at the way you naming the
buttons. use Format function of CString something
like this StrLabel.Format("Button (%d)",i);
Ankur Verma
.Net and C++ Specialist
Wiley Tech Support
|

August 29th, 2003, 01:58 AM
|
Registered User
|
|
Join Date: Aug 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I dont know VC++ yet, I WANT TO LEARN VC++,I need an ADVICE, WHICH OR WHAT BOOKS WILL I PURCHASE FOR ME TO LEARN.
PLEASE I NEED HELP ON THIS.
-ian
|

August 30th, 2003, 02:01 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 453
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Beginning Visual C++ 6 ( Ivor Horton ) is one of the best books arround.
Check this link.
http://www.wrox.com/books/0764543881.shtml
Among other people a lot of guys in this very
forum have admired this book.
|

September 2nd, 2003, 09:39 AM
|
Authorized User
|
|
Join Date: Jun 2003
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Ankur,
yes it did work! This is perfect to create a fully dynamic dialog window. Now I need to find out how to detect button clicking for each of them.
Thanks for the help
Carl
|

September 3rd, 2003, 02:31 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 453
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Right click on the AccessDlg (assuming that is
the class thats implementing the modless dialog)
and click "Add Virtual Function...", from the
list that appears select DefWindowProc and click
"Add and Edit". Fill the function with this code
LRESULT AccessDlg::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
if(message == WM_COMMAND)
{
if(LOWORD(wParam) == 4)
{
if(HIWORD(wParam) == BN_CLICKED)
AfxMessageBox("CLICKED");
}
}
return CDialog::DefWindowProc(message, wParam, lParam);
}
This function is like the windproc and you can
catch any message here. I have caught the
BN_CLICKED message for button no. four of the
the four buttons that were dynamicaly created.
Extend this function as you please.
|

September 3rd, 2003, 08:48 AM
|
Authorized User
|
|
Join Date: Jun 2003
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Ankur,
Yes that function also worked! I'm getting there...
But I don't like doing things I don't really understand. Can you explain in a couple of words how and when to use virtual functions? Or maybe an article or book I could use?
I am getting pretty good at searching MSDN's library by now, but the explanations are pretty bad.
Regards,
Carl
|
|
 |