Author Topic: How to build a runnable Windows application  (Read 1553 times)

Offline exit0

  • Single posting newcomer
  • *
  • Posts: 5
How to build a runnable Windows application
« on: February 17, 2023, 08:33:33 am »
Hi! I'm new to Code::Blocks and also new to Windows programming. I've got some programming expirience with other languages and plattforms, mostly scripting. I've insalled Codeblocks IDE bundled with MinGW on my Windows 10 PC so learn how to build Windows GUI applications. I follow a video tutorial and could create a very simply program creating a window with a menu.

I can build this in the IDE with a debug target and it looks good. For Debug i choosed "Console application" as it will also open an extra console window which i can use to printf() debug informations out very simple.

Next i'd like to build my App with the "release" target but this currently failed. When i build it as "console application" the EXE only opens up the empty console window but not the main window. When i change to "windows application" in the project build settings, it does not open a window at all but just stays in the processlist.
I've also checked the static linking of libs to ensure everybody could run the resulting EXE later on.

What do i do wrong?

Code
#include <windows.h>
#include <stdio.h>

#define FILE_MENU_EXIT 1
#define FILE_MENU_INTERFACE 2

// Prototypes
LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);
void AddMenus(HWND);

HMENU hMenu;

// MAIN
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR args, int ncmdshow)
{
    WNDCLASSW wc = {0};
    wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
    wc.hCursor = LoadCursor(NULL,IDC_ARROW);
    wc.hInstance = hInst;
    wc.lpszClassName = L"myWindowClass";
    wc.lpfnWndProc = WindowProcedure;
    if ( ! RegisterClassW(&wc))
        return -1;

//    MessageBox(NULL, "Hello World!", "My first GUI", MB_OK);

    CreateWindowW(L"myWindowClass", L"My Window", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100, 200, 640, 480, NULL, NULL, NULL, NULL);

    MSG msg = {0};
    while(GetMessage(&msg, NULL, NULL, NULL))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return 0;
}

LRESULT CALLBACK WindowProcedure(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
{
    switch (msg)
    {
    case WM_COMMAND:
        switch(wp)
        {
        case FILE_MENU_INTERFACE:
            printf("MENU INTERFACE CLICKED\n");
            break;
        case FILE_MENU_EXIT:
            DestroyWindow(hWnd);
            break;
        }
        break;
    case WM_CREATE:
        AddMenus(hWnd);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProcW(hWnd,msg,wp,lp);
    }
}

void AddMenus(HWND hWnd)
{
    hMenu = CreateMenu();
    HMENU hFileMenu = CreateMenu();

    AppendMenu(hFileMenu, MF_STRING, FILE_MENU_INTERFACE, "Interface");
    AppendMenu(hFileMenu, MF_SEPARATOR, NULL, NULL);
    AppendMenu(hFileMenu, MF_STRING, FILE_MENU_EXIT, "Exit");

    AppendMenu(hMenu, MF_POPUP, (UINT_PTR)hFileMenu, "File");
    AppendMenu(hMenu, MF_STRING, NULL, "Help");

    SetMenu(hWnd, hMenu);
}


Offline Miguel Gimenez

  • Developer
  • Lives here!
  • *****
  • Posts: 1564
Re: How to build a runnable Windows application
« Reply #1 on: February 17, 2023, 09:01:17 am »
Try creating a new Win32GUI project (File -> New -> Project).