GDI + learning notes: GDI + Environment Initialization

Source: Internet
Author: User

Author: Zhu Jincan

Source: http://blog.csdn.net/clever101/


Although I have been familiar with GDI + before, I am determined to systematically learn GDI + from now on. The textbook used is "proficient in GDI programming". In vs 2010 or earlier versions, the vs compiler uses GDI + to initialize the GDI + Environment (MFC 10 in vs 2010 depends on GDI +, so no Initialization is required ).

The procedure for initializing the GDI + Environment of vs 2003, VS 2005, and vs 2008 is the same.

Add a data member to the application class to protect permissions:

Ulong_ptr m_gdiplustoken;


The implementation file of the application class contains the header file of GDI +:


# Include <gdiplus. h>

Add gdiplus. lib to the additional library of the project.


Then add the following initialization code to the initinstance of the application class:


Bool C *** app: initinstance () <br/>{< br/> gdiplus: gdiplusstartupinput startupinput; <br/> gdiplusstartup (& m_gdiplustoken, & startupinput, null); <br/>}< br/>

The above code initializes the GDI + resources.


Add the following code to the initinstance of the application class:


Int C *** app: exitinstance () <br/>{< br/> // todo: add dedicated code and/or call the base class </P> <p> gdiplus: gdiplusshutdown (m_gdiplustoken); <br/> return _ super: exitinstance (); <br/>}< br/>

The purpose of the above Code is to destroy the GDI + resources.

For details about how to use the GDI + Library in VC 6.0, refer to this article: two methods of using GDI + in vc6.0.


Now, let's test whether the GDI + Environment Initialization is successful. We use the class interface of GDI + to draw a string in the view client area. The specific code is as follows:


CDC * PDC = pview-> getdc (); <br/> gdiplus: Graphics graphics (PDC-> m_hdc); <br/> gdiplus: Pen (gdiplus :: color (0,255, 0,255); <br/> gdiplus: solidbrush brush (gdiplus: color (,); <br/> gdiplus :: fontfamily fontfm (L ""); <br/> gdiplus: Font font (& fontfm, 24, gdiplus: fontstyleregular, gdiplus: Unitel ); <br/> crect RT; <br/> pview-> getclientrect (& RT); <br/> gdiplus: pointf (RT. width ()/2, Rt. height ()/2); <br/> graphics. drawstring (L "GDI + program diagram",-1, & font, pointf, & brush); <br/> graphics. releasehdc (PDC-> m_hdc); <br/> pview-> releasedc (PDC); <br/>



As follows:





Note:


1. to use the GDI + Library in the DLL, you only need to include gdiplus. H and gdiplus. lib, the initialization of the GDI + environment only needs to be done in the main call program, otherwise, the error of DLL re-entry may easily occur when the GDI + environment is initialized in the DLL initialization code (I have made such an error before ).


2. the GDI + interface parameter uses the Unicode Character Set, because the string parameter must be a Unicode character when calling any GDI + class interface. In the multi-Byte Character Set environment, constant strings can be developed through L macro conversion. for Variable Multi-byte characters to unicode characters, you can use the Windows API function multibytetowidechar or the a2w macro of ATL.


3. The objects of GDI + are the same as those of the GDI handle, which also occupy resources. objects that use too many GDI + at a time may even crash the program. Therefore, the resources occupied by unnecessary GDI + objects must be released at any time. For example: graphics. releasehdc (PDC-> m_hdc ).


























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.