Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class MemoryDC
- {
- private:
- HDC memoryDC;
- public:
- MemoryDC ()
- {
- HDC hdc=GetDC(GetDesktopWindow());
- memoryDC=CreateCompatibleDC(hdc);
- ReleaseDC(GetDesktopWindow(),hdc);
- }
- operator HDC() const
- {
- return memoryDC;
- }
- ~MemoryDC ()
- {
- DeleteDC(memoryDC);
- }
- };
- class BitmapDC
- {
- private:
- MemoryDC hdcbitmap;
- HGDIOBJ bitmapold;
- HBITMAP bitmapcurrent;
- int intwidth;
- int intheight;
- void init(int width, int height)
- {
- bitmapcurrent = CreateBitmap(width, height, 1, 32, NULL);
- bitmapold = SelectObject(hdcbitmap, bitmapcurrent);
- intwidth=width;
- intheight=height;
- }
- void destroy()
- {
- SelectObject(hdcbitmap, bitmapold);
- DeleteObject(bitmapcurrent);
- }
- public:
- BitmapDC(int width, int height)
- {
- init(width, height);
- }
- BitmapDC()
- {
- init(1, 1);
- }
- void size(int width, int height)
- {
- destroy();
- init(width, height);
- }
- int Width()
- {
- return intwidth;
- }
- int Height()
- {
- return intheight;
- }
- BitmapDC& operator= (const BitmapDC &bitmapsource)
- {
- if (this == &bitmapsource) // Same object?
- return *this;
- destroy();
- init(bitmapsource.intwidth, bitmapsource.intheight);
- BitBlt(bitmapsource, 0, 0, intwidth, intheight, bitmapsource, 0, 0, SRCCOPY);
- return *this;
- }
- BitmapDC& operator= (const HBITMAP &bitmapsource)
- {
- destroy();
- BITMAP bm;
- GetObject(bitmapsource,sizeof(bm),&bm);
- init(bm.bmWidth, bm.bmHeight);
- DrawHBITMAPtoHDC(bitmapsource,hdcbitmap);
- return *this;
- }
- //testing the bitmao value if is nullptr
- bool operator != ( nullptr_t ) const
- {
- return bitmapcurrent != nullptr;
- }
- bool operator==(const BitmapDC &other) const
- {
- return (other.bitmapcurrent == this->bitmapcurrent);
- }
- bool operator!=(const BitmapDC &other) const
- {
- return !(*this == other);
- }
- operator HBITMAP() const
- {
- return bitmapcurrent;
- }
- operator HDC() const
- {
- return hdcbitmap;
- }
- ~BitmapDC()
- {
- destroy();
- }
- };
- class image
- {
- private:
- ULONG_PTR m_gdiplusToken;
- Gdiplus::GdiplusStartupInput gdiplusStartupInput;
- BitmapDC HBitmap;
- Image *img=NULL;
- bool isimgused=false;
- bool isGDIPLUSIniciated=false;
- int imageheight=0;
- int imageweight=0;
- int framecount=0;
- int intSelectFrame=0;
- int framedelay=0;
- string strfilename="";
- Gdiplus::Color clrBackColor=Gdiplus::Color::Blue;
- bool blnTransparent=true;
- HPEN imgPen;
- HBRUSH imgBrush;
- CHOOSEFONT chFont;
- void readimagefile(string filename)
- {
- if (isimgused==true)
- delete img;
- strfilename=filename;
- if(toupper(filename[filename.size()-3])=='C' && toupper(filename[filename.size()-2])=='U' && toupper(filename[filename.size()-1])=='R')
- {
- isimgused=false;
- //create the transparent icon handle
- HCURSOR hicon = (HCURSOR)LoadImage(NULL, filename.c_str(), IMAGE_CURSOR, imageweight, imageheight, LR_LOADFROMFILE|LR_SHARED|LR_DEFAULTSIZE|LR_LOADTRANSPARENT);
- ICONINFO ii;
- BOOL fResult = GetIconInfo(hicon, &ii);
- if (fResult)
- {
- BITMAP bm;
- fResult = GetObject(ii.hbmMask, sizeof(bm), &bm) == sizeof(bm);
- if (fResult)
- {
- imageweight= bm.bmWidth;
- imageheight= ii.hbmColor ? bm.bmHeight : bm.bmHeight / 2;
- }
- if (ii.hbmMask)
- DeleteObject(ii.hbmMask);
- if (ii.hbmColor)
- DeleteObject(ii.hbmColor);
- }
- HBitmap.size(imageweight,imageheight);
- DrawIconEx(HBitmap,0,0,hicon,imageweight,imageheight,0,0,DI_NORMAL);
- framecount=1;
- DestroyCursor(hicon);
- }
- else
- {
- isimgused=true;
- Gdiplus::Image img2(towstring(filename).c_str());
- imageweight=img2.GetWidth();
- imageheight=img2.GetHeight();
- HBitmap.size(imageweight,imageheight);
- Gdiplus::Graphics graphics(HBitmap);
- graphics.DrawImage(&img2, 0,0,imageweight,imageheight);
- UINT count = 0;
- count = img2.GetFrameDimensionsCount();
- GUID* pDimensionIDs = (GUID*)malloc(sizeof(GUID)*count);
- img2.GetFrameDimensionsList(pDimensionIDs, count);
- framecount=img2.GetFrameCount(&pDimensionIDs[0]);
- framedelay =img2.GetPropertyItemSize(PropertyTagFrameDelay);
- img=new Image(towstring(filename).c_str());
- isimgused=true;
- free(pDimensionIDs);
- }
- }
- HICON HICONFromHBITMAP(HBITMAP bitmap)
- {
- BITMAP bmp;
- GetObject(bitmap, sizeof(BITMAP), &bmp);
- HBITMAP hbmMask = CreateCompatibleBitmap(HBitmap, bmp.bmWidth, bmp.bmHeight);
- ICONINFO ii = {0};
- ii.fIcon = TRUE;
- ii.hbmColor = bitmap;
- ii.hbmMask = hbmMask;
- HICON hIcon = CreateIconIndirect(&ii);
- DeleteObject(ii.hbmColor);
- DeleteObject(ii.hbmMask);
- return hIcon;
- }
- public:
- void resize(const int width, const int height)
- {
- imageheight=height;
- imageweight=width;
- HBitmap.size(imageweight,imageheight);
- }
- image()
- {
- if (isimgused==true)
- delete img;
- isimgused=false;
- if(isGDIPLUSIniciated==false)
- {
- Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
- isGDIPLUSIniciated=true;
- }
- imageheight=1;
- imageweight=1;
- HBitmap.size(imageweight,imageheight);
- }
- image(const int width, const int height)
- {
- if (isimgused==true)
- delete img;
- isimgused=false;
- if(isGDIPLUSIniciated==false)
- {
- Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
- isGDIPLUSIniciated=true;
- }
- framecount=1;
- imageheight=height;
- imageweight=width;
- HBitmap.size(imageweight,imageheight);
- }
- image( const string & filename)
- {
- if(isGDIPLUSIniciated==false)
- {
- Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
- isGDIPLUSIniciated=true;
- }
- readimagefile(filename);
- }
- image (const image &cSource)
- {
- if(isGDIPLUSIniciated==false)
- {
- Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
- isGDIPLUSIniciated=true;
- }
- readimagefile(cSource.strfilename);
- BitBlt(HBitmap,0,0,imageweight,imageheight,cSource.HBitmap,0,0,SRCCOPY);
- }
- image& operator= (const image &cSource)
- {
- if (this == &cSource) // Same object?
- return *this;
- if(isGDIPLUSIniciated==false)
- {
- Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
- isGDIPLUSIniciated=true;
- }
- readimagefile(cSource.strfilename);
- BitBlt(HBitmap,0,0,imageweight,imageheight,cSource.HBitmap,0,0,SRCCOPY);
- return *this;
- }
- void Pen(int PenStyle, int PenSize, COLORREF PenColor)
- {
- imgPen=CreatePen(PenStyle,PenSize,PenColor);
- }
- void Pen(HPEN hPen)
- {
- imgPen=hPen;
- }
- void Brush(HBRUSH BrushColor)
- {
- imgBrush = BrushColor;
- }
- void Brush(COLORREF BrushColor)
- {
- imgBrush=CreateSolidBrush(BrushColor);
- }
- void Brush(HBITMAP BrushImage)
- {
- imgBrush=CreatePatternBrush(BrushImage);
- }
- void Brush(int HatchedStyle, COLORREF HatchedColor )
- {
- imgBrush=CreateHatchBrush(HatchedStyle, HatchedColor);
- }
- void DrawRectangle(int PosX, int PosY, int Width, int Height, int EllipseWidth=0, int EllipseHeight=0)
- {
- HBRUSH hBackBrush=imgBrush;
- HBRUSH oldBrush=(HBRUSH)SelectObject(HBitmap,hBackBrush);
- HPEN hBackPen=(HPEN)imgPen;
- HPEN oldPen=(HPEN)SelectObject((HDC)HBitmap,hBackPen);
- RoundRect(HBitmap, PosX, PosY, Width + PosX, Height + PosY, EllipseWidth, EllipseHeight);
- SelectObject(HBitmap,oldBrush);
- DeleteBrush(hBackBrush);
- SelectObject(HBitmap,oldPen);
- DeleteBrush(hBackPen);
- }
- void DrawEllipse(int PosX, int PosY, int Width, int Height)
- {
- HBRUSH hBackBrush=imgBrush;
- HBRUSH oldBrush=(HBRUSH)SelectObject(HBitmap,hBackBrush);
- HPEN hBackPen=(HPEN)imgPen;
- HPEN oldPen=(HPEN)SelectObject((HDC)HBitmap,hBackPen);
- Ellipse(HBitmap, PosX, PosY, Width + PosX, Height + PosY);
- SelectObject(HBitmap,oldBrush);
- DeleteBrush(hBackBrush);
- SelectObject(HBitmap,oldPen);
- DeleteBrush(hBackPen);
- }
- void DrawLine(int PosX, int PosY, int PosX2, int PosY2)
- {
- HBRUSH hBackBrush=imgBrush;
- HBRUSH oldBrush=(HBRUSH)SelectObject(HBitmap,hBackBrush);
- HPEN hBackPen=(HPEN)imgPen;
- HPEN oldPen=(HPEN)SelectObject((HDC)HBitmap,hBackPen);
- MoveToEx(HBitmap, PosX,PosY, NULL);
- LineTo(HBitmap, PosX2,PosY2);
- SelectObject(HBitmap,oldBrush);
- DeleteBrush(hBackBrush);
- SelectObject(HBitmap,oldPen);
- DeleteBrush(hBackPen);
- }
- void DrawTriangle(POINT vertice1, POINT vertice2, POINT vertice3)
- {
- HBRUSH hBackBrush=imgBrush;
- HBRUSH oldBrush=(HBRUSH)SelectObject(HBitmap,hBackBrush);
- HPEN hBackPen=(HPEN)imgPen;
- HPEN oldPen=(HPEN)SelectObject((HDC)HBitmap,hBackPen);
- //vertice 1 on left
- MoveToEx(HBitmap, vertice1.x,vertice1.y, NULL);
- LineTo(HBitmap, vertice2.x,vertice2.y);
- //vertice 2 on top
- MoveToEx(HBitmap, vertice2.x,vertice2.y, NULL);
- LineTo(HBitmap, vertice3.x,vertice3.y);
- //vertice 3 on bottom
- MoveToEx(HBitmap, vertice3.x,vertice3.y, NULL);
- LineTo(HBitmap, vertice1.x,vertice1.y);
- SelectObject(HBitmap,oldBrush);
- DeleteBrush(hBackBrush);
- SelectObject(HBitmap,oldPen);
- DeleteBrush(hBackPen);
- }
- void TextColor(COLORREF txtColor)
- {
- SetTextColor(HBitmap,txtColor);
- }
- void Font(CHOOSEFONT chft)
- {
- chFont=chft;
- }
- void DrawText(string strText,int PosX=0, int PosY=0)
- {
- // geting the text rectangle
- RECT r = { 0, 0, 0, 0 };
- char *text=(char*)strText.c_str();
- //draw the text
- HBRUSH hBackBrush=(HBRUSH)imgBrush;
- HBRUSH oldBrush=(HBRUSH)SelectObject(HBitmap,hBackBrush);
- HPEN hBackPen=(HPEN)imgPen;
- HPEN oldPen=(HPEN)SelectObject((HDC)HBitmap,hBackPen);
- HFONT hFont=CreateFontIndirect(chFont.lpLogFont);
- HFONT holdfont=(HFONT)SelectObject((HDC)HBitmap,hFont);
- SetTextColor(HBitmap,chFont.rgbColors);
- //change the position of the text
- ::DrawText(HBitmap, text, -1,&r,DT_LEFT | DT_EXPANDTABS | DT_CALCRECT);
- r.left+=PosX;
- r.top+=PosY;
- r.bottom+= PosX;
- r.right+= PosY;
- SetBkColor(HBitmap,GetPixel(HBitmap,0,0));
- SetBkMode(HBitmap,TRANSPARENT);
- ::DrawText(HBitmap, text, -1,&r,DT_LEFT | DT_EXPANDTABS | DT_END_ELLIPSIS | DT_NOCLIP );
- SelectObject(HBitmap,holdfont);
- DeleteBrush(hFont);
- SelectObject(HBitmap,oldBrush);
- DeleteBrush(hBackBrush);
- SelectObject(HBitmap,oldPen);
- DeleteBrush(hBackPen);
- }
- property <int> SelectFrame
- {
- Get(int)
- {
- return intSelectFrame;
- },
- Set(int selectframe)
- {
- if(intSelectFrame<0)
- intSelectFrame=0;
- else if (intSelectFrame>=framecount)
- intSelectFrame=framecount-1;
- else
- intSelectFrame=selectframe;
- UINT count = 0;
- count = img->GetFrameDimensionsCount();
- GUID* pDimensionIDs = (GUID*)malloc(sizeof(GUID)*count);
- img->GetFrameDimensionsList(pDimensionIDs, count);
- img->SelectActiveFrame(&pDimensionIDs[0],intSelectFrame);
- Gdiplus::Graphics graphics(HBitmap);
- graphics.Clear(clrBackColor);
- graphics.DrawImage(img, 0, 0, imageweight, imageheight);
- free(pDimensionIDs);
- }
- };
- void getImageFromResource(string strResourceName)
- {
- HGLOBAL hGlobal;
- LPSTREAM pStream;
- HRSRC hRsrc = FindResource(NULL, strResourceName.c_str(), RT_RCDATA);
- HGLOBAL hGlob1 = LoadResource(NULL, hRsrc);
- int size = SizeofResource(NULL, hRsrc);
- hGlobal = GlobalAlloc(GMEM_FIXED, size);
- LPVOID resPtr = LockResource(hGlob1);
- memcpy(hGlobal, resPtr, size);
- FreeResource(hGlobal);
- CreateStreamOnHGlobal(hGlobal, true,&pStream);
- //img = new Image(pStream, false);
- }
- int GetLastImageError()
- {
- return img->GetLastStatus();
- }
- property<int> FrameCount
- {
- Get(int)
- {
- return framecount;
- }
- };
- property<int> FrameDelay
- {
- Get(int)
- {
- return framedelay;
- }
- };
- property<string> FileName
- {
- Get(string)
- {
- return strfilename;
- },
- Set(string filename)
- {
- readimagefile(filename);
- }
- };
- property<COLORREF> Backcolor
- {
- Get(COLORREF)
- {
- return clrBackColor.ToCOLORREF();
- },
- Set(COLORREF bkcolor)
- {
- if(bkcolor==-1)
- {
- blnTransparent=true;
- }
- else
- {
- blnTransparent=false;
- clrBackColor.SetFromCOLORREF(bkcolor);
- SelectFrame=SelectFrame;
- }
- }
- };
- void draw(HDC control, long posX=0, long posY=0)
- {
- if (blnTransparent==true)
- {
- TransparentBlt(control, posX, posY,width(),height(),HBitmap, 0, 0,width(), height(), clrBackColor.ToCOLORREF());
- }
- else
- {
- BitBlt(control,posX,posY,width(),height(),HBitmap,0,0,SRCCOPY);
- }
- }
- operator string() const
- {
- return strfilename;
- }
- operator HICON()
- {
- return HICONFromHBITMAP(HBitmap);
- }
- operator HBITMAP() const
- {
- return HBitmap;
- }
- int height()
- {
- return imageheight;
- }
- int width()
- {
- return imageweight;
- }
- operator HDC() const
- {
- return HBitmap;
- }
- bool operator != ( nullptr_t ) const
- {
- return HBitmap != nullptr;
- }
- bool operator==(const image &other) const
- {
- return (other.HBitmap == this->HBitmap);
- }
- bool operator!=(const image &other) const
- {
- return !(*this == other);
- }
- bool haveimage()
- {
- if((HBITMAP)HBitmap==nullptr)
- return false;
- else
- return true;
- }
- ~image()
- {
- if(isimgused==true)
- delete img;
- if(isGDIPLUSIniciated==true)
- Gdiplus::GdiplusShutdown(m_gdiplusToken);
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement