首页 > 代码库 > GDI+

GDI+

1,

编译error的话一般是却

#include <comdef.h>
#include <Windows.h>

Windows.h内会包含Windows.h,但是因为在stdafx.h中会智能创建WIN32_LEAN_AND_MEAN宏,会屏蔽comdef.h,这样就会导致声明缺失问题。

删除WIN32_LEAN_AND_MEAN宏或者手动添加包含comdef.h头文件就可以了。

 

2,

#include <comdef.h>#include <Windows.h>#include "GDIPlusB.h"#include <gdiplus.h> using namespace Gdiplus; #pragma comment(lib,"gdiplus.lib")void CGDIPlusB::InitInstance(){
  //GDI+的初始化 Gdiplus::GdiplusStartupInput gdiplusStartupInput; Gdiplus::GdiplusStartup(
&m_gdiplusToken , &gdiplusStartupInput, NULL); }
void CGDIPlusB::UnInstance
(){
  //关闭GDI+ Gdiplus::GdiplusShutdown(m_gdiplusToken);}

 

3,

void CGDIPlusB::DrawAlphaPen( HDC* phdc  ) {    COLORREF    crBackClr = RGB( 0,0,255);    COLORREF    crDark = RGB( 0 , 255 , 0 );    COLORREF    crBright = RGB( 255,255,255);       Color   GdiClrD,GdiClrB;//用于产生渐变效果;    GdiClrD.SetFromCOLORREF(crDark);       GdiClrB.SetFromCOLORREF(crBright);       int   r,g,b,r1,g1,b1;//分别得到明色和暗色的R   G   B值       r   =   GdiClrD.GetRed();       g   =   GdiClrD.GetGreen();       b   =   GdiClrD.GetBlue();       r1   =   GdiClrB.GetRed();       g1   =   GdiClrB.GetGreen();       b1   =   GdiClrB.GetBlue();       int   alpha   =  220 ;    //产生渐变的透明画刷       Color   GdiClrDark(alpha,r,g,b) ;       Color   GdiClrBright(alpha,r1,g1,b1) ;       Gdiplus::Point pt_begin , pt_end ;    SetRect( &m_rc , 200 ,200 , 200+260 , 200+149 ) ;    pt_begin.X = m_rc.left ;    pt_begin.Y = m_rc.top ;    pt_end.X = m_rc.right ;    pt_end.Y = m_rc.bottom ;    LinearGradientBrush linGrBrush( pt_begin , pt_end , GdiClrDark,GdiClrBright );    Pen   pen0(&linGrBrush,16 );       Graphics graphics( *phdc );    graphics.DrawLine( &pen0 , pt_begin , pt_end ) ;    graphics.ReleaseHDC( *phdc ) ;}void CGDIPlusB::DrawImage( HDC* phdc  ) {    TCHAR img[MAX_PATH];    _stprintf( img,_T("E:\\liuhanGit\\liuhan_memUI\\memUI\\memUI_SKIN\\p6.png")) ;    Image image( img );     Graphics graphics( *phdc );    graphics.DrawImage( &image, 200,  400 );    graphics.ReleaseHDC( *phdc ) ;}void CGDIPlusB::DrawGradientBrush( HDC* phdc ) {    COLORREF    crBackClr = RGB( 0,0,255);    COLORREF    crDark = RGB( 0 , 255 , 0 );    COLORREF    crBright = RGB( 255,255,255);       Color   GdiClrD,GdiClrB;//用于产生渐变效果;    GdiClrD.SetFromCOLORREF(crDark);       GdiClrB.SetFromCOLORREF(crBright);       int   r,g,b,r1,g1,b1;//分别得到明色和暗色的R   G   B值       r   =   GdiClrD.GetRed();       g   =   GdiClrD.GetGreen();       b   =   GdiClrD.GetBlue();       r1   =   GdiClrB.GetRed();       g1   =   GdiClrB.GetGreen();       b1   =   GdiClrB.GetBlue();       int   alpha   =  220 ;    //产生渐变的透明画刷       Color   GdiClrDark(alpha,r,g,b) ;       Color   GdiClrBright(alpha,r1,g1,b1) ;       Gdiplus::Point pt_begin , pt_end ;    SetRect( &m_rc , 500 ,200 , 500+260 , 200+149 ) ;    pt_begin.X = m_rc.left ;    pt_begin.Y = m_rc.top ;    pt_end.X = m_rc.right ;    pt_end.Y = m_rc.bottom ;    Brush* bsh0 = new LinearGradientBrush(  pt_begin , pt_end , GdiClrDark,GdiClrBright  )  ;    Graphics graphics( *phdc );    graphics.FillRectangle( bsh0 , m_rc.left , m_rc.top  , m_rc.right - m_rc.left ,  m_rc.bottom - m_rc.top ) ;     delete bsh0 ;    graphics.ReleaseHDC( *phdc ) ;}void CGDIPlusB::DrawGradientBrush2( HDC* phdc ) {    Gdiplus::Point pt_begin , pt_end ;    SetRect( &m_rc , 500 ,400 , 500+260 , 400+149 ) ;    pt_begin.X = m_rc.left ;    pt_begin.Y = m_rc.top ;    pt_end.X = m_rc.right ;    pt_end.Y = m_rc.bottom ;    LinearGradientBrush linGrBrush( pt_begin , pt_end  ,Color(255,255,0,0),Color(255,0,0,255));       Color colors[] = {           Color(255, 255, 0, 0),   // red           Color(255, 255, 255, 0), //yellow           Color(255, 0, 0, 255),   // blue           Color(255, 0, 255, 0)};  // green           REAL positions[] = {               0.0f,                  0.33f,                  0.66f,               1.0f};        linGrBrush.SetInterpolationColors(colors, positions,4);      Graphics graphics( *phdc );    graphics.FillRectangle( &linGrBrush , m_rc.left , m_rc.top  , m_rc.right - m_rc.left ,  m_rc.bottom - m_rc.top ) ;     graphics.ReleaseHDC( *phdc ) ;}