Page 1 of 1

New executable Windows API GUI project.

Posted: Wed Jun 12, 2013 1:39 am
by Prostoy Paren
I'm learning coding (it's my hobby), and want to make a GUI project using native Microsoft Windows API ( Windows 7 and CodeLite 5.1 ). I already can make a simple console application in CodeLite 5.1, but how to start a new executable Windows API project without wxWidgets, GTKMM, GTK, Qt, etc?
Some time ago I've did a simple Windows GUI program in assembler and want to use this knowledge.

Prostoy Paren

Re: New executable Windows API GUI project.

Posted: Wed Jun 12, 2013 9:33 am
by eranif
I have no knowledge of writing native GUI applications without a helper toolkit (question: why not using Qt/wx/GTK etc?)
it seems much more reasonable to use a framework instead of using the native API (note that wx is a thin wrapper on top of the native API) - you will also get the your application running on various platforms...

Anyways, to your problem:
- There is no template for Win Native GUI application.

However, its quite simple to create one:

1) Workspace -> New Project -> Console -> g++ executable
2) Right click on the project -> Settings -> Common Settings -> Linker -> Options, and add

Code: Select all

-mwindows
3) Project Settings -> Common Settings -> General, and uncheck the option 'Pause when execution ends'
4) Copy a sample from one of the many tutorials out there and replace the content of main.cpp, here is the sample code I used:

Code: Select all

#include <windows.h>

const char g_szClassName[] = "myWindowClass";

// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg) {
    case WM_CLOSE:
        DestroyWindow(hwnd);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;

    //Step 1: Registering the Window Class
    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = 0;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = g_szClassName;
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

    if(!RegisterClassEx(&wc)) {
        MessageBox(NULL, "Window Registration Failed!", "Error!",
                   MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    // Step 2: Creating the Window
    hwnd = CreateWindowEx(
               WS_EX_CLIENTEDGE,
               g_szClassName,
               "The title of my window",
               WS_OVERLAPPEDWINDOW,
               CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
               NULL, NULL, hInstance, NULL);

    if(hwnd == NULL) {
        MessageBox(NULL, "Window Creation Failed!", "Error!",
                   MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    // Step 3: The Message Loop
    while(GetMessage(&Msg, NULL, 0, 0) > 0) {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return Msg.wParam;
}
Compile and run it

Eran

Re: New executable Windows API GUI project.

Posted: Wed Jun 12, 2013 9:13 pm
by Prostoy Paren
It works.
Thank you!

May be later I'll read and learn about wx and will use it.
Sorry for my English. My native language is Russian. ;)

Prostoy Paren

Re: New executable Windows API GUI project.

Posted: Thu Jan 30, 2014 9:07 am
by programmerc++23
Hi Proston Parey. I am also in your status. I want to do that. My question is..
Did you always need to do the settings Eran said, so you can use win32 programming ?