fork(4) download
  1. /* Make mem DC + mem bitmap */
  2. HDC hdcScreen = ::GetDC(NULL);
  3. HDC hDC = CreateCompatibleDC(hdcScreen);
  4. // assume that m_nOriginalWidth & m_nOriginalHeight has been defined
  5. HBITMAP hBmp = CreateCompatibleBitmap(hdcScreen, m_nOriginalWidth, m_nOriginalHeight);
  6. HBITMAP hBmpOld = (HBITMAP)SelectObject(hDC, hBmp);
  7.  
  8. /* Draw background image */
  9. if (m_pImgBackground)
  10. m_pImgBackground->Draw(hDC, 0, 0);
  11.  
  12. /* Draw text */
  13. CRect textRect(68, 10, 638, 50);
  14. CImage *pTextImage = GreateTextImage(m_strWarnMessage, 14, L"Segoe UI", RGB(255, 255, 255), textRect, DT_LEFT | DT_NOFULLWIDTHCHARBREAK | DT_VCENTER | DT_WORDBREAK | DT_WORD_ELLIPSIS);
  15. if (pTextImage)
  16. {
  17. pTextImage->Save(L"test.bmp"); // this file looks fine
  18. pTextImage->TransparentBlt(hDC, textRect, RGB(0, 0, 0)); // when I draw this on DC, it looks not smooth
  19. }
  20. SAFEDELETE(pTextImage);
  21.  
  22. /* Call UpdateLayeredWindow */
  23. BLENDFUNCTION blend = {0};
  24. blend.BlendOp = AC_SRC_OVER;
  25. blend.SourceConstantAlpha = m_nAlpha;
  26. blend.AlphaFormat = AC_SRC_ALPHA;
  27. POINT ptPos = {nDrawPositionX, nDrawPositionY};
  28. SIZE sizeWnd = {nDrawWidth, nDrawHeight};
  29. POINT ptSrc = {nDrawPositionX - nCurrentPositionX, nDrawPositionY - nCurrentPositionY};
  30. ::UpdateLayeredWindow(hwnd, hdcScreen, &ptPos, &sizeWnd, hDC, &ptSrc, 0, &blend, ULW_ALPHA);
  31. // Assume the above code run well since I ignore some parameters
  32.  
  33. SelectObject(hDC, hBmpOld);
  34. DeleteObject(hBmp);
  35. DeleteDC(hDC);
  36. ::ReleaseDC(NULL, hdcScreen);
  37.  
  38.  
  39. CImage* CDlgWarn::GreateTextImage(CString strText, int nFontSize, LPCWSTR pszFaceName, COLORREF crColor, const RECT &rect, DWORD dwFlags)
  40. {
  41. int nWidth = rect.right - rect.left;
  42. int nHeight = rect.bottom - rect.top;
  43. if (nWidth <= 0 || nHeight <= 0 || nFontSize <= 0)
  44. return NULL;
  45.  
  46. if (pszFaceName == NULL)
  47. return NULL;
  48.  
  49. /* Create Image */
  50. CImage* pImage = new CImage();
  51. if (!pImage)
  52. return NULL;
  53.  
  54. if (!pImage->Create(nWidth, nHeight, 32))
  55. {
  56. SAFEDELETE(pImage);
  57. return NULL;
  58. }
  59. HDC hDC = pImage->GetDC();
  60. CDC *pDC = CDC::FromHandle(hDC);
  61. pDC->SetBkMode(TRANSPARENT);
  62. //pDC->SetBkColor(RGB(0, 0, 0));
  63. pDC->SetTextColor(crColor);
  64. /* Create font */
  65. CFont font;
  66. LOGFONT lf;
  67. memset(&lf, 0, sizeof(LOGFONT));
  68. lf.lfHeight = -MulDiv(nFontSize, GetDeviceCaps(hDC, LOGPIXELSY), 72);
  69. wcscpy(lf.lfFaceName, pszFaceName);
  70. VERIFY(font.CreateFontIndirect(&lf));
  71. CFont* def_font = pDC->SelectObject(&font);
  72.  
  73. /* Calculate neccessary boundings */
  74. RECT drawRect = {0, 0, nWidth, nHeight};
  75. RECT tmpRect = drawRect;
  76. dwFlags &= ~DT_CALCRECT;
  77. if (!pDC->DrawText(strText, -1, &tmpRect, dwFlags | DT_CALCRECT))
  78. {
  79. font.DeleteObject();
  80. pImage->ReleaseDC();
  81. SAFEDELETE(pImage);
  82. return NULL;
  83. }
  84. drawRect.top += (nHeight - (tmpRect.bottom - tmpRect.top)) / 2;
  85. if (!pDC->DrawText(strText, -1, &drawRect, dwFlags))
  86. {
  87. font.DeleteObject();
  88. pImage->ReleaseDC();
  89. SAFEDELETE(pImage);
  90. return NULL;
  91. }
  92. pDC->SelectObject(def_font);
  93. // Done with the font. Delete the font object.
  94. font.DeleteObject();
  95. pImage->ReleaseDC();
  96.  
  97. return pImage;
  98. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty