MFC界面設置基礎


 switch (nCtlColor) 

 {  

     case CTLCOLOR_DLG: 
	HBRUSH aBrush;  
	aBrush = CreateSolidBrush(RGB(0, 150, 0)); 
	hbr = aBrush; 
    break; 
	
	 case CTLCOLOR_STATIC:
     //讓靜態文本框透明   
     pDC->SetBkMode(TRANSPARENT);   
     //pDC->SetTextColor(RGB(255,255,255));   //設置字體顏色白色 
     hbr=(HBRUSH)::GetStockObject(NULL_BRUSH);   

 break;  } 

以上對對話框和靜態文本進行了設置,寫在重載的CTLCOLOR中,此外還可以利用 case ID進行更多操作。


更改按鈕的顏色

首先 需要在按鈕屬性當中 改成所有者繪製,然後

void CProjectDlg::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct) 
{
	// TODO: Add your message handler code here and/or call default
	
	if(nIDCtl==IDOK)         
	{ CDC dc;     RECT rect;      dc.Attach(lpDrawItemStruct ->hDC); 
	rect = lpDrawItemStruct->rcItem;   
	dc.Draw3dRect(&rect,RGB(255,255,255),RGB(0,0,0));        
	dc.FillSolidRect(&rect,RGB(100,100,255));
	UINT state=lpDrawItemStruct->itemState;  
	if((state & ODS_SELECTED))     {          dc.DrawEdge(&rect,EDGE_SUNKEN,BF_RECT);       }    
	else 
    {          dc.DrawEdge(&rect,EDGE_RAISED,BF_RECT);     }       dc.SetBkColor(RGB(100,100,255));  
	dc.SetTextColor(RGB(255,0,0));     
	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     
	}  CDialog::OnDrawItem(nIDCtl, lpDrawItemStruct);
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章