Advertisement
Guest User

Untitled

a guest
Aug 24th, 2015
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.78 KB | None | 0 0
  1. class MemoryDC
  2. {
  3. private:
  4. HDC memoryDC;
  5.  
  6. public:
  7. MemoryDC ()
  8. {
  9. HDC hdc=GetDC(GetDesktopWindow());
  10. memoryDC=CreateCompatibleDC(hdc);
  11. ReleaseDC(GetDesktopWindow(),hdc);
  12. }
  13.  
  14. operator HDC() const
  15. {
  16. return memoryDC;
  17. }
  18.  
  19. ~MemoryDC ()
  20. {
  21. DeleteDC(memoryDC);
  22. }
  23. };
  24.  
  25. class BitmapDC
  26. {
  27. private:
  28. MemoryDC hdcbitmap;
  29. HGDIOBJ bitmapold;
  30. HBITMAP bitmapcurrent;
  31. int intwidth;
  32. int intheight;
  33.  
  34. void init(int width, int height)
  35. {
  36. bitmapcurrent = CreateBitmap(width, height, 1, 32, NULL);
  37. bitmapold = SelectObject(hdcbitmap, bitmapcurrent);
  38. intwidth=width;
  39. intheight=height;
  40. }
  41.  
  42. void destroy()
  43. {
  44. SelectObject(hdcbitmap, bitmapold);
  45. DeleteObject(bitmapcurrent);
  46. }
  47.  
  48. public:
  49.  
  50. BitmapDC(int width, int height)
  51. {
  52. init(width, height);
  53. }
  54.  
  55. BitmapDC()
  56. {
  57. init(1, 1);
  58. }
  59.  
  60. void size(int width, int height)
  61. {
  62. destroy();
  63. init(width, height);
  64. }
  65.  
  66. int Width()
  67. {
  68. return intwidth;
  69. }
  70.  
  71. int Height()
  72. {
  73. return intheight;
  74. }
  75.  
  76. BitmapDC& operator= (const BitmapDC &bitmapsource)
  77. {
  78. if (this == &bitmapsource) // Same object?
  79. return *this;
  80. destroy();
  81. init(bitmapsource.intwidth, bitmapsource.intheight);
  82. BitBlt(bitmapsource, 0, 0, intwidth, intheight, bitmapsource, 0, 0, SRCCOPY);
  83. return *this;
  84. }
  85.  
  86. BitmapDC& operator= (const HBITMAP &bitmapsource)
  87. {
  88. destroy();
  89. BITMAP bm;
  90. GetObject(bitmapsource,sizeof(bm),&bm);
  91. init(bm.bmWidth, bm.bmHeight);
  92. DrawHBITMAPtoHDC(bitmapsource,hdcbitmap);
  93. return *this;
  94. }
  95.  
  96. //testing the bitmao value if is nullptr
  97. bool operator != ( nullptr_t ) const
  98. {
  99. return bitmapcurrent != nullptr;
  100. }
  101.  
  102. bool operator==(const BitmapDC &other) const
  103. {
  104. return (other.bitmapcurrent == this->bitmapcurrent);
  105. }
  106.  
  107. bool operator!=(const BitmapDC &other) const
  108. {
  109. return !(*this == other);
  110. }
  111.  
  112. operator HBITMAP() const
  113. {
  114. return bitmapcurrent;
  115. }
  116.  
  117. operator HDC() const
  118. {
  119. return hdcbitmap;
  120. }
  121.  
  122. ~BitmapDC()
  123. {
  124. destroy();
  125. }
  126. };
  127.  
  128. class image
  129. {
  130. private:
  131. ULONG_PTR m_gdiplusToken;
  132. Gdiplus::GdiplusStartupInput gdiplusStartupInput;
  133. BitmapDC HBitmap;
  134. Image *img=NULL;
  135. bool isimgused=false;
  136. bool isGDIPLUSIniciated=false;
  137. int imageheight=0;
  138. int imageweight=0;
  139. int framecount=0;
  140. int intSelectFrame=0;
  141. int framedelay=0;
  142. string strfilename="";
  143. Gdiplus::Color clrBackColor=Gdiplus::Color::Blue;
  144. bool blnTransparent=true;
  145. HPEN imgPen;
  146. HBRUSH imgBrush;
  147. CHOOSEFONT chFont;
  148.  
  149. void readimagefile(string filename)
  150. {
  151. if (isimgused==true)
  152. delete img;
  153. strfilename=filename;
  154. if(toupper(filename[filename.size()-3])=='C' && toupper(filename[filename.size()-2])=='U' && toupper(filename[filename.size()-1])=='R')
  155. {
  156. isimgused=false;
  157. //create the transparent icon handle
  158. HCURSOR hicon = (HCURSOR)LoadImage(NULL, filename.c_str(), IMAGE_CURSOR, imageweight, imageheight, LR_LOADFROMFILE|LR_SHARED|LR_DEFAULTSIZE|LR_LOADTRANSPARENT);
  159. ICONINFO ii;
  160. BOOL fResult = GetIconInfo(hicon, &ii);
  161. if (fResult)
  162. {
  163. BITMAP bm;
  164. fResult = GetObject(ii.hbmMask, sizeof(bm), &bm) == sizeof(bm);
  165. if (fResult)
  166. {
  167. imageweight= bm.bmWidth;
  168. imageheight= ii.hbmColor ? bm.bmHeight : bm.bmHeight / 2;
  169. }
  170. if (ii.hbmMask)
  171. DeleteObject(ii.hbmMask);
  172. if (ii.hbmColor)
  173. DeleteObject(ii.hbmColor);
  174. }
  175. HBitmap.size(imageweight,imageheight);
  176. DrawIconEx(HBitmap,0,0,hicon,imageweight,imageheight,0,0,DI_NORMAL);
  177. framecount=1;
  178. DestroyCursor(hicon);
  179. }
  180. else
  181. {
  182. isimgused=true;
  183. Gdiplus::Image img2(towstring(filename).c_str());
  184. imageweight=img2.GetWidth();
  185. imageheight=img2.GetHeight();
  186. HBitmap.size(imageweight,imageheight);
  187. Gdiplus::Graphics graphics(HBitmap);
  188. graphics.DrawImage(&img2, 0,0,imageweight,imageheight);
  189.  
  190. UINT count = 0;
  191. count = img2.GetFrameDimensionsCount();
  192. GUID* pDimensionIDs = (GUID*)malloc(sizeof(GUID)*count);
  193. img2.GetFrameDimensionsList(pDimensionIDs, count);
  194. framecount=img2.GetFrameCount(&pDimensionIDs[0]);
  195. framedelay =img2.GetPropertyItemSize(PropertyTagFrameDelay);
  196. img=new Image(towstring(filename).c_str());
  197. isimgused=true;
  198. free(pDimensionIDs);
  199. }
  200. }
  201.  
  202. HICON HICONFromHBITMAP(HBITMAP bitmap)
  203. {
  204. BITMAP bmp;
  205. GetObject(bitmap, sizeof(BITMAP), &bmp);
  206. HBITMAP hbmMask = CreateCompatibleBitmap(HBitmap, bmp.bmWidth, bmp.bmHeight);
  207.  
  208. ICONINFO ii = {0};
  209. ii.fIcon = TRUE;
  210. ii.hbmColor = bitmap;
  211. ii.hbmMask = hbmMask;
  212. HICON hIcon = CreateIconIndirect(&ii);
  213. DeleteObject(ii.hbmColor);
  214. DeleteObject(ii.hbmMask);
  215. return hIcon;
  216. }
  217.  
  218. public:
  219.  
  220. void resize(const int width, const int height)
  221. {
  222. imageheight=height;
  223. imageweight=width;
  224. HBitmap.size(imageweight,imageheight);
  225. }
  226.  
  227. image()
  228. {
  229. if (isimgused==true)
  230. delete img;
  231. isimgused=false;
  232. if(isGDIPLUSIniciated==false)
  233. {
  234. Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
  235. isGDIPLUSIniciated=true;
  236. }
  237. imageheight=1;
  238. imageweight=1;
  239. HBitmap.size(imageweight,imageheight);
  240. }
  241.  
  242. image(const int width, const int height)
  243. {
  244.  
  245. if (isimgused==true)
  246. delete img;
  247. isimgused=false;
  248. if(isGDIPLUSIniciated==false)
  249. {
  250. Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
  251. isGDIPLUSIniciated=true;
  252. }
  253. framecount=1;
  254. imageheight=height;
  255. imageweight=width;
  256. HBitmap.size(imageweight,imageheight);
  257. }
  258.  
  259. image( const string & filename)
  260. {
  261. if(isGDIPLUSIniciated==false)
  262. {
  263. Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
  264. isGDIPLUSIniciated=true;
  265. }
  266. readimagefile(filename);
  267. }
  268.  
  269. image (const image &cSource)
  270. {
  271. if(isGDIPLUSIniciated==false)
  272. {
  273. Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
  274. isGDIPLUSIniciated=true;
  275. }
  276. readimagefile(cSource.strfilename);
  277. BitBlt(HBitmap,0,0,imageweight,imageheight,cSource.HBitmap,0,0,SRCCOPY);
  278. }
  279.  
  280. image& operator= (const image &cSource)
  281. {
  282. if (this == &cSource) // Same object?
  283. return *this;
  284. if(isGDIPLUSIniciated==false)
  285. {
  286. Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
  287. isGDIPLUSIniciated=true;
  288. }
  289. readimagefile(cSource.strfilename);
  290. BitBlt(HBitmap,0,0,imageweight,imageheight,cSource.HBitmap,0,0,SRCCOPY);
  291. return *this;
  292. }
  293.  
  294.  
  295.  
  296. void Pen(int PenStyle, int PenSize, COLORREF PenColor)
  297. {
  298. imgPen=CreatePen(PenStyle,PenSize,PenColor);
  299. }
  300.  
  301. void Pen(HPEN hPen)
  302. {
  303. imgPen=hPen;
  304. }
  305.  
  306. void Brush(HBRUSH BrushColor)
  307. {
  308. imgBrush = BrushColor;
  309. }
  310.  
  311. void Brush(COLORREF BrushColor)
  312. {
  313. imgBrush=CreateSolidBrush(BrushColor);
  314. }
  315.  
  316. void Brush(HBITMAP BrushImage)
  317. {
  318. imgBrush=CreatePatternBrush(BrushImage);
  319. }
  320.  
  321. void Brush(int HatchedStyle, COLORREF HatchedColor )
  322. {
  323. imgBrush=CreateHatchBrush(HatchedStyle, HatchedColor);
  324. }
  325.  
  326. void DrawRectangle(int PosX, int PosY, int Width, int Height, int EllipseWidth=0, int EllipseHeight=0)
  327. {
  328. HBRUSH hBackBrush=imgBrush;
  329. HBRUSH oldBrush=(HBRUSH)SelectObject(HBitmap,hBackBrush);
  330. HPEN hBackPen=(HPEN)imgPen;
  331. HPEN oldPen=(HPEN)SelectObject((HDC)HBitmap,hBackPen);
  332.  
  333. RoundRect(HBitmap, PosX, PosY, Width + PosX, Height + PosY, EllipseWidth, EllipseHeight);
  334.  
  335. SelectObject(HBitmap,oldBrush);
  336. DeleteBrush(hBackBrush);
  337. SelectObject(HBitmap,oldPen);
  338. DeleteBrush(hBackPen);
  339. }
  340.  
  341. void DrawEllipse(int PosX, int PosY, int Width, int Height)
  342. {
  343. HBRUSH hBackBrush=imgBrush;
  344. HBRUSH oldBrush=(HBRUSH)SelectObject(HBitmap,hBackBrush);
  345. HPEN hBackPen=(HPEN)imgPen;
  346. HPEN oldPen=(HPEN)SelectObject((HDC)HBitmap,hBackPen);
  347.  
  348. Ellipse(HBitmap, PosX, PosY, Width + PosX, Height + PosY);
  349.  
  350. SelectObject(HBitmap,oldBrush);
  351. DeleteBrush(hBackBrush);
  352. SelectObject(HBitmap,oldPen);
  353. DeleteBrush(hBackPen);
  354. }
  355.  
  356. void DrawLine(int PosX, int PosY, int PosX2, int PosY2)
  357. {
  358. HBRUSH hBackBrush=imgBrush;
  359. HBRUSH oldBrush=(HBRUSH)SelectObject(HBitmap,hBackBrush);
  360. HPEN hBackPen=(HPEN)imgPen;
  361. HPEN oldPen=(HPEN)SelectObject((HDC)HBitmap,hBackPen);
  362.  
  363. MoveToEx(HBitmap, PosX,PosY, NULL);
  364. LineTo(HBitmap, PosX2,PosY2);
  365.  
  366. SelectObject(HBitmap,oldBrush);
  367. DeleteBrush(hBackBrush);
  368. SelectObject(HBitmap,oldPen);
  369. DeleteBrush(hBackPen);
  370. }
  371.  
  372. void DrawTriangle(POINT vertice1, POINT vertice2, POINT vertice3)
  373. {
  374.  
  375. HBRUSH hBackBrush=imgBrush;
  376. HBRUSH oldBrush=(HBRUSH)SelectObject(HBitmap,hBackBrush);
  377. HPEN hBackPen=(HPEN)imgPen;
  378. HPEN oldPen=(HPEN)SelectObject((HDC)HBitmap,hBackPen);
  379.  
  380. //vertice 1 on left
  381. MoveToEx(HBitmap, vertice1.x,vertice1.y, NULL);
  382. LineTo(HBitmap, vertice2.x,vertice2.y);
  383.  
  384. //vertice 2 on top
  385. MoveToEx(HBitmap, vertice2.x,vertice2.y, NULL);
  386. LineTo(HBitmap, vertice3.x,vertice3.y);
  387.  
  388. //vertice 3 on bottom
  389. MoveToEx(HBitmap, vertice3.x,vertice3.y, NULL);
  390. LineTo(HBitmap, vertice1.x,vertice1.y);
  391.  
  392. SelectObject(HBitmap,oldBrush);
  393. DeleteBrush(hBackBrush);
  394. SelectObject(HBitmap,oldPen);
  395. DeleteBrush(hBackPen);
  396. }
  397.  
  398. void TextColor(COLORREF txtColor)
  399. {
  400. SetTextColor(HBitmap,txtColor);
  401. }
  402.  
  403. void Font(CHOOSEFONT chft)
  404. {
  405. chFont=chft;
  406. }
  407.  
  408. void DrawText(string strText,int PosX=0, int PosY=0)
  409. {
  410. // geting the text rectangle
  411. RECT r = { 0, 0, 0, 0 };
  412. char *text=(char*)strText.c_str();
  413.  
  414.  
  415. //draw the text
  416.  
  417. HBRUSH hBackBrush=(HBRUSH)imgBrush;
  418. HBRUSH oldBrush=(HBRUSH)SelectObject(HBitmap,hBackBrush);
  419. HPEN hBackPen=(HPEN)imgPen;
  420. HPEN oldPen=(HPEN)SelectObject((HDC)HBitmap,hBackPen);
  421.  
  422. HFONT hFont=CreateFontIndirect(chFont.lpLogFont);
  423. HFONT holdfont=(HFONT)SelectObject((HDC)HBitmap,hFont);
  424. SetTextColor(HBitmap,chFont.rgbColors);
  425.  
  426. //change the position of the text
  427.  
  428. ::DrawText(HBitmap, text, -1,&r,DT_LEFT | DT_EXPANDTABS | DT_CALCRECT);
  429. r.left+=PosX;
  430. r.top+=PosY;
  431. r.bottom+= PosX;
  432. r.right+= PosY;
  433. SetBkColor(HBitmap,GetPixel(HBitmap,0,0));
  434. SetBkMode(HBitmap,TRANSPARENT);
  435. ::DrawText(HBitmap, text, -1,&r,DT_LEFT | DT_EXPANDTABS | DT_END_ELLIPSIS | DT_NOCLIP );
  436.  
  437. SelectObject(HBitmap,holdfont);
  438. DeleteBrush(hFont);
  439.  
  440. SelectObject(HBitmap,oldBrush);
  441. DeleteBrush(hBackBrush);
  442.  
  443. SelectObject(HBitmap,oldPen);
  444. DeleteBrush(hBackPen);
  445. }
  446.  
  447. property <int> SelectFrame
  448. {
  449. Get(int)
  450. {
  451. return intSelectFrame;
  452. },
  453. Set(int selectframe)
  454. {
  455. if(intSelectFrame<0)
  456. intSelectFrame=0;
  457. else if (intSelectFrame>=framecount)
  458. intSelectFrame=framecount-1;
  459. else
  460. intSelectFrame=selectframe;
  461.  
  462. UINT count = 0;
  463. count = img->GetFrameDimensionsCount();
  464.  
  465. GUID* pDimensionIDs = (GUID*)malloc(sizeof(GUID)*count);
  466. img->GetFrameDimensionsList(pDimensionIDs, count);
  467. img->SelectActiveFrame(&pDimensionIDs[0],intSelectFrame);
  468. Gdiplus::Graphics graphics(HBitmap);
  469. graphics.Clear(clrBackColor);
  470. graphics.DrawImage(img, 0, 0, imageweight, imageheight);
  471. free(pDimensionIDs);
  472. }
  473. };
  474.  
  475. void getImageFromResource(string strResourceName)
  476. {
  477. HGLOBAL hGlobal;
  478. LPSTREAM pStream;
  479.  
  480. HRSRC hRsrc = FindResource(NULL, strResourceName.c_str(), RT_RCDATA);
  481. HGLOBAL hGlob1 = LoadResource(NULL, hRsrc);
  482. int size = SizeofResource(NULL, hRsrc);
  483. hGlobal = GlobalAlloc(GMEM_FIXED, size);
  484. LPVOID resPtr = LockResource(hGlob1);
  485. memcpy(hGlobal, resPtr, size);
  486. FreeResource(hGlobal);
  487.  
  488. CreateStreamOnHGlobal(hGlobal, true,&pStream);
  489. //img = new Image(pStream, false);
  490. }
  491.  
  492. int GetLastImageError()
  493. {
  494. return img->GetLastStatus();
  495. }
  496.  
  497. property<int> FrameCount
  498. {
  499. Get(int)
  500. {
  501. return framecount;
  502. }
  503. };
  504.  
  505. property<int> FrameDelay
  506. {
  507. Get(int)
  508. {
  509. return framedelay;
  510. }
  511. };
  512.  
  513. property<string> FileName
  514. {
  515. Get(string)
  516. {
  517. return strfilename;
  518. },
  519. Set(string filename)
  520. {
  521. readimagefile(filename);
  522. }
  523. };
  524.  
  525. property<COLORREF> Backcolor
  526. {
  527. Get(COLORREF)
  528. {
  529. return clrBackColor.ToCOLORREF();
  530. },
  531. Set(COLORREF bkcolor)
  532. {
  533. if(bkcolor==-1)
  534. {
  535. blnTransparent=true;
  536. }
  537. else
  538. {
  539. blnTransparent=false;
  540. clrBackColor.SetFromCOLORREF(bkcolor);
  541. SelectFrame=SelectFrame;
  542. }
  543. }
  544. };
  545.  
  546. void draw(HDC control, long posX=0, long posY=0)
  547. {
  548. if (blnTransparent==true)
  549. {
  550. TransparentBlt(control, posX, posY,width(),height(),HBitmap, 0, 0,width(), height(), clrBackColor.ToCOLORREF());
  551. }
  552. else
  553. {
  554. BitBlt(control,posX,posY,width(),height(),HBitmap,0,0,SRCCOPY);
  555. }
  556. }
  557.  
  558. operator string() const
  559. {
  560. return strfilename;
  561. }
  562.  
  563. operator HICON()
  564. {
  565. return HICONFromHBITMAP(HBitmap);
  566. }
  567.  
  568. operator HBITMAP() const
  569. {
  570. return HBitmap;
  571. }
  572.  
  573. int height()
  574. {
  575. return imageheight;
  576. }
  577.  
  578. int width()
  579. {
  580. return imageweight;
  581. }
  582.  
  583. operator HDC() const
  584. {
  585. return HBitmap;
  586. }
  587.  
  588. bool operator != ( nullptr_t ) const
  589. {
  590. return HBitmap != nullptr;
  591. }
  592.  
  593. bool operator==(const image &other) const
  594. {
  595. return (other.HBitmap == this->HBitmap);
  596. }
  597.  
  598. bool operator!=(const image &other) const
  599. {
  600. return !(*this == other);
  601. }
  602.  
  603. bool haveimage()
  604. {
  605. if((HBITMAP)HBitmap==nullptr)
  606. return false;
  607. else
  608. return true;
  609. }
  610.  
  611. ~image()
  612. {
  613. if(isimgused==true)
  614. delete img;
  615. if(isGDIPLUSIniciated==true)
  616. Gdiplus::GdiplusShutdown(m_gdiplusToken);
  617. }
  618. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement