Calling main() from WinMain()

Andrej Mitrovic andrej.mitrovich at gmail.com
Tue Jun 5 09:41:12 PDT 2012


On 6/5/12, BLM768 <blm768 at gmail.com> wrote:
> I'm working on a cross-platform GUI library and I'd like to be
> able to call the user's main() function from WinMain() so users
> don't have to write both functions.

GUI libraries don't have to work this way. You could instantiate an
"App" and then call some internal "run" method which starts an event
loop which reacts to events retrieved from the OS, e.g. (pseudocode):

class MyApp : App
{
    this()
    {
        this.size = Size(400, 400);
        this.position = Point(600, 400);

        Widget mainWidget = new Widget(128, 128);
        connect(mainWidget, Event.Move, &onMove);
        this.addWidget(mainWidget);
    }

    void onMove(Widget src, Point loc) { }
}

void main(s)
{
    auto app = new MyApp();
    app.run();
}

So after calling run() the GUI library keeps processing OS events
until maybe the last window is closed, and then other statements are
executed in main. You don't really need a WinMain() function to call
OS GUI functions like GDI. For Windows you need to register a WNDCLASS
and a WndProc (a dispatch function which processes OS events), create
a window and then use an event loop like "while (GetMessage(&msg,
NULL, 0, 0))".


More information about the Digitalmars-d-learn mailing list