GDIX.CPP
上传用户:oldpeter23
上传日期:2013-01-09
资源大小:1111k
文件大小:7k
源码类别:

IP电话/视频会议

开发平台:

Visual C++

  1. //NetTalk
  2. /*------------------------------------------------------------------------------*
  3.  =============================
  4.    模块名称: GDIX.cpp
  5.  =============================
  6.  
  7.  [版权]
  8.  
  9.    2000-2002  115软件工厂  版权所有
  10.                                               
  11. *------------------------------------------------------------------------------*/
  12. #include <Windows.h>
  13. #include "GDIX.h"
  14. /*------------------------------------------------------------------------------*/
  15. //矩形类的实现
  16. CRectX::operator RECT&() const
  17. {
  18. return (RECT&)left;
  19. }
  20. /*------------------------------------------------------------------------------*/
  21. CRectX::CRectX(int iLeft,int iTop,int iRight,int iBottom)
  22. {
  23. left=iLeft;
  24. top=iTop;
  25. right=iRight;
  26. bottom=iBottom;
  27. }
  28. /*------------------------------------------------------------------------------*/
  29. CRectX::CRectX()
  30. {
  31. left=0;
  32. top=0;
  33. right=0;
  34. bottom=0;
  35. }
  36. /*------------------------------------------------------------------------------*/
  37. CRectX::CRectX(const RECT &rc)
  38. {
  39. left=rc.left;
  40. top=rc.top;
  41. right=rc.right;
  42. bottom=rc.bottom;
  43. }
  44. /*------------------------------------------------------------------------------*/
  45. CRectX::CRectX(POINT pt,SIZE sz)
  46. {
  47. left=pt.x;
  48. top=pt.y;
  49. right=pt.x+sz.cx;
  50. bottom=pt.y+sz.cy;
  51. }
  52. /*------------------------------------------------------------------------------*/
  53. CRectX::CRectX(POINT lt,POINT rb)
  54. {
  55. left=lt.x;
  56. top=lt.y;
  57. right=rb.x;
  58. bottom=rb.y;
  59. }
  60. /*------------------------------------------------------------------------------*/
  61. CRectX::~CRectX()
  62. {
  63. }
  64. /*------------------------------------------------------------------------------*/
  65. int CRectX::Width() const
  66. {
  67. return right-left;
  68. }
  69. /*------------------------------------------------------------------------------*/
  70. int CRectX::Height() const
  71. {
  72. return bottom-top;
  73. }
  74. /*------------------------------------------------------------------------------*/
  75. SIZE CRectX::Size() const
  76. {
  77. SIZE sz;
  78. sz.cx=right-left;
  79. sz.cy=bottom-top;
  80. return sz;
  81. }
  82. /*------------------------------------------------------------------------------*/
  83. void CRectX::operator =( const RECT& rc )
  84. {
  85. left=rc.left;
  86. top=rc.top;
  87. right=rc.right;
  88. bottom=rc.bottom;
  89. }
  90. /*------------------------------------------------------------------------------*/
  91. //point类的实现
  92. const POINT& CRectX::TopLeft() const
  93. {
  94. return (POINT&)left;
  95. }
  96. /*------------------------------------------------------------------------------*/
  97. const POINT& CRectX::BottomRight() const
  98. {
  99. return (POINT&)right;
  100. }
  101. /*------------------------------------------------------------------------------*/
  102. RECT* operator &(CRectX& rc )
  103. {
  104. return (RECT*)&rc.left;
  105. }
  106. /*------------------------------------------------------------------------------*/
  107. POINT CRectX::CenterPoint()
  108. {
  109. POINT pt;
  110. pt.x=left+(right-left)/2;
  111. pt.y=top+(bottom-top)/2;
  112. return pt;
  113. }
  114. /*------------------------------------------------------------------------------*/
  115. void CRectX::Normalize()
  116. {
  117. if(left>right)
  118. {
  119. LONG temp=left;
  120. left=right;
  121. right=temp;
  122. }
  123. if(top>bottom)
  124. {
  125. LONG temp=top;
  126. top=bottom;
  127. bottom=temp;
  128. }
  129. }
  130. /*------------------------------------------------------------------------------*/
  131. //一组经常用到的GDI函数
  132. //画3D的矩形
  133. BOOL Draw3dRectX(HDC hdc, RECT &rect, COLORREF clrTopLeft, COLORREF clrBottomRight,int iPenWidth)
  134. {
  135. BOOL bRet=FALSE;
  136. HPEN hpen,hOld;
  137. hpen=CreatePen(PS_SOLID,iPenWidth,clrTopLeft);
  138. if(hpen)
  139. {
  140. hOld=(HPEN)SelectObject(hdc,hpen);
  141. MoveToEx(hdc,rect.left,rect.bottom-1,0);
  142. LineTo(hdc,rect.left,rect.top);
  143. MoveToEx(hdc,rect.left,rect.top,0);
  144. LineTo(hdc,rect.right-1,rect.top);
  145. SelectObject(hdc,hOld);
  146. DeleteObject(hpen);
  147. hpen=CreatePen(PS_SOLID,iPenWidth,clrBottomRight);
  148. if(hpen)
  149. {
  150. hOld=(HPEN)SelectObject(hdc,hpen);
  151. MoveToEx(hdc,rect.right-1,rect.top,0);
  152. LineTo(hdc,rect.right-1,rect.bottom);
  153. MoveToEx(hdc,rect.right-1,rect.bottom-1,0);
  154. LineTo(hdc,rect.left-1,rect.bottom-1);
  155. SelectObject(hdc,hOld);
  156. DeleteObject(hpen);
  157. bRet=TRUE;
  158. }
  159. }
  160. return bRet;
  161. }
  162. /*------------------------------------------------------------------------------*/
  163. //画矩形,直接给出颜色
  164. void DrawRectX(HDC hDC,const RECT& rect,COLORREF cr)
  165. {
  166. HBRUSH hb;
  167. hb=CreateSolidBrush(cr);
  168. FrameRect(hDC,&rect,hb);
  169. DeleteObject(hb);
  170. }
  171. /*------------------------------------------------------------------------------*/
  172. //填充矩形,直接给出颜色
  173. void FillSolidRectX( HDC hDC, const RECT& rect, COLORREF cr )
  174. {
  175. HBRUSH hb;
  176. hb=CreateSolidBrush(cr);
  177. FillRect(hDC,&rect,hb);
  178. DeleteObject(hb);
  179. }
  180. /*------------------------------------------------------------------------------*/
  181. //画透明位图
  182. void DrawTransBmpX(HDC hDC,RECT& rect,HBITMAP hBmp,int x,int y,COLORREF crMask)
  183. {
  184. HDC hdcBmp,hdcTrans;
  185. HBITMAP hBmpTrans;
  186. hdcBmp=CreateCompatibleDC(hDC);
  187. hdcTrans=CreateCompatibleDC(hDC);
  188.     int nWidth,nHeight;
  189. nWidth=rect.right-rect.left;
  190. nHeight=rect.bottom-rect.top;
  191. COLORREF crOldBack = SetBkColor(hDC,0x00ffffff);
  192. COLORREF crOldText = SetTextColor(hDC,0);
  193. HBITMAP hOldBmp=(HBITMAP)SelectObject(hdcBmp,hBmp);
  194. hBmpTrans=CreateBitmap(rect.right-rect.left,rect.bottom-rect.top,1, 1, NULL);
  195. HBITMAP hOldBmpTrans =(HBITMAP)SelectObject(hdcTrans,hBmpTrans);
  196. SetBkColor(hdcBmp,crMask);
  197. BitBlt(hdcTrans,0,0,nWidth,nHeight,hdcBmp,0,0,SRCCOPY);
  198. BitBlt(hDC,rect.left,rect.top, nWidth, nHeight, hdcBmp, x, y, SRCINVERT);
  199. BitBlt(hDC,rect.left,rect.top, nWidth, nHeight, hdcTrans, x, y, SRCAND);
  200. BitBlt(hDC,rect.left,rect.top, nWidth, nHeight, hdcBmp, x, y, SRCINVERT);
  201. SelectObject(hdcBmp,hOldBmp);
  202. SelectObject(hdcTrans,hOldBmpTrans);
  203. SetBkColor(hDC,crOldBack);
  204. SetTextColor(hDC,crOldText);
  205. DeleteObject(hBmpTrans);
  206. DeleteDC(hdcTrans);
  207. DeleteDC(hdcBmp);
  208. }
  209. /*------------------------------------------------------------------------------*/
  210. //Blt方式写文本
  211. void TxtBlt(HDC hdc,char* txt,CRectX& rc,COLORREF crTxt,COLORREF crBg,UINT uFormat,HFONT hf,BOOL bTransparent)
  212. {
  213. HDC hMemDC;
  214. CRectX rcNew;
  215. rcNew.left=0;
  216. rcNew.top=0;
  217. rcNew.right=rc.Width();
  218. rcNew.bottom=rc.Height();
  219. hMemDC=CreateCompatibleDC(hdc);
  220. HBITMAP hb=CreateCompatibleBitmap(hdc,rc.Width(),rc.Height());
  221. HBITMAP hob=(HBITMAP)SelectObject(hMemDC,hb);
  222. HFONT hof=(HFONT)SelectObject(hMemDC,hf);
  223. SetTextColor(hMemDC,crTxt);
  224. SetBkMode(hMemDC,TRANSPARENT);
  225. FillSolidRectX(hMemDC,rcNew,crBg);
  226. if(txt)
  227. DrawText(hMemDC,txt,strlen(txt),&rcNew,uFormat);
  228. if(bTransparent)
  229. {
  230. SelectObject(hMemDC,hob);
  231. DrawTransBmpX(hdc,rc,hb,0,0,crBg);
  232. }
  233. else
  234. {
  235. BitBlt(hdc,rc.left,rc.top,rc.Width(),rc.Height(),hMemDC,0,0,SRCCOPY);
  236. SelectObject(hMemDC,hob);
  237. }
  238. SelectObject(hMemDC,hof);
  239. DeleteObject(hb);
  240. DeleteDC(hMemDC);
  241. }