83

I want to do Graphics programming in C. I had searched a lot about the compiler that provides a rich set of functions for doing GUI programming in C, but I couldn't find anything.

Basically I want to draw buttons and then accept the choice from the user and take an appropriate action. It would be helpful if you can suggest a C compiler, or a library that I can add to my compiler. I am working on the Windows operating system.

Presently, I am using TURBO C compiler that does not support direct methods for creating buttons. Any help would be appreciated.

athspk
  • 6,722
  • 7
  • 37
  • 51
Algorithmist
  • 6,657
  • 7
  • 35
  • 49

6 Answers6

160

This is guaranteed to have nothing to do with the compiler. All compilers do is compile the code that they are given. What you're looking for is a GUI library, which you can write code against using any compiler that you want.

Of course, that being said, your first order of business should be to ditch Turbo C. That compiler is about 20 years old and continuing to use it isn't doing you any favors. You can't write modern GUI applications, as it will only produce 16-bit code. All modern operating systems are 32-bit, and many are now 64-bit. It's also worth noting that 64-bit editions of Windows will not run 16-bit applications natively. You'll need an emulator for that; it's not really going to engender much feeling of accomplishment if you can only write apps that work in a DOS emulator. :-)

Microsoft's Visual Studio Express C++ is available as a free download. It includes the same compiler available in the full version of the suite. The C++ package also compiles pure C code.

And since you're working in Windows, the Windows API is a natural choice. It allows you to write native Windows applications that have access to the full set of GUI controls. You'll find a nice tutorial here on writing WinAPI applications in C. If you choose to go with Visual Studio, it also includes boilerplate code for a blank WinAPI application that will get you up and running quickly.

If you really care about learning to do this, Charles Petzold's Programming Windows is the canonical resource of the subject, and definitely worth a read. The entire Windows API was written in C, and it's entirely possible to write full-featured Windows applications in C. You don't need no stinkin' C++.

That's the way I'd do it, at least. As the other answers suggest, GTK is also an option. But the applications it generates are just downright horrible-looking on Windows.


EDIT: It looks like you're not alone in wanting to write "GUI" applications using an antiquated compiler. A Google search turns up the following library: TurboGUI: A GUI Framework for Turbo C/C++:

 TurboGUI interface sample

If you're required use Turbo C, this might be an option.

user229044
  • 232,980
  • 40
  • 330
  • 338
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • 7
    @Algorithmist: I suggested Visual Studio; it's an IDE that comes with Microsoft's compiler. The Express edition (which includes the same compiler as the pro editions) is a free download. See my answer for the link. [MinGW](http://www.mingw.org/) is another option. It's a port of GCC to Windows. I've never used any of these though. Visual Studio is my environment of choice for Windows C and C++ development. There are other questions here on Stack Overflow about alternative C compilers for Windows if you're interested in exploring the field. – Cody Gray - on strike Mar 28 '11 at 04:03
  • @Algorithmist I use Cygwin configured with Netbeans. Check this http://netbeans.org/community/releases/60/cpp-setup-instructions.html. Other option you have is Use Eclipse configured with MinGW ... check this http://www.banym.de/eclipse/install-eclipse-cdt-on-windows-7 – AurA Sep 28 '12 at 05:59
  • @Algorithmist, I use gcc in windows simply by installing [GOW](https://github.com/bmatzelle/gow/downloads)(Gnu On Windows),it's lighter than Cygwin. – Willy satrio nugroho Aug 15 '19 at 10:36
  • The link https://planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=7776&lngWId=3 is not working... – Chaminda Bandara Feb 22 '20 at 13:00
21

The most famous library to create some GUI in C language is certainly GTK.

With this library you can easily create some buttons (for your example). When a user clicks on the button, a signal is emitted and you can write a handler to do some actions.

Sandro Munda
  • 39,921
  • 24
  • 98
  • 123
  • 1
    The problem I see is the old turbo C compiler. Not sure if it and the libs surrounding it are up to date enough to handle gtk builds. – AndreasT Mar 27 '11 at 15:19
  • 10
    "The most famous library to create some GUI in C language is certainly GTK." Perhaps in your world it's the most famous. I always thought Win32 was better known. – David Heffernan Mar 27 '11 at 15:55
  • 4
    "I always thought Win32 was better known." --- You thought right. Win32 is widely known for its *portability*. – Daniel Apr 28 '17 at 10:51
  • Compiling GTK on a macbook is a nightmare. – Samie Bencherif Mar 26 '19 at 21:20
8

C is more of a hardware programming language, there are easy GUI builders for C, GTK, Glade, etc. The problem is making a program in C that is the easy part, making a GUI that is a easy part, the hard part is to combine both, to interface between your program and the GUI is a headache, and different GUI use different ways, some threw global variables, some use slots. It would be nice to have a GUI builder that would bind easily your C program variables, and outputs. CLI programming is easy when you overcome memory allocation and pointers, GUI you can use a IDE that uses drag and drop. But all around I think it could be simpler.

OMG coder
  • 167
  • 1
  • 7
sergio
  • 399
  • 4
  • 3
8

Use win APIs in your main function:

  1. RegisterClassEx() note: you have to provide a pointer to a function (usually called WndProc) which handles windows messages such as WM_CREATE, WM_COMMAND etc
  2. CreateWindowEx()
  3. ShowWindow()
  4. UpdateWindow()

Then write another function which handles win's messages (mentioned in #1). When you receive the message WM_CREATE you have to call CreateWindow(). The class is what control is that window, for example "edit" is a text box and "button" is a.. button :). You have to specify an ID for each control (of your choice but unique among all). CreateWindow() returns a handle to that control, which needs to be memorized. When the user clicks on a control you receive the WM_COMMAND message with the ID of that control. Here you can handle that event. You might find useful SetWindowText() and GetWindowText() which allows you to set/get the text of any control.
You will need only the win32 SDK. You can get it here.

BlackBear
  • 22,411
  • 10
  • 48
  • 86
  • 1
    You *will* need the Windows SDK, though. Download it [here](http://msdn.microsoft.com/en-us/windows/bb980924.aspx). (It's included with Visual Studio. You only need to download it separately if you want to use a different compiler.) – Cody Gray - on strike Mar 27 '11 at 15:25
  • @Cody Gray: forgot that, probably it was included in my masm32 setup. :) I'll add to my answer – BlackBear Mar 27 '11 at 15:27
5

Windows API and Windows SDK if you want to build everything yourself (or) Windows API and Visual C Express. Get the 2008 edition. This is a full blown IDE and a remarkable piece of software by Microsoft for Windows development.

All operating systems are written in C. So, any application, console/GUI you write in C is the standard way of writing for the operating system.

Derek Hogue
  • 4,589
  • 1
  • 15
  • 27
Viju
  • 101
  • 1
  • 4
5

A C compiler itself won't provide you with GUI functionality, but there are plenty of libraries for that sort of thing. The most popular is probably GTK+, but it may be a little too complicated if you are just starting out and want to quickly get a GUI up and running.

For something a little simpler, I would recommend IUP. With it, you can use a simple GUI definition language called LED to layout controls (but you can do it with pure C, if you want to).

David Brown
  • 35,411
  • 11
  • 83
  • 132
  • I cannot thank you enough right now for recommending IUP because I've been pulling my hairs out with GTK for the past 6 days and gotten nowhere. – person the human Dec 20 '21 at 19:09