ProgressST.cpp
上传用户:pyhyhg
上传日期:2007-01-01
资源大小:191k
文件大小:6k
源码类别:

界面编程

开发平台:

Visual C++

  1. // ProgressST.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "ProgressST.h"
  5. #ifdef _DEBUG
  6. #define new DEBUG_NEW
  7. #undef THIS_FILE
  8. static char THIS_FILE[] = __FILE__;
  9. #endif
  10. /////////////////////////////////////////////////////////////////////////////
  11. // CProgressST
  12. CProgressST::CProgressST()
  13. {
  14. // Default range of the control
  15. m_nLower = 0;
  16. m_nUpper = 100;
  17. CalcRange();
  18. // Default position
  19. m_nPos = 0;
  20. // Default step
  21. m_nStep = 10;
  22. } // End of CProgressST
  23. CProgressST::~CProgressST()
  24. {
  25. } // End of ~CProgressST
  26. BEGIN_MESSAGE_MAP(CProgressST, CProgressCtrl)
  27. //{{AFX_MSG_MAP(CProgressST)
  28. ON_WM_PAINT()
  29. //}}AFX_MSG_MAP
  30. END_MESSAGE_MAP()
  31. /////////////////////////////////////////////////////////////////////////////
  32. // CProgressST message handlers
  33. void CProgressST::OnPaint() 
  34. {
  35. PAINTSTRUCT lpPaint;
  36. // If there is NO bitmap loaded
  37. if (m_bmPattern.m_hObject == NULL) 
  38. {
  39. CProgressCtrl::OnPaint();
  40. return;
  41. }
  42. BeginPaint(&lpPaint);
  43. CWindowDC dc(this);
  44. DrawProgress(&dc);
  45. EndPaint(&lpPaint);
  46. } // End of OnPaint
  47. BOOL CProgressST::SetBitmap(UINT nBitmapId, BOOL bRepaint)
  48. {
  49. BITMAP bm;
  50. BOOL bRet;
  51. // Detach any previuos bitmap
  52. m_bmPattern.Detach();
  53. // Default return value
  54. bRet = TRUE;
  55. // Load new bitmap
  56. if (nBitmapId != NULL)
  57. {
  58. bRet = GetBitmapAndPalette(nBitmapId, m_bmPattern, m_Palette);
  59. // If all ok
  60. if (bRet == TRUE)
  61. {
  62. // Get dimension
  63. m_bmPattern.GetBitmap(&bm);
  64. // Width of the bitmap
  65. m_nWidth = bm.bmWidth;
  66. // Height of the bitmap
  67. m_nHeight = bm.bmHeight;
  68. }
  69. }
  70. // Repaint control
  71. if (bRepaint == TRUE) Invalidate();
  72. return bRet;
  73. } // End of SetBitmap
  74. void CProgressST::DrawProgress(CDC * pDC)
  75. {
  76. CRect ctrlRect;
  77.     CDC memDC;
  78. memDC.CreateCompatibleDC(pDC);
  79. // Select bitmap
  80. CBitmap* pOldBitmap = memDC.SelectObject(&m_bmPattern);
  81. GetClientRect(ctrlRect);
  82. // Create temporary DC & bitmap
  83. CDC tempDC;
  84. tempDC.CreateCompatibleDC(pDC);
  85. CBitmap bitmapTemp;
  86. bitmapTemp.CreateCompatibleBitmap(&memDC, ctrlRect.Width(), ctrlRect.Height());
  87. CBitmap* pOldTempBitmap = tempDC.SelectObject(&bitmapTemp);
  88. // Calculate control's percentage to draw
  89. int nPercentage;
  90. nPercentage = (int)((100.0/m_nRange)*(abs(m_nLower)+m_nPos));
  91. // Adjust rectangle to draw on screen
  92. float f = ((float)(ctrlRect.Width())/100)*nPercentage;
  93. if ((ctrlRect.left + (int)f) > ctrlRect.right)
  94. {
  95. ctrlRect.right -= 1;
  96. }
  97. else
  98. {
  99. ctrlRect.right = ctrlRect.left + (int)f;
  100. }
  101. // Leave space for border
  102. ctrlRect.DeflateRect(1, 1);
  103. // Tile the bitmap into the temporary rectangle
  104. TileBitmap(&tempDC, &memDC, ctrlRect);
  105. // Select and realize the palette
  106. if(pDC->GetDeviceCaps(RASTERCAPS) & RC_PALETTE && m_Palette.m_hObject != NULL)
  107. {
  108. pDC->SelectPalette(&m_Palette, FALSE);
  109. pDC->RealizePalette();
  110. }
  111. // Copy from temporary DC to screen (only the percentage rectangle)
  112. if (ctrlRect.IsRectEmpty() == FALSE)
  113. pDC->BitBlt(2, 2, ctrlRect.Width(), ctrlRect.Height(), &tempDC, 0, 0, SRCCOPY);
  114. /* RFU
  115. CRect R;
  116. GetClientRect(R);
  117. pDC->SetBkMode(TRANSPARENT);
  118. pDC->DrawText("Title", -1, R, DT_SINGLELINE | DT_VCENTER | DT_CENTER);
  119. */
  120. // Restore old selected bitmaps
  121. tempDC.SelectObject(pOldTempBitmap);
  122. memDC.SelectObject(pOldBitmap);
  123. } // End of DrawProgress
  124. void CProgressST::TileBitmap(CDC * pDestDC, CDC* pSrcDC, CRect rect)
  125. {
  126. int nHLoop;
  127. int nVLoop;
  128. int nHTiles;
  129. int nVTiles;
  130. // Calc number of horizontal tiles
  131. nHTiles = (rect.Width() / m_nWidth);
  132. if (rect.Width() % m_nWidth != 0) nHTiles++;
  133. // Calc number of vertical tiles
  134. nVTiles = (rect.Height() / m_nHeight);
  135. if (rect.Height() % m_nHeight != 0) nVTiles++;
  136. // Tile bitmap horizontally
  137. for (nHLoop = 0; nHLoop < nHTiles; nHLoop++)
  138. {
  139. // Tile bitmap vertically
  140. for (nVLoop = 0; nVLoop < nVTiles; nVLoop++)
  141. {
  142. pDestDC->BitBlt((nHLoop*m_nWidth), (nVLoop*m_nHeight), m_nWidth, m_nHeight, pSrcDC, 0, 0, SRCCOPY);
  143. }
  144. }
  145. } // End of TileBitmap
  146. void CProgressST::CalcRange()
  147. {
  148. m_nRange = abs(m_nUpper - m_nLower);
  149. // Avoid divide by zero
  150. if (m_nRange == 0) m_nRange = 1;
  151. } // End of CalcRange
  152. void CProgressST::SetRange(int nLower, int nUpper)
  153. {
  154. m_nLower = nLower;
  155. m_nUpper = nUpper;
  156. CalcRange();
  157. CProgressCtrl::SetRange(nLower, nUpper);
  158. } // End of SetRange
  159. int CProgressST::SetStep(int nStep)
  160. {
  161. m_nStep = nStep;
  162. return CProgressCtrl::SetStep(nStep);
  163. } // End Of SetStep
  164. int CProgressST::SetPos(int nPos)
  165. {
  166. // Bound checking
  167. if (nPos < m_nLower) nPos = m_nLower;
  168. if (nPos > m_nUpper) nPos = m_nUpper;
  169. m_nPos = nPos;
  170. return CProgressCtrl::SetPos(nPos);
  171. } // End of SetPos
  172. int CProgressST::StepIt()
  173. {
  174. m_nPos += m_nStep;
  175. // Bound checking
  176. if (m_nPos > m_nUpper) m_nPos = m_nLower + (m_nPos - m_nUpper);
  177. return CProgressCtrl::StepIt();
  178. } // End of StepIt
  179. BOOL CProgressST::GetBitmapAndPalette(UINT nIDResource, CBitmap& bitmap, CPalette& pal)
  180. {
  181. LPCTSTR lpszResourceName = (LPCTSTR)nIDResource;
  182. HBITMAP hBmp = (HBITMAP)::LoadImage( AfxGetInstanceHandle(), 
  183. lpszResourceName, IMAGE_BITMAP, 0,0, LR_CREATEDIBSECTION);
  184. if (hBmp == NULL) return FALSE;
  185. bitmap.Attach(hBmp);
  186. // Create a logical palette for the bitmap
  187. DIBSECTION ds;
  188. BITMAPINFOHEADER &bmInfo = ds.dsBmih;
  189. bitmap.GetObject(sizeof(ds), &ds);
  190. int nColors = bmInfo.biClrUsed ? bmInfo.biClrUsed : 1 << bmInfo.biBitCount;
  191. // Create a halftone palette if colors > 256. 
  192. CClientDC dc(NULL); // Desktop DC
  193. if(nColors > 256)
  194. pal.CreateHalftonePalette(&dc);
  195. else
  196. {
  197. // Create the palette
  198. RGBQUAD *pRGB = new RGBQUAD[nColors];
  199. CDC memDC;
  200. memDC.CreateCompatibleDC(&dc);
  201. memDC.SelectObject( &bitmap );
  202. ::GetDIBColorTable( memDC, 0, nColors, pRGB );
  203. UINT nSize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * nColors);
  204. LOGPALETTE *pLP = (LOGPALETTE *) new BYTE[nSize];
  205. pLP->palVersion = 0x300;
  206. pLP->palNumEntries = nColors;
  207. for (int i=0; i < nColors; i++)
  208. {
  209. pLP->palPalEntry[i].peRed = pRGB[i].rgbRed;
  210. pLP->palPalEntry[i].peGreen = pRGB[i].rgbGreen;
  211. pLP->palPalEntry[i].peBlue = pRGB[i].rgbBlue;
  212. pLP->palPalEntry[i].peFlags = 0;
  213. }
  214. pal.CreatePalette( pLP );
  215. delete[] pLP;
  216. delete[] pRGB;
  217. }
  218. return TRUE;
  219. } // End of GetBitmapAndPalette
  220. const char* CProgressST::GetVersionC()
  221. {
  222.   return "1.0";
  223. } // End of GetVersionC
  224. const int CProgressST::GetVersionI()
  225. {
  226.   return 10; // Divide by 10 to get actual version
  227. } // End of GetVersionI