where's main() ???
-
Having just finished up a C++ programming class, I'm making the move to MFC and Windows programming. I expected to find something (at least roughly) equivalent to the main() routine found in console apps. After looking through numerous books, I still don't know where my application code goes. I know how to do initialization (e.g. the OnDraw function) but I don't know where I would put some code to make GDI calls. I don't want to respond to messages (at least yet), I just want to make some GDI calls in my main program. Does someone know where the equivalent of main() is for MFC programs? Thanks. BTW, I'm not using AppWizard. Dave
-
Having just finished up a C++ programming class, I'm making the move to MFC and Windows programming. I expected to find something (at least roughly) equivalent to the main() routine found in console apps. After looking through numerous books, I still don't know where my application code goes. I know how to do initialization (e.g. the OnDraw function) but I don't know where I would put some code to make GDI calls. I don't want to respond to messages (at least yet), I just want to make some GDI calls in my main program. Does someone know where the equivalent of main() is for MFC programs? Thanks. BTW, I'm not using AppWizard. Dave
If you're using Win32 calls (instead of MFC), your Windows main() entry point is
WinMain()
. If you're using MFC, AppWizard will generate the application instance for you. The framework calls methods in your application, allowing you to implement/customize the app's startup. The real main() function is buried in the Windows startup stub that gets linked into your app. > BTW, I'm not using AppWizard. Aieee! I would strongly advise using the AppWizard. :eek: /ravi Let's put "civil" back in "civilization" Home | Articles | Freeware | Music ravib@ravib.com -
If you're using Win32 calls (instead of MFC), your Windows main() entry point is
WinMain()
. If you're using MFC, AppWizard will generate the application instance for you. The framework calls methods in your application, allowing you to implement/customize the app's startup. The real main() function is buried in the Windows startup stub that gets linked into your app. > BTW, I'm not using AppWizard. Aieee! I would strongly advise using the AppWizard. :eek: /ravi Let's put "civil" back in "civilization" Home | Articles | Freeware | Music ravib@ravib.comRavi, Thanks for your input. Sounds like I should use AppWizard. So I will. But I still don't know where my application code goes. What are the "application instances"? What is the Windows startup stub? Can you help me with the names of the classes that I should be looking at? Are there some methods where I would insert my application code? Thanks again. Dave Dave
-
Ravi, Thanks for your input. Sounds like I should use AppWizard. So I will. But I still don't know where my application code goes. What are the "application instances"? What is the Windows startup stub? Can you help me with the names of the classes that I should be looking at? Are there some methods where I would insert my application code? Thanks again. Dave Dave
The class you want to look at is the one AppWizard derives from
CWinApp
, specifically theInitInstance()
method. AppWizard peppers its generated code with comments which are reasonable helpful and will help you learn more about MFC's application framework. I also suggest you look at some books on MFC/Windows programming to help you get started. I think you'll find the ones by Charles Petzold and Jeff Prosise to be a great read. You can likely buy them used for very little on EBay or Amazon. And continue to post your Windows questions at CP! /ravi Let's put "civil" back in "civilization" Home | Articles | Freeware | Music ravib@ravib.com -
The class you want to look at is the one AppWizard derives from
CWinApp
, specifically theInitInstance()
method. AppWizard peppers its generated code with comments which are reasonable helpful and will help you learn more about MFC's application framework. I also suggest you look at some books on MFC/Windows programming to help you get started. I think you'll find the ones by Charles Petzold and Jeff Prosise to be a great read. You can likely buy them used for very little on EBay or Amazon. And continue to post your Windows questions at CP! /ravi Let's put "civil" back in "civilization" Home | Articles | Freeware | Music ravib@ravib.comRavi, I have 4 books on Visual C++ 6.0 and/or MFC programming but not the ones you mention, which I'll pursue getting. I used AppWizard to create my program and looked at InitInstance() and it seems to be only for initialization. The comment in the code says "Standard initialization". So, I still don't know where my application code goes. As noted above, I'll pursue getting the books but is it possible, in the meantime, to let me know where my "regular application code" goes -- not the initialization code but the code that will run after initialization? My "regular application code" will display graphics on an ongoing and continuous basis, I just don't know where to put it. Can you let me know? Thanks.
-
Ravi, I have 4 books on Visual C++ 6.0 and/or MFC programming but not the ones you mention, which I'll pursue getting. I used AppWizard to create my program and looked at InitInstance() and it seems to be only for initialization. The comment in the code says "Standard initialization". So, I still don't know where my application code goes. As noted above, I'll pursue getting the books but is it possible, in the meantime, to let me know where my "regular application code" goes -- not the initialization code but the code that will run after initialization? My "regular application code" will display graphics on an ongoing and continuous basis, I just don't know where to put it. Can you let me know? Thanks.
A Windows app that exposes a user interface requires a main window, and optionally various child windows depending on the type of interface (dialog, SD, MDI, etc.). For your purpose, it seems that a dialog based app will suffice. The graphics will be displayed within the dialog box. When you run AppWizard (to create an MFC Windows Application), select "Dialog Based" at the appropriate step. When you build and run your app, a "main dialog window" will be displayed. If you click Esc (or the little x a the top right corner), the dialog will exit, control will eventually return to your CWinApp derived class, and your app will exit. The actual displaying of graphics will likely occur within a custom control (chart, graph, whatever) you may choose to implement. This control will reside within the dialog. You can create a timer to update the control every "n" units of time. You may also want to add buttons and other widgets on the dialog to allow the user to control the app. Hope this sheds some more light on "the big picture". /ravi Let's put "civil" back in "civilization" Home | Articles | Freeware | Music ravib@ravib.com
-
A Windows app that exposes a user interface requires a main window, and optionally various child windows depending on the type of interface (dialog, SD, MDI, etc.). For your purpose, it seems that a dialog based app will suffice. The graphics will be displayed within the dialog box. When you run AppWizard (to create an MFC Windows Application), select "Dialog Based" at the appropriate step. When you build and run your app, a "main dialog window" will be displayed. If you click Esc (or the little x a the top right corner), the dialog will exit, control will eventually return to your CWinApp derived class, and your app will exit. The actual displaying of graphics will likely occur within a custom control (chart, graph, whatever) you may choose to implement. This control will reside within the dialog. You can create a timer to update the control every "n" units of time. You may also want to add buttons and other widgets on the dialog to allow the user to control the app. Hope this sheds some more light on "the big picture". /ravi Let's put "civil" back in "civilization" Home | Articles | Freeware | Music ravib@ravib.com
Ravi, Thanks for the further tip. I used AppWizard to create a dialog application. So, now, I have the same question: Where does my application code go? You mention that the displaying of graphics will occur within a "custom control". I don't know that this is -- I think what I'm looking for is a function (method) that gets called to run my code. My goal (for now) is to draw a circle in the dialog window. I want to do this is my main code section, *not* in the initialization InitInstance() method. Can you let me know where I would put this circle code? I presume that my application code goes in a well-defined and well-known location in the dialog window code, I'm just trying to find out what this location is. Can you let me know? Thanks again, sorry for not "getting it" quicker.
-
Ravi, Thanks for the further tip. I used AppWizard to create a dialog application. So, now, I have the same question: Where does my application code go? You mention that the displaying of graphics will occur within a "custom control". I don't know that this is -- I think what I'm looking for is a function (method) that gets called to run my code. My goal (for now) is to draw a circle in the dialog window. I want to do this is my main code section, *not* in the initialization InitInstance() method. Can you let me know where I would put this circle code? I presume that my application code goes in a well-defined and well-known location in the dialog window code, I'm just trying to find out what this location is. Can you let me know? Thanks again, sorry for not "getting it" quicker.
For a GUI application you need to think in terms of what you do to react to user input, not the classical main approach. As such, do not think of your dialog application as having a main code section - your application responds to messages from the Windows message system, and it is within the handlers for these messages that your application code is distributed as appropriate. In the case of drawing a circle in the window, then you would put that code within the handler for the WM_PAINT message, for example, which is called whenever the window needs to be redrawn. If you bring up the ClassWizard then it allows you to define a whole host of these functions to respond to most things. To stress, dont think of it in terms of having a main code section - Windows handles all that, you just respond to its messages.
-
Ravi, Thanks for the further tip. I used AppWizard to create a dialog application. So, now, I have the same question: Where does my application code go? You mention that the displaying of graphics will occur within a "custom control". I don't know that this is -- I think what I'm looking for is a function (method) that gets called to run my code. My goal (for now) is to draw a circle in the dialog window. I want to do this is my main code section, *not* in the initialization InitInstance() method. Can you let me know where I would put this circle code? I presume that my application code goes in a well-defined and well-known location in the dialog window code, I'm just trying to find out what this location is. Can you let me know? Thanks again, sorry for not "getting it" quicker.
Johnny is exactly right. Windows (like any event driven GUI) requires your application to respond to events. Windows notifies your application when an event occurs by sending messages to its windows. Each message is identified by a
WM_something
constant. Examples of messages are:WM_SIZE
the user has just resized the dialogWM_MOVE
the user has moved the dialogWM_QUERYENDSESSION
Windows is shutting down and would like to terminate your applicationWM_PAINT
the user has moved a window, causing (part of) your window to need to be redrawn
MFC conveniently provides predefined methods (that you can override) to customize your response to these messages. The one you probably want to handle is the last one. The handler for this message is
OnPaint()
. My gut tells me you might be better off doing some reading before venturing into building your app. Once you get a better handle (pun intended) on MFC, you can decide where to inject your application's drawing code and perhaps add more bells and whistles to your app. Good luck! /ravi Let's put "civil" back in "civilization" Home | Articles | Freeware | Music ravib@ravib.com