A really rooky question here but I'm still trying to understand something.
If I'm using WXWidgets I assume the purpose of the GUI is to provide a trigger point from where I can process event s via the event loop. I basically create the gui as I want it and open it.

When building an applicaiton, where does the 'MAIN' function come into this. Is MAIN processed as part of an even. For example if I click on button1 (just an example of a control on the GUI), it processes an even and directs the event to carry out instructions as per a function defined somewhere else. This seems straight forward, but where does 'MAIN' come into this and how do I call 'MAIN'? .... I told you this is a rooky question.

I'm having a dilemma with the very basics here. Just wondering how this would work.
I have a long time to work this out so my relative inexperience won't matter in the long run.

Thanks so much.

4 Years
Discussion Span
Last Post by daino

Most GUI tools will hide away the main() function, because you don't have to temper with it, shouldn't need to, and probably shouldn't, period. But if you want to know what it looks like or what it does, out of curiosity, I can tell you.

Typically, a GUI application is made up of two things: a main() function that creates the window, and an event-loop to handle messages relevant to that window. In Windows, the main() function is called WinMain() which replaces the main() function when compiling a Win32 application (as opposed to a "console" application). Usually, this function simply creates the window handle, attaches an event-processing function to it (i.e., a function pointer), starts the "execution" of the window (i.e., the window shows up and events start to get handled), and then it waits for it to be done (after you close the window). Usually, the WinMain() functions generated by typical GUI tools (like MFC, Qt, wxWidget, VCL, etc.) contain less than 10 lines of code. For example, this is a Qt main() function:

#include <QApplication>
#include <QTextEdit>

int main(int argv, char **args)
  QApplication app(argv, args);

  QTextEdit textEdit;

  return app.exec();

The event processing is done via a call-back function, conventionally called WndProc(), which is registered to the OS as being the call-back for the created window handle. Basically, when you click anywhere on a window or push a key while in focus of that window, the OS creates an event message that it feeds to a message queue associated to the window in question. This message can be caught or ignored by the WndProc function, depending on how you code it (assuming you would code it yourself). The WndProc function usually involves a long series of switch-cases for every type of message that could come in. Mostly, all this crap is handled by the GUI tool, and that's why it is highly recommended that you use those instead. For a small tutorial on win32 API with the WinMain and WndProc functions, see this link.

The GUI library code will filter the messages (events) and will dispatch the event to the correct call-back function. In other words, it checks the position of the mouse or which GUI component is currently under focus, and dispatches the "click" or "key-stroke" events accordingly.

Under Linux or Mac, the process under the hood is very similar, except that instead of events coming from the OS to the call-back, it comes from the X-server through the X-window system. But the basic mechanisms are the same. And again, you shouldn't use those directly, there is no point.

When you do GUI programming with a GUI tool, you don't have to care about the main() function or the underlying event-handling mechanism. What you do, after having designed the layout of the GUI, is to program event-handlers. These are just functions that get executed when a particular event happens (a click on a button, minimizing/maximizing a window, an edit to a text-box, etc.). Any initialization or finalization code that you need to do can usually be done in the "OnCreate", "OnShow", "OnClose", and "OnDestroy" events on the main window (or "form"). So, there is very little reason to go into the main() function, that's why it is often hidden or automatically (and temporarily) generated.


GUI means Graphical User Interface. It has nothing to do with triggering anything. It's all the pretty stuff in windows, like title bars, sizable windows, arrow cursor, menus, icons -- all the stuff people think a computer must have.

IMO, if you're such a rookie, you don't need to understand anything you currently asked. That comes later when you understand the language itself well. Then you can delve into this advanced stuff.

Edited by WaltP


Thanks very much Mike. Thats a great explanation and has certainly cleared up my understanding of this area.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.