Getting started with GDI + programming (C ++)

Source: Internet
Author: User

To use C ++ for GDI + programming, you must first check whether the system has installed GDI +. Currently, most XP (SP3) has installed GDI +.

[Include the gdiplus. dll file in system32 ].

Development Environment: Windows XP (SP3) + vs2008

 

(1) Use gdiplus

A. include header file # include <gdiplus. h>

Using namespace gdiplus;

B. Import Links to the database # pragma comment (Lib, "gdiplus. lib ")

C. initialize gdiplus: gdiplusstartup (& m_pgditoken, & m_gdiplusstartupinput, null );

Gdiplus: gdiplusstartupinput m_gdiplusstartupinput;

Ulong_ptr m_pgditoken;

/* This sentence code is generally placed in the constructor of the main form. */

D. Uninstall gdiplus: gdiplusshutdown (m_pgditoken );

/* This sentence code is generally placed in the destructor of the main form. */

 

(2) read PNG resources to the image pointer object

  1 Gdiplus::Image* CSupriseDlg::ImageFromIDResource(UINT resID, LPCTSTR resType)
2 {
3 HINSTANCE hInst = AfxGetResourceHandle();
4 HRSRC hRsrc = ::FindResource (hInst,MAKEINTRESOURCE(resID),resType);
5 if (!hRsrc)
6 return NULL;
7 // load resource into memory
8   DWORD len = SizeofResource(hInst, hRsrc);
9 BYTE* lpRsrc = (BYTE*)LoadResource(hInst, hRsrc);
10 if (!lpRsrc)
11 return NULL;
12 // Allocate global memory on which to create stream
13   HGLOBAL m_hMem = GlobalAlloc(GMEM_FIXED, len);
14 BYTE* pmem = (BYTE*)GlobalLock(m_hMem);
15 memcpy(pmem,lpRsrc,len);
16 IStream* pstm;
17 CreateStreamOnHGlobal(m_hMem,FALSE,&pstm);
18 // load from stream
19   Gdiplus::Image* ima=Gdiplus::Image::FromStream(pstm, TRUE);
20 // free/release stuff
21   GlobalUnlock(m_hMem);
22 pstm->Release();
23 FreeResource(lpRsrc);
24
25 return ima;
26 }

 

// Call this function to obtain the image pointer object

 1 // idb_png2 indicates the resource ID "PNG" as the resource type
2 gdiplus: Image * IMA = imagefromidresource (idb_png2, _ T ("PNG "));

 

(3) Use graphics: drawimage (image *, Int, INT)

Instead of graphics: drawimage (image *, Int, INT)

Create a PNG Image

 

The reason is that the DPI of the image may be different from that of the screen.

Generally, PNG is 96 DPI, while the screen device is 76 DPI.

 

Graphics: drawimage (image *, Int, INT)

Parameter 1: Target Image drawn

Parameter 2: Draw the X coordinate of the form in the upper left corner of the image.

Parameter 3: Y coordinate relative to the form in the upper left corner of the image

Parameter 4: Image Width

Parameter 5: Image Length

 

Graphics: drawimage (image *, Int, INT)

Parameter 1: Target Image drawn

Parameter 2: Draw the X coordinate of the form in the upper left corner of the image.

Parameter 3: Y coordinate relative to the form in the upper left corner of the image

 

As shown in:

UseGraphics: drawimage (image *, Int, INT)Draw a PNG Image [consistent with other image viewing software]

M_graphics-> drawimage (Ima, 0, 0, picwidth, picheight );

 

UseGraphics: drawimage (image *, Int, INT)Draw a PNG Image [larger]

M_graphics-> drawimage (Ima, 0, 0 );

(4) Prevent flickering

 

 1 BOOL CGdiTestDlg::OnEraseBkgnd(CDC* pDC)
2 {
3 // TODO: Add your message handler code here and/or call default
4
5 //return CDialog::OnEraseBkgnd(pDC);
6   return TRUE;
7 }

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.