封装GDI+函数库

在C++builder中使用GDI+的步骤有些麻烦,将常用的函数封装到一个DLL库中,使用的时候,直接调用,得以简化。

#define STRICT
#include <windows.h>
#include <vcl.h>

#include   "math.hpp"   
#include   <algorithm>
using   std::min;   
using   std::max;   
#include   "gdiplus.h"   

#pragma hdrstop
#pragma   warn   -inl   
#pragma   warn   -8022   

//---------------------------------------------------------------------------
//   Important note about DLL memory management when your DLL uses the
//   static version of the RunTime Library:
//
//   If your DLL exports any functions that pass String objects (or structs/
//   classes containing nested Strings) as parameter or function results,
//   you will need to add the library MEMMGR.LIB to both the DLL project and
//   any other projects that use the DLL.  You will also need to use MEMMGR.LIB
//   if any other projects which use the DLL will be performing new or delete
//   operations on any non-TObject-derived classes which are exported from the
//   DLL. Adding MEMMGR.LIB to your project will change the DLL and its calling
//   EXE's to use the BORLNDMM.DLL as their memory manager.  In these cases,
//   the file BORLNDMM.DLL should be deployed along with your DLL.
//
//   To avoid using BORLNDMM.DLL, pass string information using "char *" or
//   ShortString parameters.
//
//   If your DLL uses the dynamic version of the RTL, you do not need to
//   explicitly add MEMMGR.LIB as this will be done implicitly for you
//---------------------------------------------------------------------------

#pragma argsused
using   namespace   Gdiplus;
ULONG_PTR   GdiplusToken;   
GdiplusStartupInput   GdiplusStartupInput1;
Gdiplus::Graphics * myGP;//画布指针
Pen   * myPen;        //画笔
Brush * myBrush;      //渐变画刷
SolidBrush *sBrush;   //实心画刷
Font  * myFont;       //字体
StringFormat *sformat;//字符串对齐,文字方向
Matrix *M;
PointF Oxy;//坐标原点
//=================================================
HDC m_hdcMemory;  //内存句柄
HDC hdcTemp;      //设备上下文句柄
HBITMAP hBitMap;  //位图句柄
TSize m_size;
//-------------------------------------------------
//extern "C" __declspec(dllexport) void __stdcall GdiInit(); //初始化GDI+
//extern "C" __declspec(dllexport) void __stdcall GdiClose();
//extern "C" __declspec(dllexport) void __stdcall GdiCreateHandle(HWND handle);//创建画布1
//extern "C" __declspec(dllexport) void __stdcall GdiCreateHandle1(HDC handle);//创建画布2
//
//extern "C" __declspec(dllexport) void __stdcall GdiDrawLine(int x1,int y1,int x2,int y2);//画线
//
//extern "C" __declspec(dllexport) void __stdcall GdiPen(unsigned int color,int a);//创建画笔1
//extern "C" __declspec(dllexport) void __stdcall GdiPen1(unsigned int color,int w,int Align,int DashStyle);//创建画笔2
//
//extern "C" __declspec(dllexport) void __stdcall GdiBursh(int x1,int y1,int width,int height,unsigned int color1,unsigned int color2,int mode);//创建画刷1,mode为方向
//extern "C" __declspec(dllexport) void __stdcall GdiBursh1(TPoint &point1,TPoint &point2,unsigned int color1,unsigned int color2);//创建画刷2
//extern "C" __declspec(dllexport) void __stdcall GdiBursh2(TPoint &point1,TPoint &point2,unsigned int color1,unsigned int color2,unsigned int cols[],float bps[],int count);//创建画刷3,多色画刷
//
//
//extern "C" __declspec(dllexport) void __stdcall GdiDrawRectangle(int x1,int y1,int width,int height);//画矩形
//extern "C" __declspec(dllexport) void __stdcall GdiFillRectangle(int x1,int y1,int width,int height);//填充矩形
//
//extern "C" __declspec(dllexport) void __stdcall GdiDrawEllipse(int x1,int y1,int width,int height);//画圆形
//extern "C" __declspec(dllexport) void __stdcall GdiFillEllipse(int x1,int y1,int width,int height);//填充圆形
//
//extern "C" __declspec(dllexport) void __stdcall GdiDarwArc(int x1,int y1,int x2,int y2,float startAngle,float sweepAngle);//画弧
//
//extern "C" __declspec(dllexport) void __stdcall GdiDarwpolygon(TPoint * points,int count);//画多边形
//extern "C" __declspec(dllexport) void __stdcall GdiFillpolygon(TPoint * points,int count,int mode);//填充多边形
//
//extern "C" __declspec(dllexport) void __stdcall GdiClear(unsigned int color);
//
//extern "C" __declspec(dllexport) void __stdcall GdiDrawImage(AnsiString src,int x,int y,int width,int height);
//
//extern "C" __declspec(dllexport) void __stdcall GdiSetFont(AnsiString src,int size);
//
//extern "C" __declspec(dllexport) void __stdcall GdiDrawString(AnsiString src,int ,int y,unsigned int color);
//extern "C" __declspec(dllexport) void __stdcall GdiDrawString1(AnsiString src,int ,int y,int width,int height,float halign,float valign);
//
移动
//extern "C" __declspec(dllexport) void __stdcall GdiTranslateTransform(int dx,int dy);
旋转
//extern "C" __declspec(dllexport) void __stdcall GdiRotateTransform(float r,int driect);
伸缩
//extern "C" __declspec(dllexport) void __stdcall GdiScaleTransform(float x,float y);
还原矩阵
//extern "C" __declspec(dllexport) void __stdcall GdiResetTransform();
设置旋转矩阵:角度,坐标,方向
//extern "C" __declspec(dllexport) void __stdcall GdiRotateAt(float a,TRect &rc,int c);
应用旋转矩阵
//extern "C" __declspec(dllexport) void __stdcall GdiSetTransform();
还原旋转矩阵
//extern "C" __declspec(dllexport) void __stdcall GdiResetMatrix();
打开gif文件
//extern "C" __declspec(dllexport) void __stdcall GdiGif(AnsiString fileName,int x,int y,int w,int h);
测量字符串高宽
字符串,对齐方式
//extern "C" __declspec(dllexport) TSize __stdcall GdiGetTextBounds(const AnsiString& szText,AnsiString fontfamilyStr,float halign,float valign);
画圆角矩形
//extern "C" __declspec(dllexport) void __stdcall GdiDrawRoundRect(INT x, INT y, INT width, INT height, INT cornerX, INT cornerY);
//extern "C" __declspec(dllexport) void __stdcall GdiFillRoundRectangle(int x,int y,int width,int height,int OFFSET_X,int OFFSET_Y);//填充矩形
击中测试
//extern "C" __declspec(dllexport) bool __stdcall GdiPointIsVisible(int x,int y); //测试点击中
//extern "C" __declspec(dllexport) bool __stdcall GdiRecIsVisible(int x,int y,int width,int height);//测试矩形击中

//-------------------------------------------------
//int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
//{
//        return 1;
//}
//---------------------------------------------------------------------------
void  GdiInit()
{
    GdiplusStartup(&GdiplusToken,&GdiplusStartupInput1,NULL);   //初始化GDI+
    myGP=NULL;
    myPen = NULL;
    myBrush = NULL;
    myFont  = NULL;
}
void  GdiClose()
{
    delete myGP;
    GdiplusShutdown(GdiplusToken);   //关闭GDI+
}
//Todo:用窗口句柄,创建画布1
void  GdiCreateHandle(void * handle)
{
    myGP = new Gdiplus::Graphics((HWND)handle, false);
    myGP->SetSmoothingMode(SmoothingModeHighQuality);
}
//Todo:用画布句柄,创建画布2
void  GdiCreateHandle1(void* handle)
{
    //if(myGP == NULL)//delete myGP;
    
    myGP =new Gdiplus::Graphics((HDC)handle);
    //myGP = Gdiplus::Graphics::FromHDC((HDC)handle);
    myGP->SetSmoothingMode(SmoothingModeHighQuality);
}
//Todo:释放画布
void GdiReleaseGraphics()
{
   if(myGP != NULL)
   {
      delete myGP;
      myGP = NULL;
   }
}

//todo:画线
void  GdiDrawLine(int x1,int y1,int x2,int y2)
{
    myGP->DrawLine(myPen,x1,y1,x2,y2);
}
//todo:创建自定义风格线条
void  GdiSetLineStyle(float * array,int Count)
{
   myPen->SetDashPattern(array,Count);
}
//todo:创建画笔1
void  GdiPen(unsigned int color,int w)
{
    if(myPen != NULL)delete myPen;
    myPen = new Pen(color,w);
}
//todo:创建画笔2
//线条的风格
//0实线,1虚线,2点,3虚点线,4虚点点线
//Align 中心对齐,内对齐
void  GdiPen1(unsigned int color,int width,int Align,int DashStyle)
{
    if(myPen != NULL)delete myPen;
    myPen = new Pen(color,width);
    myPen->SetAlignment(Align);
    myPen->SetDashStyle(DashStyle);
}
//todo:创建画刷1
void  GdiBursh(int x1,int y1,int width,int height,unsigned int color1,unsigned int color2,int mode)
{
   RectF rect;
   rect.X   = x1;
   rect.Y   = y1;
   rect.Width  = width;
   rect.Height = height;
   //颜色转换
   Color c1((color1&0xff000000)>>24,(color1&0x00ff0000)>>16,(color1&0x0000ff00)>>8,color1&0x000000ff);
   Color c2((color2&0xff000000)>>24,(color2&0x00ff0000)>>16,(color2&0x0000ff00)>>8,color2&0x000000ff);

   if(myBrush != NULL) delete myBrush;
   myBrush = new LinearGradientBrush(rect,c1,c2,(LinearGradientMode)mode);
}
//todo:创画刷2
void  GdiBursh1(TPoint &point1,TPoint &point2,unsigned int color1,unsigned int color2)
{
    Gdiplus::Point p1,p2;
    p1.X = point1.x;p1.Y = point1.y;
    p2.X = point2.x;p2.Y = point2.y;
    //颜色转换
    //Color c1((color1&0xff000000)>>24,(color1&0x00ff0000)>>16,(color1&0x0000ff00)>>8,color1&0x000000ff);
    //Color c2((color2&0xff000000)>>24,(color2&0x00ff0000)>>16,(color2&0x0000ff00)>>8,color2&0x000000ff);

    if(myBrush != NULL)delete myBrush;
    myBrush = new LinearGradientBrush(p1,p2,color1,color2);

}
//todo:创建多色画刷3
//cols是所有颜色
//bps颜色所占比例
//count颜色数目
void  GdiBursh2(TPoint &point1,TPoint &point2,unsigned int cols[],float bps[],int count)
{
   Color c1,c2,clos1[20];
   Gdiplus::Point p1,p2; //区域起始点和结束点
   p1.X = point1.x;p1.Y = point1.y;
   p2.X = point2.x;p2.Y = point2.y;

   for(int i=0;i<count;i++)
   {
      clos1[i] = cols[i]; //所有颜色
   }

   LinearGradientBrush theBrush(p1,p2,Color::Black,Color::White);//创建画刷
   theBrush.SetInterpolationColors(clos1,bps,count);

   if(myBrush != NULL)delete myBrush;
   myBrush = theBrush.Clone();
}
//todo:画矩形
void  GdiDrawRectangle(int x1,int y1,int width,int height)
{
    myGP->DrawRectangle(myPen,x1,y1,width,height);
}
//todo:渐变画刷填充矩形
void  GdiFillRectangle(int x1,int y1,int width,int height)
{
   myGP->FillRectangle(myBrush,x1,y1,width,height);
}

//todo:画圆形
void  GdiDrawEllipse(int x1,int y1,int width,int height)//画圆形
{
   myGP->DrawEllipse(myPen,x1,y1,width,height);
}
//todo:填充圆形
void  GdiFillEllipse(int x1,int y1,int width,int height)//填充圆形
{
   myGP->FillEllipse(myBrush,x1,y1,width,height);
}
//todo:画弧
void  GdiDarwArc(int left,int top,int width,int height,float startAngle,float sweepAngle)
{
   myGP->DrawArc(myPen,(float)left,(float)top,(float)width,(float)height,startAngle,sweepAngle);
}
//todo:画多边形
void  GdiDarwpolygon(TPoint * ps,int count)
{
   myGP->DrawPolygon(myPen,(Gdiplus::Point *)ps,count);
}
//todo:多边形填充模式0(交替)或1(环绕)
void  GdiFillpolygon(TPoint * points,int count,int mode)
{
   myGP->FillPolygon(myBrush,(Gdiplus::Point *)points,count,FillMode(mode));
}
//todo:单色填充多边形
void GdiFillpolygonSolidBrush(unsigned int color, TPoint * points,int count)
{
    SolidBrush *sBrush = new SolidBrush(color);
    myGP->FillPolygon(sBrush,(Gdiplus::Point *)points,count,FillMode(0));
    delete sBrush;
}
//todo:清空画布
void  GdiClear(unsigned int color)
{
    //颜色转换
    Color c1((color&0xff000000)>>24,(color&0x00ff0000)>>16,(color&0x0000ff00)>>8,color&0x000000ff);
    myGP->Clear(c1);

}
//todo:用文件名,画图片
void  GdiDrawImage(AnsiString src,int x,int y,int width,int height)
{
   WideString imgName(src);
   Image *img = (Gdiplus::Image::FromFile(imgName));
   myGP->DrawImage(img,x,y,width,height);
   delete img;
}
//todo:用image*画图片
void  GdiDrawImage1(Gdiplus::Image * img,int x,int y,int width,int height)
{
   myGP->DrawImage(img,x,y,width,height);
   delete img;
}
//todo:设置字体
void  GdiSetFont(AnsiString FontNameStr,int size)
{
    WideString fontname(FontNameStr);
    if(myFont !=NULL)delete myFont;
    myFont = new Font(fontname.c_bstr(),size);
}
//todo:输出实心文字
void  GdiDrawString(AnsiString src,int x,int y,unsigned int color)
{
    WideString src1(src);
     //颜色转换
    Color c1((color&0xff000000)>>24,(color&0x00ff0000)>>16,(color&0x0000ff00)>>8,color&0x000000ff);
    SolidBrush textBrush(c1);

    myGP->DrawString(src1.c_bstr(),src1.Length(),myFont,Gdiplus::PointF(x,y),&textBrush);
}
//todo:输出文字1,对齐方式
void  GdiDrawString1(AnsiString src,int x,int y,int halign,int valign)
{
    WideString src1(src);
    StringFormat stringFormat;//文字对齐方式

    StringAlignment hsa = StringAlignment(halign);//水平对齐
    StringAlignment vsa = StringAlignment(valign);//垂直对齐

    stringFormat.SetAlignment(hsa);
    stringFormat.SetLineAlignment(vsa);

    PointF p(x,y);

    myGP->DrawString(src1.c_bstr(),src1.Length(),myFont,p,&stringFormat,myBrush);
}
//todo:垂直输出文字
void GdiDrawStringVertical(AnsiString src,int x,int y,unsigned int color)
{
    WideString str(src);
    SolidBrush textBrush(color);//实画刷

    StringFormat stringFormat;
    stringFormat.SetFormatFlags(StringFormatFlagsDirectionVertical);

    PointF p(x,y);

    myGP->DrawString(str.c_bstr(),(INT)wcslen(str),myFont,p,&stringFormat,&textBrush);
}
//todo:移动坐标原点,旋转中心
void  GdiTranslateTransform(int dx,int dy)
{
   Oxy.X = dx;
   Oxy.Y = dy;
   myGP->TranslateTransform(dx,dy);
}
//todo:旋转,角度,方向
void  GdiRotateTransform(float r)
{
   myGP->RotateTransform(r,0);
}
//todo:缩放
void  GdiScaleTransform(float x,float y)
{
   myGP->ScaleTransform(x,y);
}
//todo:还原矩阵
void  GdiResetTransform()
{
   myGP->ResetTransform();
}
//todo:设置旋转矩阵
void  GdiRotateAt(float a,TRect &rc,int c)
{
   if(M)delete M;
   M = new Matrix;
   PointF ps(rc.left+rc.Width()/2.0+Oxy.X/2,rc.top+rc.Height()/2.0+Oxy.Y/2);

   
   M->RotateAt(a,ps);
}
//todo:应用旋转矩阵
void  GdiSetTransform()
{
   myGP->SetTransform(M);
}
//todo:还原旋转矩阵
void  GdiResetMatrix()
{
   M->Reset();
}
//打开gif
void  GdiGif(AnsiString fileName,int x,int y,int w,int h)
{
    WideString imgName(fileName);
    Image* img=Gdiplus::Image::FromFile(imgName);

    int dn = img->GetFrameDimensionsCount();
    GUID *guids = new GUID[dn];
    img->GetFrameDimensionsList(guids,dn);
    int fn = img->GetFrameCount(&guids[0]);
    if(fn<2)
    {
       MessageBox(0,"图象未包含多帧","提示",MB_OK+MB_ICONASTERISK);
       return;
    }

    for(int i=0;i<fn;i++)
    {
      img->SelectActiveFrame(guids,i);
      myGP->DrawImage(img,x,y,w,h);
      Sleep(100);
    }
}
//测量字符串高宽
//字符串,字体族,对齐方式
TSize  GdiGetTextBounds(const AnsiString& szText,AnsiString fontStr,int fontSize)
{
     TSize size;
//     size.cx = 0;
//     size.cy = 0;
//     if(myFont == NULL) return size;
//     
//     StringFormat stringFormat;//文字对齐方式
//     StringAlignment hsa = StringAlignment(halign); //水平
//     StringAlignment vsa = StringAlignment(valign); //垂直
//     stringFormat.SetAlignment(hsa);
//     stringFormat.SetLineAlignment(vsa);
//
//     WideString FStr = fontfamilyStr;//字体
//     Font font(FStr,10,FontStyleRegular);
//
//      FontFamily fontfamily;
//
//      GraphicsPath path; //创建路径对象
//      font.GetFamily(&fontfamily);
//      WideString str = szText;
//
//      path.AddString(str.c_bstr(),str.Length(),&fontfamily,font.GetStyle(),font.GetSize(),PointF(0,0),&stringFormat);
//      RectF rcBound;
//      path.GetBounds(&rcBound);
//
//      size.cx = rcBound.Width;
//      size.cy = rcBound.Height;
//      return size;
    WideString a= fontStr;
    Font gFont(a, fontSize);
    StringFormat stringformat(StringAlignmentNear);

    GraphicsPath graphicsPathObj;
    FontFamily fontfamily;
    gFont.GetFamily(&fontfamily);
    WideString b = szText;
    graphicsPathObj.AddString(b,
                              b.Length()-1,
                              &fontfamily,
                              gFont.GetStyle(),
                              gFont.GetSize(),
                              PointF(0,0),
                              &stringformat);
    RectF rcBound;
    /// 获取边界范围
    graphicsPathObj.GetBounds(&rcBound);
    /// 返回文本的宽高
    size.cx = rcBound.Width;
    size.cy = rcBound.Height;
    return size;


}
//画圆角矩形,组合出来
void  GdiDrawRoundRect(INT x, INT y, INT width, INT height, INT cornerX, INT cornerY)
{
    INT elWid = 2*cornerX;
    INT elHei = 2*cornerY;

    myGP->DrawArc(myPen,x,y,elWid,elHei,180,90); // 左上角圆弧
    myGP->DrawLine(myPen,x+cornerX,y,x+width-cornerX,y); // 上边

    myGP->DrawArc(myPen,x+width-elWid,y, elWid,elHei,270,90); // 右上角圆弧
    myGP->DrawLine(myPen,x+width,y+cornerY, x+width,y+height-cornerY);// 右边

    myGP->DrawArc(myPen,x+width-elWid,y+height-elHei, elWid,elHei,0,90); // 右下角圆弧
    myGP->DrawLine(myPen,x+width-cornerX,y+height, x+cornerX,y+height); // 下边

    myGP->DrawArc(myPen,x,y+height-elHei, elWid,elHei,90,90);
    myGP->DrawLine(myPen,x,y+cornerY, x, y+height-cornerY);
}
//填充圆角矩形:颜色,矩形大小,圆角偏移
void  GdiFillRoundRectangle(int x,int y,int width,int height,int OFFSET_X,int OFFSET_Y)
{  
    //矩形填充的步骤:  
    //1、把圆角矩形画分为四个圆角上分成四个同等的扇形外加三个直角矩形  
    //2、先填充三个直角矩形  
    //3、然后填充四个角上的扇形  
  
    //创建画刷  
    //Brush *brush = new SolidBrush(color);

    //填充三个直角矩形

    myGP->FillRectangle(myBrush,x,y+OFFSET_Y,width,height-OFFSET_Y*2);
    myGP->FillRectangle(myBrush,x+OFFSET_X,y,width-OFFSET_X*2,height);

    //填充四个角上的扇形区
    //填充左上角扇形
    #define PIE_OFFSET 2
    myGP->FillPie(myBrush,x,y,OFFSET_X*2+PIE_OFFSET,OFFSET_Y*2+PIE_OFFSET,180,90);

    //填充右下角的扇形
    myGP->FillPie(myBrush,x+width-(OFFSET_X*2+PIE_OFFSET),y+height-(OFFSET_Y*2+PIE_OFFSET),OFFSET_X*2+PIE_OFFSET,OFFSET_Y*2+PIE_OFFSET,360,90);

    //填充右上角的扇形
    myGP->FillPie(myBrush,x+width-(OFFSET_X*2+PIE_OFFSET),y,(OFFSET_X*2+PIE_OFFSET),(OFFSET_Y*2+PIE_OFFSET),270,90);

    //填充左下角的扇形
    myGP->FillPie(myBrush,x,y+height-(OFFSET_X*2+PIE_OFFSET),(OFFSET_X*2+PIE_OFFSET),(OFFSET_Y*2+PIE_OFFSET),90,90);

}

//"矩形"击中测试2015-5-15
bool  GdiRectIsVisible(int x,int y,int width,int height,TPoint p)
{
   GraphicsPath *path1 = new GraphicsPath();
   path1->AddRectangle(RectF(x,y,width,height));
   bool re = path1->IsVisible((float)p.x,(float)p.y,myGP);
   delete path1;
   return re;
}
//单色画刷,并填充矩形
void  GdiFillRectSolidBrush(unsigned int color,int left,int top,int width,int height)
{
   SolidBrush *sBrush = new SolidBrush(color);
   myGP->FillRectangle(sBrush,left,top,width,height);
   delete sBrush;
}
//实心画刷,填充圆角矩形
void  GdiFillRoundSolidBrush(unsigned int color,int x,int y,int width,int height,int OFFSET_X,int OFFSET_Y)
{
   SolidBrush *sBrush = new SolidBrush(color);

   myGP->FillRectangle(sBrush,x,y+OFFSET_Y,width,height-OFFSET_Y*2);
   myGP->FillRectangle(sBrush,x+OFFSET_X,y,width-OFFSET_X*2,height);

   //填充四个角上的扇形区
   //填充左上角扇形
   #define PIE_OFFSET 2
   myGP->FillPie(sBrush,x,y,OFFSET_X*2+PIE_OFFSET,OFFSET_Y*2+PIE_OFFSET,180,90);

   //填充右下角的扇形
   myGP->FillPie(sBrush,x+width-(OFFSET_X*2+PIE_OFFSET),y+height-(OFFSET_Y*2+PIE_OFFSET),OFFSET_X*2+PIE_OFFSET,OFFSET_Y*2+PIE_OFFSET,360,90);

   //填充右上角的扇形
   myGP->FillPie(sBrush,x+width-(OFFSET_X*2+PIE_OFFSET),y,(OFFSET_X*2+PIE_OFFSET),(OFFSET_Y*2+PIE_OFFSET),270,90);

   //填充左下角的扇形
   myGP->FillPie(sBrush,x,y+height-(OFFSET_X*2+PIE_OFFSET),(OFFSET_X*2+PIE_OFFSET),(OFFSET_Y*2+PIE_OFFSET),90,90);


   delete sBrush;
}
//测量字符串宽度2
TSize GdiMeasureString(AnsiString str1)
{
   WideString str = str1;

   RectF layoutRect(0, 0, 600, 100);

   StringFormat format;
   format.SetAlignment(StringAlignmentNear);

   RectF stringRect;
   myGP->MeasureString(str.c_bstr(),str.Length(), myFont, layoutRect, &format, &stringRect);

   TSize s;
   s.cx = stringRect.Width;
   s.cy = stringRect.Height;

   return s;

}
//从资源中加载图片
Image* GDIGetImageFromResource(char * RES_ID)
{
	//LPCTSTR pResourceName = MAKEINTRESOURCE(nResourceID);

	//HMODULE hInstance = ahInstance;
	HRSRC hResource = FindResource(HInstance,RES_ID,RT_RCDATA);
	if(!hResource)
	{
                ShowMessage("未找到资源");
		return NULL;
	}
	
	//取得资源大小
	DWORD dwResourceSize = SizeofResource(HInstance, hResource);
	if(!dwResourceSize)
	{
		return NULL;
	}

	const void* pResourceData = LockResource(LoadResource(HInstance, hResource));

	if(!pResourceData)
	{
		return NULL;
	}

	HGLOBAL hResourceBuffer = GlobalAlloc(GMEM_MOVEABLE, dwResourceSize);
	if(!hResourceBuffer)
	{
		GlobalFree(hResourceBuffer);
		return NULL;
	}

	void* pResourceBuffer = GlobalLock(hResourceBuffer);
	if(!pResourceBuffer)
	{
		GlobalUnlock(hResourceBuffer);
		GlobalFree(hResourceBuffer);
		return NULL;
	}

	//把资源拷贝到分配的内存中
	CopyMemory(pResourceBuffer, pResourceData, dwResourceSize);

	IStream* pIStream = NULL;	
	if(CreateStreamOnHGlobal(hResourceBuffer, FALSE, &pIStream)==S_OK)
	{
		Image *pImage = Gdiplus::Image::FromStream(pIStream);

		pIStream->Release();

		GlobalUnlock(hResourceBuffer);

		GlobalFree(hResourceBuffer);

		return pImage;
	}

	return NULL;
}
//从资源中显示图片
//资源类型:Icon,Cursor,Bitmap,String,Accelerators,Menu,Dialog,RCData,Version info
void  GdiDrawImageFromRes(TRect &r,char * RES_ID)
{
   Image *img = GDIGetImageFromResource(RES_ID);

   if(img)
   {
     if(myGP)
     {
       myGP->DrawImage(img,(int)r.left,(int)r.top,(int)r.right,(int)r.bottom);
       delete img;
     }
   }
}
//====================================================
//创建内存绘图DC
//====================================================
HDC GdiCreateMemCanvas(int width,int height)
{
    //==============================
    hdcTemp = GetDC(0);//设备上下文环境句柄
    m_hdcMemory = CreateCompatibleDC(hdcTemp);//内存环境句柄
    //该函数创建与指定的设备环境相关的设备兼容的位图
    hBitMap = CreateCompatibleBitmap(hdcTemp,width,height);

    SelectObject(m_hdcMemory, hBitMap);//该函数选择一对象到指定的设备上下文环境中
    return m_hdcMemory;
}
//销毁内存
void GdiCloseMemCanvas()
{
    ReleaseDC(0, hdcTemp); //GetDC
    hdcTemp = NULL;

    DeleteObject(hBitMap);//SelectObject

    DeleteDC(m_hdcMemory);
    m_hdcMemory = NULL;
}
//复制内存到目标Canvas
void GdiMemCanvasCopy(TCanvas * Canvas,TRect rc,int left,int top)/*SRCCOPY*/
{
   BitBlt(Canvas->Handle, rc.Left, rc.Top, rc.Width(), rc.Height(), m_hdcMemory, left, top, SRCCOPY);
}

 
 


  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值