Click here to Skip to main content
15,921,062 members
Articles / Desktop Programming / WTL
Article

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

Rate me:
Please Sign up or sign in to vote.
3.39/5 (76 votes)
14 Apr 20032 min read 285.7K   4.9K   88   51
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


Written By
TW
Malaysia Malaysia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: Why 99.9% of GDI+ samples... Pin
TW8-Nov-03 2:48
TW8-Nov-03 2:48 
GeneralRe: Why 99.9% of GDI+ samples... Pin
Marc Clifton8-Nov-03 2:51
mvaMarc Clifton8-Nov-03 2:51 
GeneralRe: Why 99.9% of GDI+ samples... Pin
TW8-Nov-03 2:58
TW8-Nov-03 2:58 
GeneralRe: Why 99.9% of GDI+ samples... Pin
Marc Clifton8-Nov-03 3:04
mvaMarc Clifton8-Nov-03 3:04 
GeneralRe: Why 99.9% of GDI+ samples... Pin
TW8-Nov-03 3:08
TW8-Nov-03 3:08 
GeneralRe: Why 99.9% of GDI+ samples... Pin
Marc Clifton8-Nov-03 6:32
mvaMarc Clifton8-Nov-03 6:32 
GeneralRe: Why 99.9% of GDI+ samples... Pin
TW8-Nov-03 6:37
TW8-Nov-03 6:37 
GeneralRe: Why 99.9% of GDI+ samples... Pin
Marc Clifton8-Nov-03 6:55
mvaMarc Clifton8-Nov-03 6:55 
You didn't answer my question. I want to know why YOU think C/C++ is coupled with design patterns. I want to know what YOU think design patterns are.

The article is 6 years old. In this rapidly moving industry, that makes it obsolete.

Modularity -- Frameworks enhance modularity by encapsulating volatile implementation details behind stable interfaces. Framework modularity helps improve software quality by localizing the impact of design and implementation changes. This localization reduces the effort required to understand and maintain existing software.

Really. Show me one that actually does this. MFC and .NET certainly don't. I know of only one framework that meets these goals.

Reusability -- The stable interfaces provided by frameworks enhance reusability by defining generic components that can be reapplied to create new applications. Framework reusability leverages the domain knowledge and prior effort of experienced developers in order to avoid re-creating and re-validating common solutions to recurring application requirements and software design challenges. Reuse of framework components can yield substantial improvements in programmer productivity, as well as enhance the quality, performance, reliability and interoperability of software.

That is so naive it is laughable. A framework does not make a component generic. A component is generic because it is well designed, regardless of the framework. Most developers, after reviewing their prior effort, realize that things could have been done a lot better, and throw out all but very basic functions. So much for your idea of a reusable framework.

In your article, you say that you're primarily an SDK programmer. Is there some particular reason you don't want to leverage the MFC framework to gain substantial improvements in programmer productivity, as well as enhance the quality, performance, reliability and interoperability of software??? ROTFLMAO!

Extensibility -- A framework enhances extensibility by providing explicit hook methods [Pree:94] that allow applications to extend its stable interfaces. Hook methods systematically decouple the stable interfaces and behaviors of an application domain from the variations required by instantiations of an application in a particular context. Framework extensibility is essential to ensure timely customization of new application services and features.

Really? I've seen a lot of frameworks that don't do this. I love the last sentence. You know what you end up with? A framework that is completely defeated with customized hooks.

Inversion of control -- The run-time architecture of a framework is characterized by an ``inversion of control.'' This architecture enables canonical application processing steps to be customized by event handler objects that are invoked via the framework's reactive dispatching mechanism. When events occur, the framework's dispatcher reacts by invoking hook methods on pre-registered handler objects, which perform application-specific processing on the events. Inversion of control allows the framework (rather than each application) to determine which set of application-specific methods to invoke in response to external events (such as window messages arriving from end-users or packets arriving on communication ports).

Oh, thrilling. So what this means is that your workflow (canonical application processing steps) are scattered throughout your application-specific processing in the form of custom messaging and event handlers. MMmm. That sounds real easy to maintain.

It's a good article. I'm sure it fools many people. It's obsolete. Yet another article produced by academia that 1) has no bearing on reality, 2) speaks of lofty goals without providing any clues as to how to go about achieving them. Design patterns? That's only the tip of the iceberg.

Marc

Latest AAL Article
My blog
Join my forum!
GeneralRe: Why 99.9% of GDI+ samples... Pin
Tanzim Husain15-Apr-03 20:35
Tanzim Husain15-Apr-03 20:35 
GeneralRe: Why 99.9% of GDI+ samples... Pin
15-Apr-03 23:02
suss15-Apr-03 23:02 
GeneralRe: Why 99.9% of GDI+ samples... Pin
Marc Clifton8-Nov-03 2:29
mvaMarc Clifton8-Nov-03 2:29 

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

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