Click here to Skip to main content
Licence 
First Posted 14 Apr 2003
Views 166,449
Bookmarked 74 times

Using GDI+ with MFC or native C/C++

By | 14 Apr 2003 | Article
Why 99.9% of GDI+ samples in .NET languages? What about C/C++?

Introduction

If you did a GDI+ sample query on Google, I bet you will get hundreds or thousands of URLs pointing you to a .NET free code. If you go book shop you will see dozens of books talking about GDI+. However, if you try to find information about integrating GDI+ with MFC/ATL/WTL, I bet you will find none (if you could, kindly post the URL!). So, is it impossible or so difficult that it doesn't worth using?

Findings

The answer is: Extremely easy to use GDI+ in MFC/ATL/WTL, or even straight C language. First, I have to admit I have no knowledge about .NET, I am not a fan of Microsoft .NET but rather prefer platform SDK. GDI+ IS NOT solely for .NET, if you plan to use GDI+ on Windows version lower than XP, then you would have to redistribute the GDI+ runtime (here). Don't worry, it is only 1.04 MB, not a xx MB .NET runtime.

So?

  • Add the following line to your stdafx.h
    #include <gdiplus.h>
    using namespace Gdiplus;
    #pragma comment(lib, "gdiplus.lib")
  • Intialize the GDI+ resources. Add this to your CWinApp derived class as member:
    GdiplusStartupInput gdiplusStartupInput;
    ULONG_PTR gdiplusToken;

    At InitInstance(), add:

    GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
  • Your application is ready to consume GDI+ now.
  • Upon exit, release GDI+ resources. Add the following line to ExitInstance():
    GdiplusShutdown(gdiplusToken);

As you can see, it is that simple. In general, it should be very easy to port your existing WIN32/MFC/WTL painting code into GDI+. If you ask why? Consider there is one GDI+ C++ class called RedEyeCorrection, that's right, to correct the red eyes that sometimes occur in flash photographs. If you have at least one year WIN32/MFC experiences, just follow the link here to see more.

I included a demo project to demonstrate the above. Enjoy!

GDI+ is device context handle centric, hence if you can split drawing logic aside from actual drawing code, you will have no problem in porting to GDI+. In some cases, 100 lines of drawing code can be reduced to just 5 lines with GDI+ intensive support of complex drawing exercises. This means mixing ordinary GDI with GDI+ a trivial task.

The downloaded project is a standard MFC SDI project, if you have read it (I hope so...), you will find that it is unbelievable simple. Nearly all time, you can simply cut-n-paste source code embedded in MSDN GDI+ to try things out. One tips: if the GDI+ need to load an external file, you can just right click the image in the help and save it, then continue to use the downloaded project to continue testing.

Finally, you do need to have a copy of platform SDK to have necessary GDI+ header, library at your VC search path. It is not necessary for the latest version, I am using version Aug'02. GDI+ files can be extracted from CAB_11, if you do not want to install the SDK.

** Don't forget to visit here for more information on integrating GDI+ with Win32/MFC/ATL/WTL **

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

TW



Malaysia Malaysia

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionBuild samples with VS2010 [modified] PinmemberWill Stott6:09 13 Sep '11  
GeneralGood Title, Excellent Sample PinmemberMomony10:35 29 Apr '10  
GeneralRe: Good Title, Excellent Sample PinmemberTheo Buys2:24 10 Feb '11  
GeneralGDI+ inside DLL's PinmemberMember 25423088:57 7 Aug '08  
GeneralRe: GDI+ inside DLL's PinmemberGreg Prosch6:30 10 Nov '08  
GeneralRe: GDI+ inside DLL's Pinmemberzl1985471:02 15 Jul '09  
GeneralRe: GDI+ inside DLL's PinmemberGreg Prosch5:56 15 Jul '09  
GeneralBitmap - delete the object PinmemberAlex Evans14:56 6 Feb '05  
GeneralRe: Bitmap - delete the object PinmemberTW16:40 6 Feb '05  
GeneralRe: Bitmap - delete the object PinmemberAlex Evans9:25 7 Feb '05  
Generalthank very much. ^^ Pinmembervpas18:28 24 Jan '05  
GeneralBest GDI+ MFC Beginners article so far PinmemberTommYNandA20:37 19 Nov '04  
GeneralTo end this silly debate.... PinmemberTanzim Husain18:15 19 Nov '03  
GeneralDo not want to debate, but... PinmemberZhefu Zhang15:11 10 Nov '03  
QuestionYou wanted a URL? PineditorMarc Clifton2:39 8 Nov '03  
AnswerRe: You wanted a URL? PinmemberTW2:43 8 Nov '03  
GeneralRe: You wanted a URL? PineditorMarc Clifton2:47 8 Nov '03  
AnswerRe: You wanted a URL? Pinmembervjedlicka22:56 7 Dec '05  
GeneralRe: You wanted a URL? PinmemberTheo Buys2:16 10 Feb '11  
GeneralRe: You wanted a URL? Pinmembervjedlicka2:32 10 Feb '11  
Generalgoods PinsussAnonymous21:21 14 Jun '03  
GeneralGood but incomplete PinmemberNicolas Bonamy3:44 16 Apr '03  
GeneralWhy 99.9% of GDI+ samples... PinmemberJim Crafton7:42 15 Apr '03  
GeneralRe: Why 99.9% of GDI+ samples... PinmemberMario M.9:44 15 Apr '03  
GeneralRe: Why 99.9% of GDI+ samples... PineditorMarc Clifton2:19 8 Nov '03  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 15 Apr 2003
Article Copyright 2003 by TW
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid