1. 程式人生 > >關於vc中Button的顏色字型與背景圖片的修改

關於vc中Button的顏色字型與背景圖片的修改

void bb::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
 
 CDC dc;
    CRect rect;
    dc.Attach(lpDrawItemStruct ->hDC);   // Get the Button DC to CDC
 rect=lpDrawItemStruct->rcItem;
 
    rect = lpDrawItemStruct->rcItem;     //Store the Button rect to our local rect.
 rect = lpDrawItemStruct -> rcItem;
 
 
    CBitmap m_Bitmap;
    CDC dcMem;
    BITMAP s_Bmp;
    if(image){
     m_Bitmap.Attach(m_hBitmap);
     dcMem.CreateCompatibleDC(&dc);     // 創建於記憶體裝置環境相相容的裝置環境
     dcMem.SelectObject(&m_Bitmap);
     // 將點陣圖物件選入到記憶體裝置環境中
     m_Bitmap.GetBitmap(&s_Bmp);        // 獲取點陣圖資訊
     dc.StretchBlt(rect.left,rect.top,rect.Width(),rect.Height(),
      &dcMem,0,0,s_Bmp.bmWidth,s_Bmp.bmHeight,SRCCOPY);
    }else{      
     //dc.Draw3dRect(&rect,RGB(2,25,25),RGB(10,0,10));
    
     dc.FillSolidRect(&rect,bkcolor);//Here you can define the required color to appear on the Button.
    }
    UINT state=lpDrawItemStruct->itemState; //This defines the state of the Push button either pressed or not.
   
    if((state & ODS_SELECTED))
    {
     dc.DrawEdge(&rect,EDGE_SUNKEN,BF_RECT);
    
    }
    else
    {
     dc.DrawEdge(&rect,EDGE_RAISED,BF_RECT);
    }
    dc.SetBkMode(TRANSPARENT);
    dc.SetBkColor(textbkcolor);   //Setting the Text Background color
    dc.SetTextColor(textcolor);     //Setting the Text Color
   
    TCHAR buffer[MAX_PATH];           //To store the Caption of the button.
    ZeroMemory(buffer,MAX_PATH );     //Intializing the buffer to zero
    ::GetWindowText(lpDrawItemStruct->hwndItem,buffer,MAX_PATH); //Get the Caption of Button Window
   
    dc.DrawText(buffer,&rect,DT_CENTER|DT_VCENTER|DT_SINGLELINE);//Redraw the Caption of Button Window
   
    dc.Detach(); // Detach the Button DC
   
}