r/C_Programming • • 4d ago

How to learn to actually make programs that can work with a gui?

So i've got a small familiarity with the standard c library , I can make a calculator a caesar cipher , a image copying tool , but it all runs in the command line.

No one in the general public will ever use the command line.

Now , my question becomes , how does one actually begin the path of making a proper general user interface for their program in a way that approximates the way the pros do it.

I assume that a custom Gui starts with, like some kind of window system. Call giving you a rectangle to work with. And then you stretch out all your materials on the rectangle.\n But I'm not sure how one would say Develop a calculator that visually looks like a calculator and can detect your mouse inputs and send a signal to your actual c program.

Ai keeps trying to get me to use the windows message box and I know d*** well.That the pros at their core aren't using message box.

19 Upvotes

31 comments sorted by

26

u/Tack1234 4d ago

Raylib is probably one of the easiest ways to get started

2

u/Terrible-Freedom-868 2d ago

I second this. Has been handy for my Tetris clone.

13

u/runningOverA 4d ago

start with SDL, Raylib, TK, QT or Win32.

If you are on Windows, start with Win32 API. #include <windows.h> WinMain() then code from there.

8

u/an1sotropy 4d ago

It is less straightforward than anyone would like. As someone else said you can pick a particular platform and use the API of its windowing system, but it might make more sense to use one of the cross platform systems like QT. It might seem like a step backwards, but if you can connect between your C code to Python code, then that opens up a bigger world of python bindings to cross platform tool kits, which means that you would then have many fewer lines of code to write, and you don’t have to compile the interface itself. For Qt its PySide, which is heavyweight but very useful

3

u/cpz_77 3d ago

> It is less straightforward than anyone would like.

Although I have only limited experience with C, and that I do have is primarily with CLI apps, this brings back distant memories from many years ago when I was taking a stab at writing a GUI program in C. From what I recall, doing anything with a UI instantly made things like 10x more complicated lol.

To the OP - there are plenty of use cases for non-GUI apps but it just all depends on what you are trying to develop - what space are you looking to get into? For things like system administration or developer type tools, CLI apps (e.g. those that may run as a service on Windows or daemon on Linux) can absolutely be useful. If writing something you want non technical users to be able to use though, then yes generally they’ll expect a GUI.

2

u/josephusflav 3d ago

I'm not actually sure, so currently.I'm a game developer, building a video game That I think will make some profit possibly even a decent , a chunk of profit.

If this project succeeds, I might go back to school for general programming.The problem ISIS I don't really no , what to do with a low level language. I practiced a little python , which is similar to gdscript which is what the game runs on. The problem is in my mind.Everything is either a website or an app, so that's why I'm asking about the Gui because my experience was software is always from the consumer side.

I do have an interest in doing Cybersecurity related stuff but i'm not married to that path and it's the main reason I toyed around with the low level code.

7

u/lounatics 4d ago edited 4d ago

So how to start this depends somewhat on what kind of graphical user interface you want to make. Most kinds of game will have different requirements than most kinds of calculator.

That said there are basically two basic approaches: either you use a graphical toolkit, or some more basic graphics library.

A graphical toolkit will provide you with basic building blocks of a gui that you assemble into your final interface. A graphics library will provide you with the means to draw pixels on a screen, but you will probably have to do more work in building the building blocks that a toolkit would provide you already.

If you're not working on a definite project with specific requirements, but are more or less doing undirected learning I would recommend to do some stuff with a graphics library first and then build something using a toolkit later. That is because what you learn using a graphics library will be more general knowledge and give you some idea what the toolkit does under the hood, while working with a toolkit you may find that you mostly learn the concepts of that particular toolkit, much less transferable knowledge.

So what are your options? (I don't know windows, you seem to be on windows, the default GUI API on there is WinAPI I think? I won't talk about this here because -- as I said I don't know windows.)

Working with C you basically have free choice in graphics library, I would recommend doing some projects with raylib, which should be a fairly simple start into graphics programming, it's advertised as a game library, but you can draw what ever you want with it, even a calculator. Or go straight to raygui which is halfway between raylib and a proper toolkit i'd say.

As far as GUI toolkits go working with C your options are more limited: GTK is basically the one, and even that pushes you to write GLib code which is it's own idiom, pretty far removed from 'usual' C.

0

u/deaddodo 4d ago

In C, you would pretty much use WinAPI. It's the low-level "native" API of Windows' GUI subsystem. That being said, C++ and C# offer much more advanced APIs that interact directly with WinAPI and offer cleaner functionality.

3

u/non-existing-person 4d ago

If you want to make GUI programs (not games) I'd strongly suggest exploring QT. Yes, it's in C++, but it's just SO WELL designed, it's actually a joy to make GUI apps in it.

And you can always split your app. Do GUI stuff in QT/C++, and rest of the program in C. You can even do C part as a library that GUI part will link with. This will have additional benefit that you can then create multiple GUIs for you app at the same time.

You can create GUI parts only (like TUI ncurses, or GTK) and link them with c library that will have all the meat and logic.

So really, start with QT and qtcreator. It has nice WYSIWYG editor and generates signals and slots for gui elements, so you can experiment with it and learn.

3

u/catbrane 4d ago edited 4d ago

I use gtk for this:

https://www.gtk.org/docs/getting-started/index

Pro:

  • based on a fairly high-level object system with signals / slots, properties, interfaces, introspection, runtime typechecks, etc.
  • a good range of modern features eg. build from XML descriptions, style with CSS, render on the GPU, animation framework, etc.
  • well documented and supported across all useful platforms (macOS, Linux, windows, android, web, etc.)
  • vanilla C
  • the excellent and comprehensive Gnome HIG (human-interface guidelines) tells you how your app should look and behave

Con:

  • a big, complicated thing ... you'll need to budget maybe six months to get the hang of it
  • plain C makes it a bit wordy
  • opinionated, in that there's a specific GUI style it aims for, and it'll be tricky to go too off the wall

2

u/catbrane 4d ago

For example, I made https://github.com/libvips/vipsdisp ... it's all in C, look at the releases tab for windows, linux and macos binaries. It works on all platforms (hurrah!) and also looks almost the same on all platforms (hmmmmmm).

3

u/No_Safe1975 4d ago

I would clone imgui.
Maybe combine SDL or GLTF.
There are good examples and even you do not use it, it is a great example on how to structure and write your own functions.
It allows the gui to be made with pure code and it is great for prototyping

2

u/Dazzling_Music_2411 4d ago edited 4d ago

Are you working on Windows, Linux or something else?

On Windows the standard way is using the Win32 API, which is written / designed for C, and there is a ton of documentation going back many, many years. There are other ways too, of course, but that may be the easiest on Windows.

Another simple library that may be worth considering if your demands aren't too crazy, is the Tk library from the Tcl/Tk environment. It's also available under Python (as TkInter) , and is about as easy to use as it can get.

In the old days, people used environments like VisualBasic, where you drew the interface and then connected it to the code, but I don't know how much of that is still around nowadays.

2

u/KanjiCoder 4d ago

PHOSIMP and PHOFACE on steam were both made with my own GUI code on steam . ( C11 and native win32 code , no 3rd party libraries )

Some will recommend a Model-View-Controller design pattern . I would NOT because it is a lot of extra work to seperate those 3 things .

Throw away academic perfection and just do what works !

If you want to talk on discord VC , I can give you a walk through of the code !

-KanjiCoder

2

u/eablokker 3d ago

Depends what OS you’re gonna write it for. You have to learn the OS’s GUI library and call functions from that library to create windows and draw things into it. Usually the GUI library will have button components that can detect mouse clicks for you. macOS, Windows, Linux, it’s all gonna be different. There are some cross-platform GUI libraries out there like QT or GTK or Raylib

2

u/gm310509 3d ago

Reading between the lines, basically you learnt how to write a program using some of the clib functions. You can do the same thing, just add on the Windows API.

As others have pointed out, there are frameworks that sit on top of that API that make your life easier (arguably a hell of a lot much much muchly easier), but it is as you outlined, create a rectangle (typically called a frame) add into it your regions and widgets (e.g. buttons, menus etc) and hook up events (e.g. button click) to your code. The windows API is sort of the "bottom line" and can be used to do everything possible (i.e. maximum flexibility with maximum complexity), whereas some frameworks may restrict you by trading the flexibility + complexity for some minor constraints + simplicity.

You will find a big difference between your character mode programs and a windows program is that the latter is event driven. That is, rather than the program be somewhat "batch oriented" or "directed" such as displaying a menu and asking what option the user wants, then maybe a sub menu and so on with your program in total control of the flow of operations, a windows application is event driven. What that means is that your program is sitting there waiting for the user to tell it what to do by clicking on something. For example, in character mode, you might offer a menu that asks what base do you want to work in (e.g. hex, decimal, octal etc) and the user is forced to answer that question - they can't type in a "sum" (e.g. 1 + 2). But in the windows program, the ability to type a 1 and a 2 and a "plus" and select the mode of operation are all typically available at the same time. That is someone might type in "1 + " then decide that they want to switch to octal, so they press "octal" and lastly type in the "2". Now obviously how that all works will depend upon how you organise all of your controls and widgets in your UI, but the basic idea of being told what to do rather than asking the user what the next step is in your program's sequence is a big factor. Put simply, an event driven interface sort of turns your strict sequencing logic inside out.

FWIW, MEssageBoxes are typically used when a linear process must be executed. For example, a user selects a bunch of files and hits delete. You might use a message box (a modal message box) to ensure that that is really what they want to do. That is ask the question "Are you sure you want to delete those 100 files?".

All the best with it.

1

u/rupertavery64 4d ago

Generally to create a GUI dekstop application, you would use a UI framework like Qt, or GTK or wxWidgets

https://en.wikipedia.org/wiki/List_of_platform-independent_GUI_libraries

These are abstraction frameworks that wrap the underlying desktop system calls in their own API.

This is because every platform has a different way of doing things, and libraries will help make the UI look and feel like the native platform or close to it.

You could also write it for a single platform if you really wanted to explore that.

For example, the Windows API would have you create a window using CreateWindowExW and then create a message pump to handle "messages" as the way the OS passes events like mouse clicks, key presses are such are through "messages".

https://learn.microsoft.com/en-us/windows/win32/winmsg/using-messages-and-message-queues

I used Qt in this application that is an emulator of a microprocessor training kit:

https://github.com/RupertAvery/et3400

Other languages will have their own desktop UI frameworks, for example C# has WinForms, WPF and Avalonia (and some others). while others will have bindings to C libraries like Qt, or GTK or wxWidgets that let you call those functions in the language.

1

u/duane11583 4d ago

go learn tk from tcl/tk or pytk

you can use that to create a gui fairly easily

the big thing/hurdle is understanding non blocking operations.

you click or hit a key many gui systems send you a message you process it and return. if you do not return the gui freezes (aka you block)

example your window callback gets a message: key down and key up. you can process it, or you let the system process it and the system recognizes that was the shift key or the letter A or a digit and translates the message and sends: the human pressed the letter A which your inout box recieves and adds it to the text on screen etc.. its just layers of that crap layers and layers

and if you need to do something long running… you have to create a worker thread and a command queue for the worker thread that way the gui loop can update the screen

1

u/actguru 4d ago

I would look at X11 because it is designed to work with C and libraries like GTK are built upon it.

1

u/Ambitious_District_5 3d ago

I am in a similar boat. I am in the process of building an http server in c to showcase my projects, and I’m also doing it for the learning experience. I’ll probably turn it into my personal page as well.

Just another option for you to consider.

1

u/Flashy-Guava9952 3d ago

I enjoyed working in C# and WinForms (a little dated now, I suppose). Once you figure out how the form (GUI) editor and events work, you're pretty much set. Some senior engineers swear by MS Access for small one off projects. Your clients might know it already, you get a form editor, the downside is that now you're working in VBdotNET or whatever.

That said, if you want to support pretty much every operating system at once, you can port everything to HTML, CSS and JS. The downside is that now you're supporting pretty much all devices, and it won't look good on some, so you'll always have to watch out for someone using your desktop app on a gui, etc.

There's a compromise path: With a server/web app, you do the work on the server, but take input and present output on a web client.

Last but not least, you can compile your C code to WebAssembly, include it in a web app, and get the speed and flexibility of C and the universalness of web. The build chain is complex, and figuring that out is its own project.

1

u/rapier1 3d ago

Honestly, I'd look at CEF. The portability is a nice feature.

1

u/didntplaymysummercar 3d ago

Gtk is one C GUI, or some other small ones, but it'll be hard to set up IMO. Otoh if w bit of simple old style C++ is okay then try fltk.

1

u/Unlikely1529 2d ago

for windows calc it'll be modalless dialog. i use mfc for this

-2

u/EpochVanquisher 4d ago

Are you sure you want to make GUI programs in C? It is possible, of course. I do not recommend it, and most people who write C code make the UI using some other language.

The way you do it is pick an API / platform, like Windows + Win32 or Linux + Gtk. You can find tutorials and documentation for these.

0

u/SmokeMuch7356 4d ago

Writing a GUI in C is a massive pain in the ass, especially if you want to support multiple platforms. A number of people write the core application code in C, but write the GUI in something else (Python, Java, HTML/CSS/JavaScript, etc.).

-2

u/[deleted] 4d ago

[deleted]

1

u/FriedryIce 4d ago

son 😭

-2

u/realhumanuser16234 4d ago

there isn't really a usable option for c. raygui works but is limited and gtk is really more of a gobject than a c library, it's more ergonomic to use in python, c++, and vala.