문제

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.

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top