Creating Windows GUIs with C++

This is only slightly related to robo, but…

I’ve been making Windows applications for about a year since I took a programming class in school last year. The main thing I’ve done is a server launcher for Command & Conquer Renegade (an old FPS) and its assorted mods. More to the point, I create installers and distribute my programs over the web. I’ve been using VB.NET. While I love the language, a lot of people who I distributed the program to have complained about the .NET framework, as it requires a large RAM overhead (my program, while relatively small, takes 20 MB of RAM to run), and is sluggish redrawing the window and such on the screen. They are also frustrated with having to download a relatively large framework in the first place.

I’m working on acquiring a copy of VB6 to circumvent these issues, but at the suggestion of another member of our code team, I’m trying to do something similar in C++

To make a long story short, I’ve been teaching myself C++ (due to its similarity to the C we use to program our robots with) so that I can create better, smaller, and faster-running apps. However, since I highly doubt that my users would want do everything in command prompt, I want to create a GUI.

I understand it’s possible to create windows using only the Windows API, but that’s very complex (for me, at this level, anyways), and I’ve been looking into other options.

A fellow code member on my team suggested I used Glade, which is a GUI builder that uses the GTK+ toolkit.

Does anyone else have any experience, either in Glade or just creating windows with C++ in general, and if so, do you have any advice for me?

Where I work at Sony Creative Software, we use C++ directly with the Win32 API. It makes for much faster programs, especially when it comes to user interface elements. It’s fine to work with once you’re used to it, but there’s a steep learning curve - mostly because it’s written in such a way that it can be used in C (i.e. no classes, just a whole lot of global functions, structures and macros).

I would suggest you look into MFC (Microsoft Foundation Classes). It’s basically a Microsoft-written C++ wrapper for the Win32 API. If you’re using Visual C++, there are wizards to get you started with MFC applications.

Otherwise, if you’re interested in using the API directly, I have to recommend this book; it’s considered the “bible” of Windows programming.

http://www.winprog.net/tutorial/

Helped me (of course, a little side help from a couple of advanced programmers) create this program:

http://rapidshare.com/files/76407628/Navi2go_Quick_Launcher_Installer.rar.html

the winprog site is a really good introduction to programming API which leads to learning to use OS components and programs.

I’m looking forward to starting to learn MFC, which should be the next step for programming applications and so.

If you need any help with API, I’ll be glad to help.

Nir.

Check out the Qt toolkit from Trolltech: http://trolltech.com/products/qt/homepage

It’s open source (GPL’d)…so if you create open source apps with it you can use it for free. The docs are great–they have some tutorials and lots of examples.

I’ve used it to create ~3 GUI apps now, and it’s worked great every time.

  • Taj

Also check out wxWidgets

Awesome guys. Thanks for the help.

wxWidgets is pretty cool for multi platform free stuff. There is a pretty nice visual compiler called wxDev-C++. The one bad thing about wxWidgets is that it is bulky and is all 3rd party so some things aren’t all the way there, its not the most supported. Programs like VLC media player and audacity use wxWidgets. With wxDev-C++ the exe seems to be kinda big.

I looked a little bit at QT and it just seems kinda weird, and I believe you need a license for it. I didn’t look into it too much.

I would suggest learning the windows programming, you will probably be much better off. Get Visual Studio and maybe a nice book and play around.

Or…

You can learn some C#, from what I hear C# and the .net framework are very easy to use and are good for GUI applications. One plus side to C# is all the memory management is done inside the .net framework so you don’t have to worry about memory leaks as in C++. With C# you cant do things with memory though as you can in C/C++ though.

So it all comes down to personal preference. C# may be easier but I think it is a bit more restricted compared to C++ and with C++ you will probably be writing and debugging more then C#. I don’t know too much about C# but this is just what I have heard :wink:

To clarify, Qt is dual-licensed under a commercial license as well as the GPL. If you use Qt to develop a program under the terms of the GPL, than you may use the free (cost) GPL Qt. If you are developing closed-source programs, than you will have to pay for the commercial license of Qt.

I’m having issues now.

My code:

This is just a simple “Hello World” using message boxes.


#include <Windows.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
				   LPSTR lpCmdLine, int nShowCmd)
{
	MessageBox(NULL, "Hello!", 
		"Hello World", MB_OK | MB_ICONINFORMATION);
	if (MessageBox(NULL, "I'm learning to use C++ to write native Windows programs.  Cool, isn't it?",
		"Cool, No?", MB_YESNO | MB_ICONQUESTION) == IDYES)
		MessageBox(NULL, "You know it!", "Sweet!", MB_OK);
	else MessageBox(NULL, "Jerk.", "Grrr", MB_OK);
	return 0;
}

I’m using Microsoft Visual Studio C++ Express Edition 2008 as my IDE.

  1. When I create an empty solution and insert this code, everything runs fine on my computer, but when I tried to show it to friends they said they all got an error that my application was not configured properly.

  2. When I create an empty Win32 solution and insert the code, it refuses to build, telling me for each argument I pass:

error C2664: ‘MessageBoxW’ : cannot convert parameter 2 from ‘const char [13]’ to ‘LPCWSTR’
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

I’ve tried using the Windows.h that came with the IDE and one I got from Windows Platform SDK for Windows Server 2003 R2.

Your error message is Unicode-related. For every Windows function that deals with strings, there are actually two functions - one that uses ASCII strings (each character is 1 byte wide) and one that uses wide strings (each character is 2 bytes wide). The Win32 API functions typically have an ‘A’ or ‘W’ at the end of the name (like ‘MessageBoxW’).

The problem here is that your build environment appears to be configured to use Unicode, so all the Windows functions like MessageBox are automatically being aliased to their Unicode equivalents, like MessageBoxW. You’re therefore trying to pass a string with ASCII characters (declared something like “stringgoeshere”) into a function that takes a wide-character string (declared like L"stringgoeshere").

You could just put an ‘L’ in front of all your strings to force them to use wide characters, but there’s a better solution. Windows has a data type called a ‘TCHAR’ you can use for strings, which is automatically converted into either ASCII or wide characters when you compile, depending on whether or not the _UNICODE preprocessor flag is set. All you have to do is include tchar.h, enclose your literal strings with the _T() or _TEXT() macros, and use the TCHAR data type whenever declaring string variables.

#include <tchar.h>

...

// This is a string of 256 characters (ASCII or wide)
TCHAR szText[256]; 

...

// _T and _TEXT do the same thing
MessageBox(NULL, _T("Hello!"), 
	_TEXT("Hello World"), MB_OK | MB_ICONINFORMATION);


If you plan to use standard string functions (like strcpy, strcmp, etc.) there are TCHAR equivalents (like _tcsncpy and _tcsncmp) that will be converted to the appropriate function by the preprocessor at compile time - just look them up in tchar.h.

Thank you. It now compiles. And it works perfectly on my machine. However it still generates an error whenever I show it to somebody else:

this application has failed to start because the application configuration is incorrect. Reinstalling the application may fix this problem.

My code:


#include <Windows.h>
#include <tchar.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
				   LPSTR lpCmdLine, int nShowCmd)
{
	MessageBox(NULL, _T("Hello!"), 
		_T("Hello World"), MB_OK | MB_ICONINFORMATION);
	if (MessageBox(NULL, _T("I'm learning to use C++ to write native Windows programs.  Cool, isn't it?"),
		_T("Cool, No?"), MB_YESNO | MB_ICONQUESTION) == IDYES)
		MessageBox(NULL, _T("You know it!"), _T("Sweet!"), MB_OK);
	else MessageBox(NULL, _T("Jerk."), _T("Grrr"), MB_OK);
	return 0;
}

A Google search turned up this - seems like applications built with VS 2008 need to have certain DLLs distributed with them, even if they’re just using simple Win32 API stuff.

It gets better and better.

While the fundamentals of writing Windows applications is relatively straightforward, it’s all the arcane gotchas that take a long time to come across and learn so you can be really good. If you’re looking for a C++ framework, I’d go with MFC to start. There’s good Wizard support in Visual Studio and lots of help available on the Web. There are literally thousands of examples to be had. www.codeproject.com is a very good source of both information and examples. One comment about examples though, the ones provided directly by Microsoft are horrible and you’d be better off never looking at them.

Another good choice for a C++ framework is WTL, Windows Template Library. It was originally developed as a side project within Microsoft to be what MFC was not, a object oriented C++ solution. Since it uses templates heavily, it’s probably not that easy for a beginner to get up to speed on. It was never officially supported by Microsoft and is now an open source at Source Forge. It’s what I’m currently using for what little Windows programming I do these days.

So much for being able to distribute just my executable. :mad:

However, I really appreciate you guys. Awesome help. This really shows how awesome FIRST (and CD) are.

Oh, that’s one advantage of WTL I forgot to mention. WTL requires no additional DLLs unless you explicitly decide to link to additional libraries. And in that case you know what you require. Also, your application can be quite small since you don’t drag in things you’re not using.

Hang on, were you able to send it to your friends?

Wow! Now I remember why I gave up on Win32 so long ago. I was in pretty much you exact position 5 years ago. After running into so many “gotchas” (I had a different word for them), I decided to cut my losses. I switched to Python and haven’t looked back.

I won’t start a holy war thread here, but you may want to carefully consider exactly what you want to do, and then compare your available language options before committing.

I would also like to highly recommend “How to Think Like a Computer Scientist” by Allen Downey. You can download it for free at http://www.greenteapress.com/thinkpython/
Allen Downey is a prof at Olin College (my alma mater). Several years ago he wrote a Java version of the book and copy-lefted it. Someone took the book, revised it for Python, and rereleased it. Allen then read this book and learned Python.

… Yes, he learned Python by reading his own book!

It has some chapters that deal with a little bit of user interfacing, so it shouldn’t be hard at all to get going.

Good luck!
Eric

Not to be a killjoy but I know someone who uses python and his application is fairly slow, for the simplest of programs you have to package the exe with tons of files, it just seems like a hassle. If you are going to learn a different language to do it C# is the way to go. C# is the most similar to C++ as well :wink: