MFC修改控件颜色

修改CButton背景色

//直接在窗体类中加了个DRAW事件,试了下可以修改BUTTON背景和字体颜色。。。
void CUuiiDlg::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)  
{
	if(nIDCtl==IDC_BUTTON1) //checking for the button
	{
		CDC dc;
		RECT rect;
		dc.Attach(lpDrawItemStruct ->hDC); // Get the Button DC to CDC 
		rect = lpDrawItemStruct->rcItem; //Store the Button rect to our local rect. 
		dc.Draw3dRect(&rect,RGB(255,255,255),RGB(0,0,0)); 
		dc.FillSolidRect(&rect,RGB(100,100,255));//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.SetBkColor(RGB(100,100,255)); //Setting the Text Background color
		dc.SetTextColor(RGB(255,0,0)); //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
	}  
		 
	CDialog::OnDrawItem(nIDCtl, lpDrawItemStruct);
}


1. 修改CStatic背景色

CClientDC dc(this);
RECT m_pRect;
GetDlgItem(IDC_SAMPLE)->GetWindowRect(&m_pRect);
ScreenToClient(&m_pRect);
CBrush NewBrush(RGB(0, 0, 255));
dc.FillRect(&m_pRect, &NewBrush); 


2. 改变CStatic字体
CClientDC hdc(this); 
CFont font; 
font.CreatePointFont(500, "楷体_GB2312",NULL); 
CStatic *m_pbtWnd = &m_button;
m_pbtWnd->SetFont(&font); 


3. 改变CButton字体
CFont *font;   
font = new CFont;   
f->CreateFont(30,0,0,0,
        FW_BOLD,
                  TRUE,
                  TRUE,
                     0,
          ANSI_CHARSET,
    OUT_DEFAULT_PRECIS,                                   
   CLIP_DEFAULT_PRECIS,
       DEFAULT_QUALITY,                                  
DEFAULT_PITCH|FF_SWISS,        
          _T("Arial"));                                   
GetDlgItem(IDC_BUTTON1)->SetFont(f); 


4. 窗口始终在最前面:
方法一:在对话框的属性中,在more   style中有一项是System   model,选上它,即可!
方法二:OnInitDialog里加入SetWindowPos(&wndTopMost,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);


5. 限制CEdit只能输入小写字母
方法一:属性里设置下,LowerCase(输入大写后自动变小写)
方法二:新建个继承自CEdit的类。。然后重载OnChar函数.... (只能输入小写,输入其他不反应)
void CMyEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 

// TODO: Add your message handler code here and/or call default 
if (nChar < 'a' || nChar > 'z') 
return; 
CEdit::OnChar(nChar, nRepCnt, nFlags); 
}

6.MFC弹出非模态对话框
CTestDialog *pTestDlg = new CTestDialog(); 
pTestDlg->Create(IDD_DIALOG_TEST, this); 
pTestDlg->ShowWindow(SW_SHOW); 
///下面三行错误 
//CTestDialog dlg1; 
//dlg1.Create(IDD_DIALOG_TEST, NULL); 
//dlg1.ShowWindow(SW_SHOW); 
为什么前三行不能用后三行代替? 
用后三行,弹出的对话框闪了一下就消失了 
注意一下他们的生命周期


7.如何去掉框架的关闭按钮?
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if( !CFrameWnd::PreCreateWindow(cs) )
   return FALSE;
cs.style &= ~WS_SYSMENU;
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs

return TRUE;
}


8.CStatic 显示图片:
   CPaintDC dc(this); 
   HBITMAP bmp; 
   bmp = (HBITMAP)::LoadImage(AfxGetInstanceHandle(),"E://a.bmp", 
    IMAGE_BITMAP,0,0,    
    LR_CREATEDIBSECTION 
    | LR_DEFAULTSIZE 
    | LR_LOADFROMFILE 
    | LR_DEFAULTCOLOR); 
   ((CStatic *)GetDlgItem(IDC_STATIC1))->ModifyStyle(NULL,SS_BITMAP|SS_CENTERIMAGE,0); 
   ((CStatic *)GetDlgItem(IDC_STATIC1))->SetBitmap(bmp) ;


你可能感兴趣的:(VC/MFC)