Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using class functions from another class??

Status
Not open for further replies.

gorgor

Programmer
Aug 15, 2002
164
US
I'm playing around with an MFC (dialog-based exe) project in MSVC++ 6.0 and I've run into a problem. I've spent 2 full days searching the web and haven't found an answer. It seems that it should be straight forward. Here goes:

Description of the overall project:
I created a project named paint. The main app instantiates an object called "dlg" (is this what happens?)of class CPaintDlg. It is a dialog box. I created a control class CPinButton that is derived from CButton (I added more functionality to the class) and I placed the control on the dialog. The CPaintDlg object instantiates a CButton object called "m_button1" (is this what happens?) of class CPinButton. There is a standard edit box control on the dialog box with a member variable "m_uiVariable" mapped to it. Now when I double-click the right mouse button on m_button1, I want to increment the member variable "m_uiVariable" and update the dialog box. I can do all this except I ran into the following problem:

Problem:
The class CPinButton has no know knowledge of CPaintDlg so CPinButton::OnRButtonDblClk(...) cannot increment m_uiVariable. It has no knowledge of the variable or any functions in CPaintDlg. I tried creating a pointer to the "dlg" object and a pointer to a function "CPaintDlg::UpdateVariable()" (the function increments the variable and updates the dialog box) and passing them to CPinButton. That didn't work (maybe I was just doing it wrong). I just can't seem to get any association between the two classes. Is there any way to do this? It should be easy, right??

I'm getting desperate. Any help or direction at all will be greatly appreciated!
 
You don't have to, you can, but you don't have to create a CButton derived class, you don't even need a CButton to do what you describe.

Just put the button on the dialog then handle the CLICK message for the button in the dialig class itself. The MFC framework provides all that mechanism for you. Then of course the dialog has access to it's own members. After placing the button on the dialog in the resource editor just double click the button which will open the Class Wizard and use that dialog to generate the message handler function and MFC plumbing for you in the dialog class.

I highly recommend using a tutorial or book that is targeted for beginners when starting out to use VC++ and MFC to develop Windows applications. It will go much faster using such a resource.

-pete
 
try defining "m_uiVariable" like this globally in your program.

extern <type> m_uiVariable

If Visual C++ created the variable for you. comment out that declaration of the variable and do yourself in this format globablly. I didn't have the same exact problem but I had something simular. I needed a class to be seen in many different &quot;c++&quot; files and I had to use the extern keyword before I declared the variable. My program needed it used like this:

extern CFileIO DebugFile

extern is a keyword.
CFileIO was my class name.
DebugFile was the new variable created.

if this doesn't help look for mor information ont he extern keyword. It should help out.



 
Yes it is usually quite easy.

I suggest you use the resource editor and classwizard to map GUI interaction (button click etc) to methods of the dlg class.

You can also let the classwizard create a button variable in the dialog class. You can then change the generated CButton member in the .h file to your own class.

Nerdy signatures are as lame as the inconsistent stardates of STTNG.
 
palbano & perfnurt...

When I add a button with class wizard, the only message handlers available for the standard button (CButton) are BN_CLICKED and BN_DOUBLECLICKED. I require all possible messages handlers for the button itself. Sure, if I want to 'trigger' the doublerightclick even on ANY double right click in any area of the dialog, then I could map the respective message handler to the CPaintDlg ID and go from there. Am I way off? I've read a couple C++ books that covered MFC and I haven't come across anything that gave examples for what I'm trying to do. Thanks for your patience.
 
Yeah, your correct, i missed the [red]right[/red] mouse button part... sorry.

Go to MSDN and read articles TN061 abd TN062 about Windows Controls and Message Reflection. That should at least get you started. Sorry for the bum steer, have a beer on me [cheers]

-pete
 
palbano,

Those articles look pretty promising. Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top