下面的函数将菜单中的文字写入内存DC于目标区域(非一种颜色)异或后输出在屏幕菜单上,奇怪的是字体变大了,好像还变成了粗体
void CXPMenu::InvertText(CDC* pDC,CRect rect,CString sText)
{
int nWidth = rect.right - rect.left;
int nHeight = rect.bottom - rect.top; CBitmap bmpText;
bmpText.CreateCompatibleBitmap(pDC,nWidth,nHeight);
CDC *pMDC = new CDC;
pMDC->CreateCompatibleDC(pDC);
pMDC->SelectObject(&bmpText);
pMDC->SetBkMode(TRANSPARENT);
pMDC->SetTextColor(RGB(255,255,255));
CRect rcText;
rcText.left = 0;
rcText.top = 0;
rcText.right = nWidth;
rcText.bottom = nHeight;
pMDC->DrawText(sText, &rcText, DT_EXPANDTABS|DT_VCENTER|DT_SINGLELINE); pDC->BitBlt(rect.left,rect.top,nWidth,nHeight,pMDC,0,0,SRCINVERT); bmpText.DeleteObject();
delete pMDC;
}

解决方案 »

  1.   

    我觉得是DrawText函数出现的你不想要的现象
      

  2.   

    仔细阅读一下:--------------------------------------------------------------------------
    CDC::DrawText  
    virtual int DrawText( LPCTSTR lpszString, int nCount, LPRECT lpRect, UINT nFormat );int DrawText( const CString& str, LPRECT lpRect, UINT nFormat );Return ValueThe height of the text if the function is successful.如果成功,则返回文本高度。 ParameterslpszStringPoints to the string to be drawn. If nCount is –1, the string must be null-terminated.指向要绘制的文本的指针。如果nCount是-1,该字符串必须是空终止的 nCountSpecifies the number of chars in the string. If nCount is –1, then lpszString is assumed to be a long pointer to a null-terminated string and DrawText computes the character count automatically.字符串中字符数目。如果为-1,lpszString被认为是一个指向空终止的字符串的长指针。DrawText自动计算字符数目。 lpRectPoints to a RECT structure or CRect object that contains the rectangle (in logical coordinates) in which the text is to be formatted.指向RECT结构或Crect对象的指针,结构(或对象)中包含有矩形(逻辑单位表示),其中的文本带有格式。 str A CString object that contains the specified characters to be drawn.含有要被绘制的文本的Cstring对象。 nFormatSpecifies the method of formatting the text. It can be any combination of the following values (combine using the bitwise OR operator): 指定格式化文本的方法。它可以是下列值的组合(可用运算符或位操作符进行组合): DT_BOTTOM   Specifies bottom-justified text. This value must be combined with DT_SINGLELINE.DT_BOTTOM 底部对齐的文本。该值必须同DT_SINGLELINE组合使用。DT_CALCRECT   Determines the width and height of the rectangle. If there are multiple lines of text, DrawText will use the width of the rectangle pointed to by lpRect and extend the base of the rectangle to bound the last line of text. If there is only one line of text, DrawText will modify 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.DT_CALCRECT 决定矩形的宽度和高度,如果是多行文字,DrawText将会使用lpRect指向的矩形,并扩展矩形以容纳文本。如果是单行文本,DrawText将会调整矩形的右边以便容纳该行文字。两种情况下,DrawText都返回格式化文本的高度,但并不绘制它们。DT_CENTER   Centers text horizontally.DT_CENTER 将文本水平居中。 
    DT_END_ELLIPSIS or DT_PATH_ELLIPSIS   Replaces part of the given string with ellipses, if necessary, so that the result fits in the specified rectangle. The given string is not modified unless the DT_MODIFYSTRING flag is specified. 
    DT_END_ELLIPSIS或DT_PATH _ELLIPSIS 如果必要,将用椭圆替代部分指定的文本,以便适合给定的矩形。除非指定DT_MODIFYSTRING 标志,否则给定的字符串不会被修改。 You can specify DT_END_ELLIPSIS to replace characters at the end of the string, or DT_PATH_ELLIPSIS to replace characters in the middle of the string. If the string contains backslash (\) characters, DT_PATH_ELLIPSIS preserves as much as possible of the text after the last backslash.可以指定DT_END_ELLIPSIS在字符串末端替代字符,或者指定DT_PATH_ELLIPSIS在字符串中间替代字符。如果字符串中含有反斜杠(\),DT_PATH_ELLIPSIS将尽可能保存在最后一个反斜杠后的文本。 DT_EXPANDTABS   Expands tab characters. The default number of characters per tab is eight.DT_EXPANDTABS 扩展制表符,每一制表符所含字符的缺省数目是8。 
    DT_EXTERNALLEADING   Includes the font’s external leading in the line height. Normally, external leading is not included in the height of a line of text.DT_EXTERNALLEADING包括在行高中的字体外部间隔。通常外部间隔不包括在一行文本的高度之中。 
    DT_LEFT   Aligns text flush-left.
      

  3.   

    DT_LEFT 文本左对齐。 
    DT_MODIFYSTRING   Modifies the given string to match the displayed text. This flag has no effect unless the DT_END_ELLIPSIS or DT_PATH_ELLIPSIS flag is specified. 
    DT_MODIFYSTRING 修正给定字符串以便与显示文本匹配。只有在指定了DT_END_ELLIPSIS 或DT_ PATH_ELLIPSIS 标志时,该标志才起作用。 Note Some uFormat flag combinations can cause the passed string to be modified. Using DT_MODIFYSTRING with either DT_END_ELLIPSIS or DT_PATH_ELLIPSIS may cause the string to be modified, causing an assertion in the CString override.某些uFormat标志组合能导致传递的字符串受到修改。DT_MODIFYSTRING同DT_END_ELLIPSIS 或DT_PATH_ELLIPSIS 一起使用会导致字符串修改(重新调整),导致在Cstring覆盖中异常。 DT_NOCLIP   Draws without clipping. DrawText is somewhat faster when DT_NOCLIP is used.DT_NOCLIP 绘制但不剪切, DrawText 在使用DT_NOCLIP后总能运行得快一些。 DT_NOPREFIX   Turns off processing of prefix characters. Normally, DrawText interprets the ampersand (&) mnemonic-prefix character as a directive to underscore the character that follows, and the two-ampersand (&&) mnemonic-prefix characters as a directive to print a single ampersand. By specifying DT_NOPREFIX, this processing is turned off.DT_NOPREFIX 关闭前缀字符,通常DrawText 将助记前缀&理解为其后字符加上下划线,&&前缀符表示显示一个&字符,指定 DT_NOPREFIX后,也关闭了这种处理。 
    DT_PATH_ELLIPSIS   
    DT_RIGHT   Aligns text flush-right.DT_RIGHT 文本右对齐。 
    DT_SINGLELINE   Specifies single line only. Carriage returns and linefeeds do not break the line.DT_SINGLELING 指定单行,回车与换行不会中断该行。 
    DT_TABSTOP   Sets tab stops. The high-order byte of nFormat is the number of characters for each tab. The default number of characters per tab is eight.DT_TABSTOP 设置制表位,nFormat的高位字节即为每一制表符所含字符的数目,缺省值为8。 
    DT_TOP   Specifies top-justified text (single line only).DT_TOP 文本靠上对齐(只用于单行文字)。 
    DT_VCENTER   Specifies vertically centered text (single line only).DT_VCENTER 垂直居中对齐(只用于单行文字)。 
    DT_WORDBREAK   Specifies word-breaking. Lines are automatically broken between words if a word would extend past the edge of the rectangle specified by lpRect. A carriage return–linefeed sequence will also break the line. 
            DT_WORDBREAK 设定单词中断。如果超过lpRect指定的矩形边界,行会自动在词间断开,回车与换行也会断行。 Note  The values DT_CALCRECT, DT_EXTERNALLEADING, DT_INTERNAL, DT_NOCLIP, and DT_NOPREFIX cannot be used with the DT_TABSTOP value.DT_CALCRECT,DT_EXTERNALLEADING,DT_INTERNAL,DT_NOCLIP和DT_NOPREFIX不能与DT_TABSTOP一同使用。 ResCall this member function to format text in the given rectangle. It formats text by expanding tabs into appropriate spaces, aligning text to the left, right, or center of the given rectangle, and breaking text into lines that fit within the given rectangle. The type of formatting is specified by nFormat. 在给定的矩形内调用该成员函数格式化文本。通过将制表值扩展到适当大小,使文本在给定矩形内左对齐、右对齐或居中,使文本断成多行以适应给定矩形来格式化文本,格式类型由nFormat指定。 This member function uses the device context’s selected font, text color, and background color to draw the text. Unless the DT_NOCLIP format is used, DrawText clips the text so that the text does not appear outside the given rectangle. All formatting is assumed to have multiple lines unless the DT_SINGLELINE format is given. 该成员函数适应设备上下文中选取的字体、文本颜色、背景色来显示文本。在采用DT_NOCLIP格式时,DrawText剪切不会使文本显示超过矩形范围,除非选择DT_SINGLELINE格式,所有格式都认为适用于多行文本。 If the selected font is too large for the specified rectangle, the DrawText member function does not attempt to substitute a smaller font.如果所选字体相对于矩形范围过大,DrawText成员函数并不用较小的字体来代替。 If the DT_CALCRECT flag is specified, the rectangle specified by lpRect will be updated to reflect the width and height needed to draw the text.如果指定了DT_CALCRECT标志,由lpRect 指定的矩形将会更新,以反映显示文本需要的高度和宽度。 If the TA_UPDATECP text-alignment flag has been set (see CDC::SetTextAlign), DrawText will display text starting at the current position, rather than at the left of the given rectangle. DrawText will not wrap text when the TA_UPDATECP flag has been set (that is, the DT_WORDBREAK flag will have no effect).如果TA_UPDATECP文本对齐标志已经设置(见CDC::SetTextAlign),DrawText将在当前位置开始显示文本,而不是在矩形的左边。DrawText在TA_UPDATECP标志已经设置的情况下不会折行(即DT_WORDBREAK标志不起作用)。 
    --------------------------------------------------------------------------
      

  4.   

    问题跟DrawText()没关系,设置下字体就ok了,可能跟所选的DC有关系