Building Native Windows Applications with CLION, GCC and CygWin

As a software developer and architect, I’m always looking at options for my customers whether they’re around technologies, tools, hosting, security and tenancy model (being the last three mandatory in a “cloudy” world) among some other factors and considerations. To me crafting software it’s more than cracking code and ensure it compiles, it’s more about building smart solutions that will help people, as well as enjoying the building process. What really makes it enjoyable are two things (in my humble and personal opinion: First, what the challenge and requirements are;  Second, technology to use and lastly, the IDE).

In regards to points 2 & 3, I have to say that C++ is an awesome and powerful language and similarly to C, they both have been standard and platform independent since their very first versions, something that was achieved by Java and subsequently by .NET (and its Linux implementation, Mono for instance). So what’s all this introductory fuss about then? well… This post is about building a very simple native Windows application using non-Microsoft technologies:

Our source code Today is pretty straightforward, it’s classic Win32 development and if you’re like me and keen on learning how to do this kind of development without any frameworks, I can mention two books that helped me a lot when I was learning (They’re not recent or new though and the OS has changed a lot since the books were originally published – They are a relic, actually)

petzold Norton

The code and a screenshot of the application running are shown below

#include <windows.h> HINSTANCE hInst; const char g_szClassName[] = "bonafideideasWindowClass"; int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){ MSG Msg; HWND hwnd; WNDCLASSEX wc; wc.lpszMenuName = NULL; wc.hInstance = hInstance; wc.lpszClassName = g_szClassName; wc.cbSize = sizeof(WNDCLASSEX); wc.cbClsExtra = wc.cbWndExtra = 0; wc.style = CS_HREDRAW | CS_VREDRAW ; wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); wc.lpfnWndProc = [=](HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) -> LRESULT { HDC hdc; RECT rect; TEXTMETRIC tm; PAINTSTRUCT ps; switch(msg) { case WM_CLOSE: if (MessageBox(NULL, "Are you sure you want to quit?", "Confirmation", MB_ICONQUESTION | MB_YESNO) == IDYES) DestroyWindow(hwnd); break; case WM_DESTROY: PostQuitMessage(0); break; case WM_PAINT: hdc = BeginPaint (hwnd, &ps) ; GetClientRect (hwnd, &rect) ; DrawText (hdc, TEXT ("Native Windows Development with CygWin and CLion."), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER) ; EndPaint (hwnd, &ps) ; return 0 ; default: return DefWindowProc(hwnd, msg, wParam, lParam); } return 0; }; if(!RegisterClassEx(&wc)) { MessageBox(NULL, "Window Registration Failed", "Error", MB_ICONEXCLAMATION | MB_OK); return 0; } hwnd = CreateWindowEx( WS_EX_CLIENTEDGE, g_szClassName, "Simplest Windows Native App built with CLion", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 500, 250, NULL, NULL, hInstance, NULL); if(hwnd == NULL) { MessageBox(NULL, "Window Creation Failed", "Error", MB_ICONEXCLAMATION | MB_OK); return 0; } ShowWindow(hwnd, nCmdShow); UpdateWindow(hwnd); while(GetMessage(&Msg, NULL, 0, 0) > 0) { TranslateMessage(&Msg); DispatchMessage(&Msg); } return Msg.wParam; }

 

AppRunning

Now, let’s talk a bit of CLion… As previously mentioned it’s a cross-platform IDE (I’m writing a few things on Linux with it, actually).  It’s very similar to IntelliJ IDEA (Keep in mind it’s from the same company). The UI is easy to use and  functional (It’s not fair to compare to more mature products) and just to give you a glimpse

image

Besides providing with a good and decent Intellisense  support and fast parsing oh header files and libraries, good debugging experience and it also supports most of the new features in C++11 and C++14 Standards (This is because of using GCC as compiler).

image

Happy coding!,

Angel

6 thoughts on “Building Native Windows Applications with CLION, GCC and CygWin”

  1. Hi!

    Do you recommend learn Windows Development in this way (using this books you reference) ?

    Build native windows application without that massive visual studio installation?
    It’s portable among windows versions?

    Could be used another IDE (like Eclipse or even simple ones like Dev-C++ new version)?

    Could tell about pros or cons about in terms of what this learning help me to be better Windows developer and if makes a difference in curriculum?

    Thanks! Love find your post!

    1. Hi Sam,

      Thanks for your comments 🙂

      Please find my answers below

      1-. The way I learn is through reading and then practicing, I need to know how things hang together. Windows has evolved a lot throughout the years and the books I mentioned might not be a good source, but they’re still good books though.

      2-. Yes, you can build windows apps without VS. I also use Qt creator which is heaps lighter than VS. Qt SDK is portable across Windows, Linux and OS X.

      3-. Like I said before, Qt Creator is an excellent option

      4-. By knowing and understanding better the underlying platform (Windows) it will definitely make you a better developer. Most .NET developers take for granted some of the features they have thus they tend to forget the basics (like properly disposal of objects, in other words enclose in an using statement everything that’s IDisposable). As a Windows Developer, you can go beyond the CLR and pretty much use and leverage what the OS provides (that might not necessarily be exposed or surfaced to .NET) and it’s more fun (IMHO)

      Cheers,

      Angel

      1. Hi Angel! Changed your url quite a while?

        Never thanked you for your answers. Correcting now: Thanks for your attention!

        I appreciate too learn how things hang together like you say.

        Merry Christmas for you and your family!

        Til 2018 with all good for all us 🙂

  2. Hi,

    Days before I made a post here… but now it’s gone… ok….

    I ran your code on Visual Studio 2015 community. I was bit curious to see running.

    I fix some casts and lambda instruction.

    I posted here to share: http://pastebin.com/NE5AGxrS

    great post!

    see ya.

    Samuel

Leave a Reply

Your email address will not be published. Required fields are marked *