GDI + for vc6 MFC

Source: Internet
Author: User

Link: http://www.diybl.com/course/3_program/c++/cppjs/2008219/100318.html #

Author information: www.diybl.com time: 2008-02-19 Author: anonymous name Edit: This Site

Five years ago, I used GDI + to develop programs in C. During this period of time, when using vc6 for plotting, the deep cut will lead to various inconveniences of using GDI, which is no better than using GDI +. Indulge in the efficiency and lightness of vc6, and don't want to install the heavy. Net just for a single GDI +, it raises a question. Is it that GDI + can only be used in. Net?

After visiting Microsoft's msdn website, the mystery was immediately solved. So we have this article-how to use GDI + in vc6 MFC.

GDI + can be applied to two-dimensional vector images, raster images, and typography. It is an interface designed for C/C ++ programmers to present in the C ++ class, it can be used in all Windows-based applications. As a successor to GDI, GDI + has added new features in GDI, such as text anti-aliasing, gradient brushes, Alpha Fusion, and many existing features of GDI. In addition, GDI + has changed the programming mode to make it more flexible and easy to use.

GDI + does not exist in. net. It is actually provided by Windows XP or Windows Server 2003 operating system. It is packaged in a file named gdiplus. dll. This file is located in the corresponding folder under C:/Windows/winsxs. Take the author's XP system as an example. There are two versions, and the corresponding folders are:

X86_Microsoft.Windows.GdiPlus_6595b64144ccf1df_1.0.0.0_x-ww_8d353f13
X86_Microsoft.Windows.GdiPlus_6595b64144ccf1df_1.0.2600.2180_x-ww_522f9f82

In the application, we do not need to specify the version number. The system automatically selects the latest version. Assume that we have compiled a GDI + application in VC6. In the degbug version, choose Build> Start Debug> Go to Start debugging, And Then terminate the application, the following information is displayed in the Output area of VC6:
Loaded ''c:/WINDOWS/WinSxS/x86_Microsoft.Windows.GdiPlus_6595b64144ccf1df_1.0.2600.2180_x-ww_522f9f82/GdiPlus. dll''
The system automatically selects the latest version.

Just as Windows is written in C language, GdiPlus. dll is implemented using C functions and is not encapsulated as C ++ classes, causing inconvenience to non-C programmers. Similar to MFC, Microsoft once again packages these C functions into C ++ classes, providing C ++ programmers with GdiPlus. h leads 30 head files and a GdiPlus. lib. Therefore, if you are a C programmer, you only need GdiPlus. dll. C ++ programmers also need to find header files such as GdiPlus. h and GdiPlus. lib files.

For Windows XP or Windows Server 2003, use the built-in GdiPlus. dll to avoid conflicts with system services. This file is required for Windows Versions earlier than Windows XP, such as Windows NT 4.0 SP6, Windows 2000, Windows 98, and Windows Me. You can download and distribute the gdiplus_dnld.exe file at the soft official address. The address is:
Http://www.microsoft.com/downloads/details.aspx? FamilyID = 6a63ab9c-df12-4d41-933c-be590feaa05a & DisplayLang = en

This file is a self-decompressed file, which should be decompressed to the application directory, but should not be installed in the system directory.

C ++ programmers should also find GdiPlus. h and GdiPlus. lib. These files are included in the file named gdiplus.zip below:
Http://www.crazy-bit.com/download/gdiplus.zip

The following addresses provide all the resources in one stop, including the header files such as gdiplus. H, gdiplus. Lib, and gdiplus. dll:
Http://www.codersource.net/samples/mfcgdiplus.zip

There are different reference methods based on different decompression methods.

The first method is direct reference. Copy gdiplus. h and gdiplus. lib to the corresponding include and Lib directories of VC respectively. The two directories can be queried through the toos-> options-> directories tab of vc6, and select include files and library files in the show directories for drop-down list box respectively. For example, include includes three paths: vc98/include, vc98/mfc/include, and vc98/ATL/include, which correspond to non-MFC applications respectively, different paths used by the MFC and ATL applications.

Then add the following statement to stdafx. h:

# Define ulong_ptr ulong
# Include <gdiplus. h>
Using namespace gdiplus;
# Pragma comment (Lib, "gdiplus. lib ")

The ulong_ptr data type is used in gdiplusinit. h, but this type is not defined in vc6. Therefore, the # include statement must be defined first. # Pragma comment (Lib, "gdiplus. lib") indicates to search for gdiplus. Lib in the application directory or system lib directory during connection.

The second method is to decompress it to any folder and specify the specific path in stdafx. h.

# Define ULONG_PTR ULONG
# Include "C:/gdiplus/Include/gdiplus. h"
Using namespace Gdiplus;
# Pragma comment (lib, "C:/gdiplus/lib/gdiplus. lib ")

In addition to using the above method to connect gdiplus. in addition to lib, you can also enter gdiplus in Project> Settings> Link> Object/library modules. lib (multiple libraries can be separated by spaces, but GDI + only has one library ). This method is not used to input # pragma comment in StdAfx. h.

Before using GDI +, you must start GDI + through GdiplusStartup (). After using it, call GdiplusShutdown () to clear the scene. According to the characteristics of MFC, it should be placed in InitInstance () and ExitInstance () of the XXXApp class accordingly.

Add two private member variables for the XXXApp class first:
Private:
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;

Then, in InitInstance (), call GdiplusStartup () before m_pMainWnd-> ShowWindow () and m_pMainWnd-> UpdateWindow ():

GdiplusStartup (& gdiplusToken, & gdiplusStartupInput, NULL );

// The one and only window has been initialized, so show and update it.
M_pmainwnd-> showwindow (sw_show );
M_pmainwnd-> updatewindow ();
Return true;

NOTE: If gdiplusstartup () is placed after showwindow () and updatewindow (), when the window is displayed for the first time, the image drawn by GDI + is not displayed because it is not started yet, until the second wm_paint message is sent.

In exitinstance (), write the following code:

Gdiplusshutdown (gdiplustoken );
Return cwinapp: exitinstance ();

Now, you have prepared the GDI + and can use it.

In the ondraw () method of the xxxview class:

Graphics graphics (PDC-> m_hdc );
 
Pen (color (50,255, 0,255), 15 );
Pen. setdashstyle (dashstyledash );
Pen. setstartcap (linecaproundanchor );
Pen. setendcap (linecaparrowanchor );
 
Graphics. drawline (& pens, 20, 20,300,100 );
Graphics. drawline (& pen, 300,100,600,100 );

Use the member variable m_hdc of the PDC parameter of the ondraw () method to construct a graphics instance graphics on the stack, create a pen and set it, and then use the drawline () of graphics () method to draw a straight line.

Compared with GDI, GDI + does not necessarily select pen, brush, and other objects into DC for drawing. Instead, it creates a graphics instance by using the DC handle. Then, it is used directly for drawing. This style greatly facilitates programmers.

So far, we can use GDI + in vc6 MFC to describe a bright future. :)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.