C++/Win32 Tutorials
March 1, 2008
If you’ve come here to learn some Win32 programming in C++, you’ve no doubt noticed that helpful documents on the subject are scarce. They do exist, but they tend to be specific and rather bland. This tutorial aims to remedy that situation.
When I finished college in ‘97, I was a master Unix programmer at a time when Windows was becoming the monster OS it is today. Companies were hiring C++ programmers like crazy, and here I was, skilled in programming Unix but with no idea how to program Windows. Somehow, it was just assumed that you could figure it out. That’s true, but nevertheless a little detailed explanation of what’s going on can go a long way towards writing solid code. I wish someone had done this for me when I was leaving university
Screenshots
Here are two screenshots of the project as it develops:
(Images current as of Lesson 19 on July 05, 2004)
![]() Application in skinned mode |
![]() Application in unskinned mode |
GlowdotPortScanner_build19.zip [304kb zipped]PrerequisitesI originally wrote this document for a friend of mine, who I wanted to get caught up on Windows programming. He had a good background in C programming for Unix, and needed a quick primer to get him programming Windows in C++. The following tutorial is a polished version of what I wrote for him.I personally learn more quickly if I have an actual project to complete, and I think that’s probably true for most people. So my idea for this tutorial was to walk through a complete project, throwing as many features into it as possible, until the project was complete. This way, if you get through the tutorial, you will know everything you need to start and finish a working Win32 application.The project I chose was a port scanner — a useful tool for ISPs. I chose this particular project for its simplicity, and also its need for the use of sockets. You’re going to need to know how to work with sockets if you want to write anything interesting these days (when was the last time you used an app that didn’t do something with the Internet?)
So, will you be a master of Win32 programming after finishing this? No way. There’s a lot more to learn. But after finishing this tutorial, you will feel much more comfortable digging into the Win32 API documentation while working on your own projects.
This tutorial is designed to evolve. If you find something that isn’t clear, or really want more information on a particular topic, feel free to leave comments and/or questions using the form at the bottom of each page. I will most likely be updating these pages several times until its a rock solid set of files, and your input will be most appreciated.
Win32 vs. MFC vs. .NET
If you came here, you obviously want to learn the Win32 API, so I won’t bother to explain to you in detail why it’s a good choice. It is faster, and you have more control over the workings of your application, and that’s that. .NET certainly has advantages for large team projects and applications that you want to move to other Windows products, and MFC is great for fast development, but if you plan on writing a game (especially using DirectX), the next Winamp, or your own super-stylin’ skinned monster app, you’ll probably want to do it using the API. And if you start with Win32, you will have very little problem moving to MFC or .NET, since they just simplify the process.
Topics Covered
The following topics will be covered on the way to completing the Port Scanner Application:
- Basic Window Creation
- Creating a Win32 Window Wrapper Class (Part 1)
- Creating a Win32 Window Wrapper Class (Part 2)
- Inheritance and Beginning GUI
- Port Scanning and Sockets
- More GUI
- Skinning
- Threads
- Window Dressing: Icons, Icon Buttons, Tooltips & Accelerators
- Menus
- Polishing the GUI
- Resolving DNS
- Dialogs
- Printing
- The System Tray and Balloon Tips
- Working With Files
- ListViews
- Sounds
- Context Menus
Lessons are not broken up neatly into tidy topical discussions, because that’s not how you’ll be developing your applications. Remember that this tutorial is designed to walk you through the development process of a full application, and you will need to prepare your brain for that kind of learning. You can pick up any book on programming, or even take a class, and gain a full breadth of knowledge on the subject and walk away with no clue how to start, build and finish an application. I want to change that.Whenever you decide to start programming a new system in a new language, there is almost always a massive set of documentation that you can go over. But if you are just starting out, how do you tackle thousands of pages of docs? You don’t. But once you get up and running, consulting documentation to find a specific answer is easy and non-intimidating. The MSDN pages are no different. Once you’ve actually completed your first app, you’ll see how useful MSDN (and Google) can be in getting you past whatever hurdle you’ve hit in your development.
So that’s why this tutorial is written the way it is. Topics will be brought up quickly and briefly, in just enough detail to get done what needs to get done. You can always refer to the additional reading to get more information about a particular topic. And when you become a master, consult the Win32 API docs like a pro.
One more hint I can offer when learning this stuff: it has helped me in the past to actually type in the source code myself, rather than just read it off of a computer screen. I think this has something to do with forcing you to actually engage every line of code rather than just glossing over things you don’t fully understand. This might not be helpful to you, but it has worked well for me in the past.
Source Code
At the end of each page, you will find a link to download a zipped project file. The project files for this tutorial were created in Microsoft’s Visual Studio .NET (2003). The projects were created in MSVS because I use it and I like it, and you can pick up the standard version of the C++ compiler for less than $100 bucks at Amazon.
You can easily adapt this to Borland or another compiler, but you are on your own there.
Further reading
At the bottom of each page, you will find links to other sites covering the topics presented and possibly some book recommendations. There are hardly any good books covering Win32 programming in C++, but I have found a few that you might find useful.
Many people recommend the Petzold book, but it’s out of print and hard to find. I recommend Nitty Gritty Windows Programming with C++ by Henning Hansen. It basically only covers the API, and assumes a good knowledge of C++, but is a good reference book. If you still need to learn C++, I recommend either The C++ Programming Language by Bjarne Stroustrup (for those already familiar with basic programming concepts) or Thinking in C++ by Bruce Eckel (for beginners).






hi,
Wish I knew your name. I know at this point you’re swearing at me since i probably should have seen it somewhere by now?! hehe.
Listen all I would like te say is THANK YOU VERY VERY MUCH!!!!
Been working on winapi since the beginning of last year and never seen such a user friendly tutorial. Thanks for taking the time and supp;ying us with this brilliant piece! Always good to go back to the basics!
I - for one - hope you have a great … lets make it … life! (Screw the day part)
Cheers.
I really appreciate this excellent tutorial. I’ve been trying to start programming with the windows api lately but I’ve been kind of confused about using window controls like buttons, text, etc. This tutorial has cleared it up a lot and given me a better understanding of programming a real program.
It seems very hard to find a good tutorial out there or even good code for that matter. It seems like everybody just leaves out parts of the code in their tutorials and doesn’t provide a full source code or their code doesn’t compile. I haven’t had any problems with the code compiling!
I haven’t finished the tutorial yet (only on page 5) but I just wanted to say thanks before I forgot to.
Good one for every newbie to begin with………
Thanks to the creator.