Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > Visual C++
|
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
 
Old November 13th, 2004, 06:26 PM
Registered User
 
Join Date: Nov 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Executing main() from a control btn

Hi,
I'm a C++ newbie and have a liitle problem I hope someone can help me with here.

I have a Visual C++ project consisting of about 20 source files, one of which contains main(). I originally built a dialog-based application, and have no menus in the application. I am trying to run main(), by clicking on a Control button, "Analyze", inside the OnAnalyze() function.

From my understanding, main() is supposed to harness all my other source files and implement it. Now, I need to run main() from a control button, in my MFC application. The project compiles with no errors at present but I don't know how to run main() with the control button.

Or would it may be necessary to implement an instance of the control in main()? or since I'm working with Visual C++ 6.0, would I need to modify WinMain()? Am I asking the right question here?

Thanks a lot in advance!
 
Old November 18th, 2004, 06:09 AM
Authorized User
 
Join Date: Jul 2003
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Am I asking the right question here?
Nope. There is no main() function in a Visual C++ MFC program. "Windows programs" do not work the same way as a normal C++ console program. A console program has a main() function which is where execution begins, and execution proceeds sequentially down the code.

A "windows program" does not work that way. With a windows program using MFC, a code framework is provided for you, and that framework is organized completely differently than a console program. Essentially what you do is write code to respond to 'events' caused by the user of the program. The user’s actions, like moving the mouse and clicking on menu items, are called events, and the provided framework routes windows messages created by those events to the proper areas of the code, and your task is to write code to intercept the messages you are interested in, and to take some kind of action as a response.

Quote:
quote:I originally built a dialog-based application, and have no menus in the application. I am trying to run main(), by clicking on a Control button, "Analyze", inside the OnAnalyze() function.
If you used the App Wizard to add an OnAnalyze() function to the Analyze button's BN_CLICKED message, then you should think about what you want to happen when the user clicks on that button. You need to write the code to do whatever that is inside the OnAnalyze() function,. You do not have to call main() or anything else. After you build and then execute the program, when you click on the Analyze button, the MFC framework provided for you will call your OnAnalyze() function. It does that in response to a the BN_CLICKED message generated by the MFC framework when you clicked on the button. Then, the code you wrote inside the OnAnalyze() function will be executed.

If you think about a windows program that you have used like Notepad or Word, once you launch the program it doesn’t do anything—it just sits there waiting for some action on your part. Once you take some action like clicking on File>New or File>Open, the program springs into action. Your windows program with MFC is similar. When you execute your program, it will display your dialog box, and then it will sit there and do nothing until you take some action. When you click on the Analyze button, the MFC framework creates a BN_CLICKED message, the MFC framework notices that you attached a function to that message, so it executes your function.

Quote:
quote:I have a Visual C++ project consisting of about 20 source files, one of which contains main(). I originally built a dialog-based application, and have no menus in the application.
Once you create a dialog based application you can't switch to a console type program with a main() function.


 
Old November 18th, 2004, 06:15 AM
Authorized User
 
Join Date: Jul 2003
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well, I see you can't edit posts here, which I guess is one of the continuing reasons not to frequent the Wrox forums. If you need help with Visual C++ go to this very active forum and your questions will be answered within the hour:

http://www.codeguru.com/forum/forumdisplay.php?f=7

 
Old November 19th, 2004, 06:23 PM
Registered User
 
Join Date: Nov 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks 7Stud for the response.

I already took out main(), since like you said, I run a Windows, not a Console Application. Having transfered the code to OnAnalyze() however, it actually created the output file, "Output.txt", with a heading "Pressure", but just one line of Pressure, from the command:
-> outputdeck<<press<<endl;

i tried changing the command to:

outputdeck<<press;

but i still get only one Pressure value, which is "0".

I'm thinking either of 2 things:

1) My code to generate Pressure Data, called "press" is incorrect somewhere; [ see external declarations]
2) the statement -> outputdeck<<press; is incorrect: i actually expect to see a stream of data, based on my earlier statements, and the size of the input file, input.txt.

Below is the new code in OnAnalyze() [ i got rid of the instance of the class CKuchukDlg, initialized num_layers and changed my line counter: int i = 0], but still get one pressure output, "0"....

void CKuchukDlg::OnAnalyze()
{
// let maximum number of layers = 100: this value can be modified.
int num_layers = 100;

int i = 0; // index to count what line is being read;
int H_ft[100],kh_md[100], kv_md[100];
float visc_cp[100], por[100], ct_psi[100];

char line[100];


ifstream inputdeck;
inputdeck.open("input.txt", ios::in);

if (inputdeck.fail())
{
//cout<<"Error With Files!";
AfxMessageBox("File Read Error!", MB_ICONSTOP);
}

else
{
cin.ignore(60, '\n');
cin.ignore(60, '\n');

while (i <= num_layers && cin.getline(line,100)) //
{
cin>>H_ft[i]>>kh_md[i]>>kv_md[i]>>visc_cp[i]>>por[i]>>ct_psi[i];
i++;

} /* while block */
}


ofstream outputdeck;
outputdeck.open("output.txt", ios:ut);

outputdeck<<"Pressure\n";

extern double pressure(double x);
extern double press;

outputdeck<<press;
AfxMessageBox("Output file Generated!", MB_ICONINFORMATION|MB_OK);
}


Would I be correct in assuming my problem would be (1) or (2) above? I'd appreciate anyone's response.

Thanks a lot!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Populate command btn Captions MattFinn7 Access VBA 2 September 17th, 2008 03:12 PM
2 Radio btn with one Submit btn, convert to 2 Btn ismailc XSLT 2 August 15th, 2008 07:00 AM
ambigious default2 and btn event does not fire shoakat ASP.NET 2.0 Professional 1 June 19th, 2007 10:21 PM
SP executing time on progressBar control filipczako ADO.NET 0 November 29th, 2005 06:48 PM
Running main() from a dialog-based control aaadetos C++ Programming 1 December 4th, 2004 12:48 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.