Sunday, 20 July 2014

Visual effects for Winapi windows

FAQ: Why is my winapi hello world ugly?

A simple hello world:

#include <Windows.h>

int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR lpcmdline, int ncmdshow)
{
    MessageBox(NULL, TEXT("Old style"), NULL, NULL);
    return 0;
}


displays the following message box:


On inspection, it is apparent that the ugly monstrosity that appeared simply doesn't match the windows theme. In particular, the OK button has the old Win95 feel to it, which particularly stands out. (I'm assuming you're not on older version of Windows, in which case, you wouldn't know/notice.)

But why? you say. My build environment is Windows Vista/7/8!

The answer is, you need to enable visual styles. Modify the code:

#pragma comment(linker,"\"/manifestdependency:type='win32' \
name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")


#include <Windows.h>
#include <CommCtrl.h>

int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR lpcmdline, int ncmdshow)
{
    InitCommonControls();
    MessageBox(NULL, TEXT("New style"), NULL, NULL);
    return 0;
}


and you get something much better now:


For more information, read this.

No comments:

Post a Comment