Visual Styles in Win32 API C GCC MinGW
If you are starting with GUI (Graphical User Interface) programming you may be having some issues with the styles not appear how they are “suppose to”. Buttons may look very old using the “3D Style” and not using the glowing gradient pulsing buttons we are so use to in modern Operating Systems such as Vista, Windows 7 and 8. Below I will explain how to get the Visual Styles enabled in C programming in the Win32 API. I had a lot of issues getting it working so hopefully this will help you out.
First we create a Resource File. This allows us to embed resources into
our C program such as Icons, Bitmaps and the Manifest file to enable
the Visual Styles. Create a file called resources.rc
.
1 24 "Application.manifest"
The value “1” identifies as the type of application. Since this is for a program (EXE) and not a DLL or library we use 1. Sadly I cannot find the documentation for this, but in the future if I do I will be sure to include it here.
The value “24” is the type of resource, and in this case RT_MANIFEST. I set them as the numeric value since I had issues defining them to their “names”. MSDN Resource Types
The value “Application.manifest” is the file we want to embed into the program. The next thign we do is create the Application.manifest file.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity version="1.0.0.0" processorArchitecture="*" name="CompanyName.ProductName.YourApplication" type="win32" />
<description>Your application description here.</description>
<dependency>
<dependentAssembly>
<assemblyIdentity type="win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" processorArchitecture="*" publicKeyToken="6595b64144ccf1df" language="*" />
</dependentAssembly>
</dependency>
</assembly>
There is a lot here, but if you do copy and paste this you will be good if the windows version is at least Windows 7. This just tells the program and windows libraries that your program is indeed a recent program that can use Windows 7 visual styles and newer. MSDN Manifest Files
Now we can “compile” the resource file to be used soon in our program. Run the following command from the directory you have your resources.rc file and Application.manifest file.
$ windres resources.rc -o resources.o
Next we have to add some code to our main application, in this case main.c.
#define _WIN32_WINNT 0x0600
#define _WIN32_IE 0x0900
#include <windows.h>
#include <commctrl.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmd) {
INITCOMMONCONTROLSEX icex;
icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
icex.dwICC = ICC_STANDARD_CLASSES;
InitCommonControlsEx(&icex);
/* Your Create Window code and other stuff */
}
The issue a lot of tutorials skip on is as follows.
#define _WIN32_IE 0x0900
The InitCommonControlsEx
require to have at least a value of 0x0300
and that doesn’t get set by default. So you will have to set that, I
set mine to 0x0900
since that is the version I am running.
Now we compile the code while linking the libraries.
$ gcc -o program.exe main.c resources.o -lcomctl32 -mwindows
And we are done. You should have nice buttons now, along with other windows.