Search This Blog

Starting Windows GUI Programming in C

(This article is written keeping in mind the beginner learner and friends I have come across who missed concepts which seem obvious to a luckier learner and the questions I wondered about while learning this stuff. So if you are among those who got it fast or if you don't want to question yourself too much, you might like to graze through the first few paragraphs. And for others, this article will be quite helpful in its entirety. But, having the zeal of figuring stuff out is more useful.)
View Sections
Sections

Background
   Usually learning computer programming starts with learning languages and writing console (text-mode or Command Line Interface) programs. Very often while learning, we don't get any hint of how much one can do with a text editor and compiler. Also, we usually aren't exposed to the power of using external libraries. Making GUI (Graphical User Interface) programs utilizes all these aspects of programming and for sure makes one feel of having put coding to some really cool use. (Though that doesn't mean console applications don't do cool stuff.)

Programming tools
   First, I would like you to know about what an IDE is and about command line tools. An IDE (Integrated Development Environment) is a GUI program which has all options related to developing a program. You can write code inside it, compile it using a menu option and run it - all inside the same place. Another great plus with some of them is code completion - helping you find correct "words" for your code. You just might be using one and not know the term for it. "IDE"! Examples of IDEs include Microsoft Visual Studio, C-Free, Code::Blocks, Eclipse, Turbo C/C++ (ancient, not recommended for serious use). An IDE makes developing programs and managing code easier.
   However, you can also do the same thing using an individual compiler and text editor. Notepad++ is a good text editor for Windows which provides many more options than the simple Notepad. For compiling you can use MinGW/gcc (gcc was originally for Unix, MinGW is its "adaptation" for Windows). So, open your code in a text editor. Then, open command prompt and go the folder where your code file is placed. And write the command for compilation. e.g. "C:\code>gcc first.c" is the command to compile first.c of C:\code folder (active folder). This method is again used below in more detail.

Windows API
   While typically learning C programming starts with console programs which use the Standard Library for i/o (in form of printf(), scanf() etc.), GUI programs use Windows API (Application Programming Interface). The API, in simple terms, is a library which provides functions and definitions which can be used in your programs the same way as printf() or cout. The code of the API remains largely hidden from us and is embedded in our program (executable .exe) while compilation; i.e. the compiling process performs the task of linking the API library files along with compiling our code (exactly like your introductory console programs). While learning more about programming, you will find about manually linking files. This link provides quite a detailed view of the whole process.

   To have an interesting start, just copy-paste some basic code, compile it and watch it work. You might then learn about what the code means. So, here you go.

First: Using command line tools
Compiling a Windows GUI executable on command prompt with gcc (MinGW):
  • The following file contains a basic Windows GUI program code, which uses the core Windows API functions: sites.google.com/site/vyoamx/win32.c.  Download and save it to your computer.
  • Assuming you have gcc (MinGW) installed and the gcc folder is in your PATH, open command prompt and go to the folder where the code file is saved.
  • gcc -o <name of your choice for the .exe> <name of the code file (with extension)> -mwindows
And boom bam boom, you have a Windows executable made in C without use of any IDE. Mind the "-mwindows" flag. My win32.c code allows for simple freehand drawing.

Second: Using an IDE
I find Code::Blocks and Microsoft Visual C++ Express good for C/C++. Visual C++ however, uses some Microsoft-specific code, which might be a small issue at start. So, we'll go with Code::Blocks here.
   Download the Code::Blocks installation binary (means the executable .exe), install and you are ready to go. Create a new project (Win32 GUI project -> Frame-based). In the "Sources" group of the project select main.cpp and replace its contents with the code of the file used above. Then F9 for Build and Run. Note that, by default C++ is the language. To configure, read this page.

Code explanation
   So, now you have made your own meaningful Windows GUI program. In the code the most important thing to note is the WinMain() function, as opposed the main() function. Next you have the user-defined WindowProcedur(), where events such as mouse click, program "refresh" etc, are reported and the corresponding actions are defined. This function is where most of the programming is done.

Learning material
   Now, to understand about the code you have to get some nice material on Windows programming. Indian author Yashwant Kanetkar's Let Us C is where my code is from and is explained properly. Nice book to say the least. For more specific material, the following are the two recommendations I have come across. I'll update them when I find better ones.

A few thoughts
   Now, I have a bit rambling to do about my learning to code. You may skip the next paragraph if you aren't interested in non-technical discussion.
   So, programming. In all the courses in my school and college on programming, they taught all about the language without giving any hint about its, shall we say, practical uses. I mean, it wouldn't hurt to include concepts of using external libraries and little bit of GUI in these courses. They were confined to console programs and the standard libraries. So, I have started learning about them now by myself. That is, as of now I am a beginner in the subject. Anyway, better late than never. Talking of academic courses, the good thing about them is that they have an established syllabus, which makes me feel I am learning a topic properly. I still feel a bit unsure when I learn stuff by myself about whether I am learning the stuff the right way or I am just getting it done some way. But, that's just me. And I am a bit insane. What do you think about learning that way? And how did you reach upto this topic? Do comment. "My journey in learning Windows programming". People should write that. Learning of other people's experience is very valuable in learning at large. I have to add, good books help more than Google. For the record, I started noob-level Windows programming with the book "Let Us C" by Yashwant Kanetkar.

Buying Musical Instruments in Kharagpur - Paul's Musical Mart

I wanted to buy an harmonica. So, where in Kharagpur could I get one? I asked a senior and he suggested this shop called 'Paul's Musical Mart'.

Located in Gole Bazar - a big marketplace of Kharagpur near the railway station - you can buy a variety of musical instruments here. Me and my friend got to the place, had a bit trouble finding the shop, but eventually got there asking around.

The shop has a variety of instruments including acoustic and electric guitars, harmonicas and violins.