Gdiplus How to use

Source: Internet
Author: User

GDI is the abbreviation for the graphics device interface, meaning the interface of the graphical devices, whose main task is to exchange information between the system and the drawing program, and to process the graphics output of all Windows programs.

Under the Windows operating system, the vast majority of applications with graphical interface can not be separated from GDI, we use the many functions provided by GDI to easily output graphics, text and other operations on the screen, printers and other output devices. The advent of GDI so that programmers do not need to care about hardware devices and device drivers, you can convert the output of the application into the output of hardware devices, the implementation of the program developers and hardware equipment isolation, greatly facilitates the development work.

GDI + is a subsystem in Windows XP that is primarily responsible for information about the display screen and printing device output, which is a set of application programming interfaces implemented through C + + classes. As the name implies, GDI + is an inheritor of previous versions of GDI, and for compatibility reasons, Windows XP still supports previous versions of GDI, but when developing new applications, developers should use GDI + in order to meet graphics output because GDI + GDI was optimized for previous versions of Windows and added many new features.

GDI + as a graphical device interface allows application developers to output screen and printer information without regard to the details of the specific display device, they simply call the GDI + library output of the class to complete the graphics operation, the actual drawing work by these methods to the specific device driver to complete, GDI + makes graphics hardware and applications isolated from each other. This makes it very easy for developers to write device-independent applications.


Gdiplus How to use:

Development tools: VS2008

1. Create a new dialog box application


2. Copy the Gdiplus_includes folder, lib file, Gdiplus.dll file into the application

3. Add the following code to the StdAfx.h file
GDI + #define UNICODE#IFNDEF ulong_ptr#define ulong_ptr unsigned long* #endif//#include "c:\gdi+\includes\gdiplus.h"   ////Please modify the path for your header file//using namespace Gdiplus;  #pragma comment (lib, "C:\\gdi+\\lib\\gdiplus.lib")////please modify the path to your. lib file # include ": /gdiplus_includes\\gdiplus.h "using namespace GdiPlus; #pragma comment (lib,"). /gdiplus_lib\\gdiplus.lib ")

4. In the InitInstance () function of the C**app.cpp file, before C**dlg Dlg, add the following code:
Gdiplusstartupinput Gdiplusstartupinput; Ulong_ptr           Gdiplustoken; Gdiplusstartup (&gdiplustoken, &gdiplusstartupinput, NULL);

After Dlg, add the following code before return:
Close the Gdiplus Environment Gdiplusshutdown (Gdiplustoken);

Such as:
BOOL cgdiplusdemoapp::initinstance () {//If an application manifest running on Windows XP specifies that you want//use ComCtl32.dll version 6 or later to enable visualization,//You need to initcom Moncontrolsex (). Otherwise, the window cannot be created. Initcommoncontrolsex initctrls;initctrls.dwsize = sizeof (initctrls);//Set it to include all//common control classes to be used in the application. INITCTRLS.DWICC = Icc_win95_classes;initcommoncontrolsex (&initctrls); CWinApp::InitInstance (); AfxEnableControlContainer ();//Standard initialization//If these features are not used and you want to reduce//final executable size, remove the following//unwanted specific initialization routines//change the registry key used to store settings//TODO: The string should be modified appropriately, for example, to the company or organization name Setregistrykey (_t ("native application generated by Application Wizard"); Gdiplusstartupinput Gdiplusstartupinput; ULONG_PTR Gdiplustoken; Gdiplusstartup (&gdiplustoken, &gdiplusstartupinput, NULL); Cgdiplusdemodlg Dlg;m_pmainwnd = &dlg;int_ptr Nresponse = dlg.  DoModal (); if (nresponse = = IDOK) {//TODO: The code in this place processing when//"OK" to close the dialog box}else if (nresponse = = IDCANCEL) {//TODO: When to place processing in// "Cancel" closes the code for the dialog box}//close the Gdiplus environment Gdiplusshutdown (gdiplustoken);//Because the dialog box is closed, FALSE is returned to exit the application,//instead of launching the application's message pump. return FALSE;}


5.c**dlg Action OnInitDialog: Set dialog box location and size, get resource information
OnPaint: Drawing

Header file
Private:image *m_pimagebackground;int m_nbackgroundwidth, M_nbackgroundheight;image *m_pImagePic;int m_nPicWidth, m_ Npicheight;private:void myinitdialog (); void Initui ();//set interface initialization information void Getimagesource ();//Get picture information void Mypaint (); void Drawbackground (hdc hdc);//Draw background void Drawpic (HDC hdc);//Draw picture//Get picture information bool Imagefromidresource (UINT NID, LPCTSTR STR, Image *&pimg); BOOL Imagefromidresource (CString sfilepathname, Image *&pimg);

source file
void Cgdiplusdemodlg::myinitdialog () {Initui (); Getimagesource ();} void Cgdiplusdemodlg::mypaint () {CPAINTDC DC (this); CRect rcclient; GetClientRect (&rcclient); CDC Dcmem; CBitmap Bmpmem;dcmem.createcompatibledc (&AMP;DC); if (Bmpmem.createcompatiblebitmap (&dc,rcclient.width (), Rcclient.height ())) {cbitmap* poldbmp = Dcmem.selectobject (&AMP;BMPMEM); CWnd::D Efwindowproc (WM_PAINT, (WPARAM) dcmem.m_hdc,0);D Rawbackground (DCMEM.M_HDC);D rawpic (DCMEM.M_HDC);d C. BitBlt (0,0,rcclient.right,rcclient.bottom,&dcmem,0,0,srccopy);d cmem.selectobject (poldbmp); Bmpmem.deleteobject ();} Dcmem.deletedc ();} void Cgdiplusdemodlg::initui () {//dialog box background picture width and height const int nbkbmpwidth = 800;const int nbkbmpheight = 450;// Get the pixel width and pixel height of your computer monitor int ax = GetDC ()->getdevicecaps (horzres)-nbkbmpwidth;   int ay = GetDC ()->getdevicecaps (vertres)-Nbkbmpheight;int nwidth = 0; int nheight = 0;if (ax <= 0) {ax = 0;} Else{ax = AX/2;} if (ay <=0) {ay = 0;} Else{ay = AY/2;} RECT Clientrect;clientrect.left = Ax;clientrect.top = Ay;clientrecT.right = clientrect.left + nbkbmpwidth;clientrect.bottom = clientrect.top + nbkbmpheight; MoveWindow (&clientrect);} void Cgdiplusdemodlg::getimagesource () {Imagefromidresource (idb_png_bg,_t ("PNG"), m_pimagebackground); m_ Nbackgroundwidth = M_pimagebackground->getwidth (); m_nbackgroundheight = M_pimagebackground->getheight (); CString spath = _t ("Res\\hd.png"); Imagefromidresource (spath,m_pimagepic)//imagefromidresource (IDB_PNG_HD,_T ("png "), m_pimagepic); m_npicwidth = M_pimagepic->getwidth (); m_npicheight = M_pimagepic->getheight ();} void Cgdiplusdemodlg::D rawbackground (hdc hdc) {RECT RCT; GetWindowRect (&AMP;RCT); Point Ptwinpos={rct.left,rct.top}; Graphics graph (HDC); Point points[] = {Point (0, 0), point (m_nbackgroundwidth, 0), point (0, M_nbackgroundheight),};graph. DrawImage (M_pimagebackground, points, 3); graph. RELEASEHDC (HDC);} void Cgdiplusdemodlg::D rawpic (hdc hdc) {RECT RCT; GetWindowRect (&AMP;RCT); Point Ptwinpos={rct.left,rct.top}; Graphics graph (HDC); int nhoroffset = 50;int nverofFset = 50; Point points[] = {Point (Nhoroffset, Nveroffset), point (Nhoroffset+m_npicwidth, Nveroffset), point (Nhoroffset, Nveroffset+m_npicheight),};graph. DrawImage (M_pimagepic, points, 3); graph. RELEASEHDC (HDC);} BOOL Cgdiplusdemodlg::imagefromidresource (UINT NID, LPCTSTR STR, Image *&pimg) {hinstance HInst = AfxGetResourceHandle (); Hrsrc hrsrc =:: FindResource (Hinst,makeintresource (NID), STR); Typeif (!HRSRC) return false;//load resource into Memorydword len = Sizeofresource (HInst, HRSRC); byte* lprsrc = (byte*) loadresource (HInst, HRSRC), if (!LPRSRC) return false;//Allocate Global memory on which to create Str Eamhglobal M_hmem = GlobalAlloc (gmem_fixed, Len); byte* Pmem = (byte*) globallock (M_HMEM); memcpy (Pmem,lprsrc,len); istream* pstm; CreateStreamOnHGlobal (m_hmem,false,&pstm);//Load from Streampimg=gdiplus::image::fromstream (pstm);//free/ Release Stuffglobalunlock (M_HMEM);p stm->release (); FreeResource (LPRSRC); return TRUE;} BOOL Cgdiplusdemodlg::imagefromidresource (CString sfilepAthname, Image *&pimg) {//load file into Memorydword len = 0; HANDLE Hfile;dword Size1,readbyte;hfile=createfile (sfilepathname,generic_read,file_share_read,null,open_existing , file_attribute_normal| File_flag_sequential_scan,null); if (Invalid_handle_value = = hfile) {return FALSE;} Len=getfilesize (HFILE,&AMP;SIZE1); if (0xFFFFFFFF = = len) {CloseHandle (hfile); return FALSE;} Allocate global memory on which to create Streamhglobal M_hmem = GlobalAlloc (gmem_fixed, Len); byte* Pmem = (byte*) globallock (M_HMEM); ReadFile (hfile,pmem,len,&readbyte,null); istream* pstm; CreateStreamOnHGlobal (m_hmem,false,&pstm);//Load from Streampimg=gdiplus::image::fromstream (pstm);//free/ Release Stuffglobalunlock (M_HMEM);p stm->release (); return TRUE;}

Engineering

Run effect


SOURCE download


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Gdiplus How to use

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.