Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hallo, i am not sure where to start my code in C++ using winforms. There is no main() procedure, instead there is a WinMain() procedure, so where exactly to put my code and where not. Where is the loop?
I want to add;
while(1) {... call another method;}

C++
int WINAPI WinMain (HINSTANCE hThisInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR lpszArgument,
                     int nCmdShow)


HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */
//......
//......
/* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;
//.....
hwnd = CreateWindowEx (.....)

//.....
ShowWindow (hwnd, nCmdShow);
while (GetMessage (&messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}


C++
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                  /* handle the messages */
    {
        case WM_DESTROY:
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}


What I have tried:

--------------------------------------------------------
Posted
Updated 11-Jun-17 7:46am

Winforms isn't like a console app, there is a "main" proc, but it's not where you put most of your code (if any). And there isn't a loop in the sense you mean!

Instead it's job it to process Windows messages, and call the appropriate function when a particular message is received - and it "sits" in a loop forever getting messages and processing them - that is all it does. The actual work of you application is done in the functions that are called in response to messages being received (most of them generated by the user, so you are mostly reacting to user initiated events) Such events include the mouse moving, a mouse button being clicked, a keyboard key being pressed or released.

Don't just dive into this stuff: read your book or course notes - or if you don't have a book or course, get one - and it will explain what you do where. Windows coding is not the same as "traditional" procedural code where you ask the user what to do: instead you react to what the user does which is a much bigger difference than you might think!
 
Share this answer
 
Comments
[no name] 11-Jun-17 13:04pm    
I come from .NET world and there are procedures that work just simultaneously to the WinForm. For example the hidden Main() procedure which does start the WinForm aka Application.Start(New Form1). Because in this project of C++ i dont plan for now to implement anything just to test some I/O functionality while a Window just sits there.
See EFNet #Winprog[^] and Win32 Programming - FunctionX[^] for some useful tutorials.
 
Share this answer
 
Comments
[no name] 11-Jun-17 12:56pm    
Thanks for the links, i'm not planning to make real C++ WinForm apps, since i'm familiar with VB.NET and it's very easy with that. I just recently started with C++ and am curious how one would create a native Win32 app using WINAPI on which i can also have my functions like in the VB.NET or C# World.
Richard MacCutchan 12-Jun-17 3:35am    
Both of those links will teach you how to use the Windows API (not WinForms) in C/C++. The tutorial in the first link is a great starting point.
I ended up creating some events, im not sure if is the way to use in C++ but good for some testing.
bool onLoad;
void on_Load();

bool onClosing;
void on_Closing();


LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    if(onLoad == false)
    {
        onLoad = true;
        on_Load();
    }

    switch (message)                  /* handle the messages */
    {
        case WM_DESTROY:
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            onClosing = true;
            on_Closing();
            break;
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}


void on_Load()
{
    //FORMS HAS LOADED
    //do stuff
   ofstream myFile;
		myFile.open("C:\\Loading.txt", ios::out | ios::app);
		myFile << "Loading!";
		myFile.close();

}

void on_Closing()
{
    //FORM IS CLOSING
    //do stuff
    ofstream myFile;
		myFile.open("C:\\Closing.txt", ios::out | ios::app);
		myFile << "Closing!";
		myFile.close();
}
 
Share this answer
 
v2
Comments
Richard MacCutchan 12-Jun-17 3:37am    
What you have here is not really the way to do it. Go back and work through the tutorial I suggested.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900