0

I am creating a bitmap in the memory which combine with an image and text. My code is:

HDC hdcWindow = GetDC();
HDC hdcMemDC = CreateCompatibleDC(hdcWindow);
HBITMAP hbmDrag = NULL;

if (!hdcMemDC) {
   ReleaseDC(hdcWindow);
   return NULL;
}

RECT clientRect = {0};
GetClientRect(&clientRect);
hbmDrag = CreateCompatibleBitmap(hdcWindow, 256, 256);
if(hbmDrag) {
   SelectObject(hdcMemDC, hbmDrag);
   FillRect(hdcMemDC, &clientRect, mSelectedBkgndBrush);
   Graphics graphics(hdcMemDC);
   // Draw the icon
   graphics.DrawImage(mImage, 100, 100, 50, 50);
#if 1
   CRect desktopLabelRect(0, y, clientRect.right, y);
   HFONT desktopFont = mNameLabel.GetFont();
   HGDIOBJ oldFont = SelectObject(hdcMemDC, desktopFont);
   SetTextColor(hdcMemDC, RGB(255,0,0));
   DrawText(hdcMemDC, mName, -1, desktopLabelRect, DT_CENTER | DT_END_ELLIPSIS | DT_CALCRECT);
#else
   // Set font
   Font font(hdcMemDC, mNameLabel.GetFont());
   // Set RECT
   int y = DEFAULT_ICON_HEIGHT + mMargin;
   RectF layoutRect(0, y, clientRect.right, y);
   // Set display format
   StringFormat format;
   format.SetAlignment(StringAlignmentCenter);
   // Set brush
   SolidBrush blackBrush(Color(255, 0, 0, 0));
   // Draw the label
   int labelWide = DEFAULT_ICON_WIDTH + mMargin;
   CString labelName = GetLayOutLabelName(hdcMemDC, labelWide, mName);
   graphics.DrawString(labelName, -1, &font, layoutRect, &format, &blackBrush);
#endif
}

DeleteDC(hdcMemDC);
ReleaseDC(hdcWindow);

return hbmDrag;

The image can be outputted to the bitmap success. For the text, if I use "DrawText", it can't be shown in the bitmap although the return value is correct; But Graphics::DrawString can output the text success.

I don't know the reason. Anybody can pls tell me? Thanks a lot.

Shevliaskovic
  • 1,562
  • 4
  • 26
  • 43
yw5643
  • 189
  • 1
  • 12

1 Answers1

1

You are passing the DT_CALCRECT flag to DrawText(). This flag is documented as (emphasis mine):

Determines the width and height of the rectangle. If there are multiple lines of text, DrawText uses the width of the rectangle pointed to by the lpRect parameter and extends the base of the rectangle to bound the last line of text. If the largest word is wider than the rectangle, the width is expanded. If the text is less than the width of the rectangle, the width is reduced. If there is only one line of text, DrawText modifies the right side of the rectangle so that it bounds the last character in the line. In either case, DrawText returns the height of the formatted text but does not draw the text.

Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
  • Thanks your comments. But it still doesn't work even I removed "DT_CALCRECT" or increased the width and height of the lpRect passed to DrawText. – yw5643 Nov 28 '13 at 06:18
  • To my surprise, DrawText can work in the WM_SIZE handler. But in the WM_SIZE handler, I passed a window DC to DrawText instead of the one created by CreateCompatibleDC. – yw5643 Nov 28 '13 at 06:20
  • Ah, I have found the root cause. As you said, if I pass DT_CALCRECT to DrawText without enghou width or height, it will return the RECT instead of output the text. So I should call DrawText twice. Thanks your remind. – yw5643 Nov 28 '13 at 13:04