rcTitle.right -= 8;
TCHAR tzTitle[64] = { 0 };//最多只显示前64个字符
GetWindowText( hWnd,tzTitle,sizeof(tzTitle) );
int nOldBkMode = SetBkMode( hMemDC,TRANSPARENT );
HFONT hFont = (HFONT)::SendMessage( hWnd, WM_GETFONT, 0, 0 );
HFONT hFontTitle;
LOGFONT lf;
if( hFont != NULL )
{
::GetObject( hFont, sizeof(LOGFONT),&lf );
//lf.lfWeight = FW_BOLD;
}
else
{
SystemParametersInfo( SPI_GETICONTITLELOGFONT,sizeof(LOGFONT),&lf,0 );
//lf.lfWeight = FW_BOLD;
} hFontTitle = CreateFontIndirect( &lf );
HFONT hOldFont = (HFONT)SelectObject( hMemDC,hFontTitle );
DrawText( hMemDC,tzTitle,_tcsclen( tzTitle ),&rcTitle,DT_LEFT | DT_SINGLELINE | DT_VCENTER );
SelectObject( hMemDC,hOldFont );
DeleteObject( hFontTitle );
SetBkMode( hMemDC,nOldBkMode );
        SetTextColor(hMemDC,RGB(255,0,255));//设置字体颜色为什么不行?
BitBlt( hDC,0,0,nWidth,nHeight,hMemDC,0,0,SRCCOPY );
SelectObject( hMemDC,hOldBitmap );

解决方案 »

  1.   

    SetTextColor(hMemDC,RGB(255,0,255));//设置字体颜色为什么不行?这个设置好了需要你自己用TextOut输出的。
    不是设置一下,CDC中的颜色就变换了
      

  2.   

    将  SetTextColor(hMemDC,RGB(255,0,255));放置到DrawText调用之前,否则不起作用啊。所有DC的设置,都应该在实际的绘制函数之前设定,否则没有用的
      

  3.   

    发一个给你:
    HBITMAP hBitmap=(HBITMAP)LoadImage(0,"C:\\1.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
     
    //      HBITMAP  hBitmap=LoadBitmap(GetModuleHandle(NULL),MAKEINTRESOURCE(IDB_BITMAP1));
     
      HDC hMemDC=CreateCompatibleDC(item->hDC);

      HBITMAP hOldBitmap=(HBITMAP)SelectObject(hMemDC,(HGDIOBJ)hBitmap);
     
    BITMAP bitmap;
     
      GetObject(hBitmap,sizeof(BITMAP),&bitmap);
     
      StretchBlt(item->hDC,0,0,item->rcItem.right,item->rcItem.bottom,hMemDC,0,0,bitmap.bmWidth,bitmap.bmHeight,SRCCOPY); 
     
      SelectObject(hMemDC,hOldBitmap);
     
      DeleteObject((HGDIOBJ)hBitmap);
     
      DeleteObject((HGDIOBJ)hMemDC);

    FrameRect(item->hDC,&(item->rcItem),(HBRUSH)GetStockObject(BLACK_BRUSH));
    // Draw3dRect(item->hDC,&(item->rcItem),
    // RGB(255,0,0), RGB(128,138,135));


    //  Set the text color to gray if the button is selected.
    if( item->itemState & ODS_SELECTED )
    {
    SetTextColor( item->hDC, RGB( 128, 128, 128 ) ); //gray
    }
    else
    {
    SetTextColor( item->hDC, RGB( 255, 0, 0 ) ); //red
    }


    //  This will make the text move down and to the right when the button is
    //  pressed.
    rect = item->rcItem;
    if( item->itemState & ODS_SELECTED )
    {
    rect.top  += 2;
    rect.left += 2;
    }

    char title[256];
    GetWindowText( item->hwndItem, title, sizeof( title ) );
    DrawText( item->hDC, title, -1, &rect,
    DT_CENTER | DT_VCENTER | DT_SINGLELINE );

      

  4.   

    楼上说的非常对: rcTitle.right -= 8;
    TCHAR tzTitle[64] = { 0 };//最多只显示前64个字符
    GetWindowText( hWnd,tzTitle,sizeof(tzTitle) );
    int nOldBkMode = SetBkMode( hMemDC,TRANSPARENT );
    HFONT hFont = (HFONT)::SendMessage( hWnd, WM_GETFONT, 0, 0 );
    HFONT hFontTitle;
    LOGFONT lf;
    if( hFont != NULL )
    {
    ::GetObject( hFont, sizeof(LOGFONT),&lf );
    //lf.lfWeight = FW_BOLD;
    }
    else
    {
    SystemParametersInfo( SPI_GETICONTITLELOGFONT,sizeof(LOGFONT),&lf,0 );
    //lf.lfWeight = FW_BOLD;
    } hFontTitle = CreateFontIndirect( &lf );
    HFONT hOldFont = (HFONT)SelectObject( hMemDC,hFontTitle );
    SetTextColor(hMemDC,RGB(255,0,255));//设置字体颜色为什么不行?  要放在这里才行。
    DrawText( hMemDC,tzTitle,_tcsclen( tzTitle ),&rcTitle,DT_LEFT | DT_SINGLELINE | DT_VCENTER );
    SelectObject( hMemDC,hOldFont );
    DeleteObject( hFontTitle );
    SetBkMode( hMemDC,nOldBkMode );
            
    BitBlt( hDC,0,0,nWidth,nHeight,hMemDC,0,0,SRCCOPY );
    SelectObject( hMemDC,hOldBitmap );
      

  5.   

    建议尽量不要使用DrawText,使用TextOut;因为DrawText有些CDC属性会受上一次设置的影响,不好把控
      

  6.   

    1. 选中到dc里就可以立即删除字体?难道不需要bitblt后删除字体?是不是过于提前删除了,会不会有隐患?2.为什么要hFontTitle = CreateFontIndirect( &lf )? 不是已经有字体了吗?有字体还再次create作甚?
      

  7.   

    rcTitle.right -= 8;
    TCHAR tzTitle[64] = { 0 };//最多只显示前64个字符
    GetWindowText( hWnd,tzTitle,sizeof(tzTitle) );
    int nOldBkMode = SetBkMode( hMemDC,TRANSPARENT );
    //实际上直接下面的这个hFont也可以的,不用CreateFontIndirect的
    HFONT hFont = (HFONT)::SendMessage( hWnd, WM_GETFONT, 0, 0 );
    HFONT hFontTitle;
    LOGFONT lf;
    //但是获取的hFont有可能是空的,所以一方面为了保险,二方面为了给字体设置一个粗体效果,虽然后来我在下面给注释起来了,
    if( hFont != NULL )
    {
    ::GetObject( hFont, sizeof(LOGFONT),&lf );
    //lf.lfWeight = FW_BOLD;
    }
    else
    {
    SystemParametersInfo( SPI_GETICONTITLELOGFONT,sizeof(LOGFONT),&lf,0 );
    //lf.lfWeight = FW_BOLD;
    }
    //综上,我才重新创建一个FONT,其实不创建也行,但只能使用获取的hFont,要想改变一下这个hFont,比如字体啊,粗体啊,字号啊,什么的,就只好创建一个了。
    hFontTitle = CreateFontIndirect( &lf );
    //我是选中到内存DC了,不是屏幕DC
    HFONT hOldFont = (HFONT)SelectObject( hMemDC,hFontTitle );
    SetTextColor(hMemDC,RGB(255,0,255));//设置字体颜色为什么不行?  要放在这里才行。
    //然后在内存DC里画文字
    DrawText( hMemDC,tzTitle,_tcsclen( tzTitle ),&rcTitle,DT_LEFT | DT_SINGLELINE | DT_VCENTER );
    SelectObject( hMemDC,hOldFont );
    //画完文字,文字已经在内存DC上有了,然后才删除FONT,不影响下面的内存DC到屏幕DC的BitBlt
    DeleteObject( hFontTitle );
    SetBkMode( hMemDC,nOldBkMode );
            
    BitBlt( hDC,0,0,nWidth,nHeight,hMemDC,0,0,SRCCOPY );
    SelectObject( hMemDC,hOldBitmap );