Friday, February 20, 2009

[VC++] Enabling XP style theme in Win32/MFC App

You might have noticed that your application developed in VC6, XP style themes are not enabled. If you want your application to use visual styles, you must add an application manifest that indicates that ComCtl32.dll version 6 should be used if it is available. If you are looking for adding XP visual styles you have two options to deal with, you can either add a manifest file to the path of your executable or you can embed the manifest content to your executable. See how to do the same in each option.

Adding Manifest file method

  • Create a text file with name YourAppName.exe.manifest in your executable path. For eg: if your executable's name is TestApp.exe manifest file's name will be TestApp.exe.manifest
  • Paste the below code to the manifest file you created. Replace CompanyName.ProductName.YourApp and Your application description here with your application details in the manifest file.
  • Now you can run your application and feel the difference !
<?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="X86"
name="CompanyName.ProductName.YourApp"
type="win32" />
<description>Your application description here.</description>
<dependency>
<dependentAssembly>
<assemblyIdentity
type="win32"
name="Microsoft.Windows.Common-Controls"
version="6.0.0.0"
processorArchitecture="X86"
publicKeyToken="6595b64144ccf1df"
language="*" />
</dependentAssembly>
</dependency>
</assembly>
Embeding manifest in your executable
  • Take Workspace window->Resource tab of your Visual Studio IDE
  • Right click on the resources and choose insert , then Insert Resource dialog will be shown.
  • Select Custom... button of the Insert Resource dialog and enter the custom resource type as 24 and press OK.
  • Copy and paste the above shown manifest file after specific modifications (mentioned in the previous method)
  • Change the Resource ID of the newly added IDR_DEFAULT1 to 1
  • Call InitCommonControls() (include commctrl.h and link comctl32.lib)from any of your init function such as InitInstace() or Winmain()
  • Rebuild the application, everything is embedded in your application. Execute it and feel the XP style.

No comments: