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

本文介绍如何在MFC项目中轻松集成GDI+,包括必要的头文件包含、资源初始化与释放步骤,并通过示例展示GDI+的强大功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 aboutGDI+. However, if you try to find information about integratingGDI+ 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 useGDI+ on Windows version lower than XP, then you would have to redistribute theGDI+ 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 yourCWinApp 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 toExitInstance():
    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 intoGDI+. If you ask why? Consider there is oneGDI+ 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 linkhere 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 toGDI+. In some cases, 100 lines of drawing code can be reduced to just 5 lines withGDI+ intensive support of complex drawing exercises. This means mixing ordinary GDI withGDI+ 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 MSDNGDI+ 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

 

http://www.codeproject.com/Articles/3962/Using-GDI-with-MFC-or-native-C-C

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值