改变按钮的背景色OnCtlColor,why?

有关OnCtlColor的说明MSDN中解释如下:

Most controls send this message to their parent (usually a dialog box) to prepare thepDC for drawing the control using the correct colors.

To change the text color, call the SetTextColor member function with the desired red, green, and blue (RGB) values.

To change the background color of a single-line edit control, set the brush handle in both theCTLCOLOR_EDIT and CTLCOLOR_MSGBOX message codes, and call theCDC::SetBkColor function in response to the CTLCOLOR_EDIT code.

OnCtlColor will not be called for the list box of a drop-down combo box because the drop-down list box is actually a child of the combo box and not a child of the window. To change the color of the drop-down list box, create aCComboBox with an override of OnCtlColor that checks forCTLCOLOR_LISTBOX in the nCtlColor parameter. In this handler, theSetBkColor member function must be used to set the background color for the text.

Note

This member function is called by the framework to allow your application to handle a Windows message. The parameters passed to your function reflect the parameters received by the framework when the message was received. If you call the base-class implementation of this function, that implementation will use the parameters originally passed with the message and not the parameters you supply to the function.

但在实际应用中,你却可以发现并不能改变按钮的背景色!!代码如下:

HBRUSH CMyDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
	// TODO: Change any attributes of the DC here 
	if (pWnd->GetDlgCtrlID() == IDC_BUTTON_1) //为什么这里无法改变按钮颜色??  
         //此处就算是按照MSDN中 nCtlColor == CTLCOLOR_BTN 也是不行哦。
	{
		pDC-> SetTextColor(RGB(15,5,200));
	}
	return hbr; // TODO: Return a different brush if the default is not desired
}


暂时无解?google上不了,先mark一下了。

百度上的搜索结果是需要自己重载CButton的DrawItem。还是使用CButtonST吧。

有关UI的那些事。。。。。。还是转向QT啦。

嘿嘿,既然吃不了荷包蛋,就该吃蛋炒饭吧,何必去追求鸡为何不会下鸭蛋呢!

附上DrawItem的source code:设定按钮的文本颜色为蓝色!

void MyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct )
{
	// TODO:  Add your code to draw the specified item
	UINT uStyle = DFCS_BUTTONPUSH;

	// This code only works with buttons.
	ASSERT(lpDrawItemStruct->CtlType == ODT_BUTTON);

	// If drawing selected, add the pushed style to DrawFrameControl.
	if (lpDrawItemStruct->itemState & ODS_SELECTED)
		uStyle |= DFCS_PUSHED;

	// Draw the button frame.
	::DrawFrameControl(lpDrawItemStruct->hDC, &lpDrawItemStruct->rcItem, 
		DFC_BUTTON, uStyle);

	// Get the button's text.
	CString strText;
	GetWindowText(strText);

	// Draw the button text using the text color red.
	COLORREF crOldColor = ::SetTextColor(lpDrawItemStruct->hDC, RGB(0,0,255));
	::DrawText(lpDrawItemStruct->hDC, strText, strText.GetLength(), 
		&lpDrawItemStruct->rcItem, DT_SINGLELINE|DT_VCENTER|DT_CENTER);
	::SetTextColor(lpDrawItemStruct->hDC, crOldColor);

}


 

MFC中,可以通过WM_CTLCOLOR消息和OnCtlColor函数来设置按钮控件的背景色。具体步骤如下: 1.在你的对话框类中添加一个成员变量,用于存储按钮控件的背景色,例如: ```cpp HBRUSH m_hBrush = CreateSolidBrush(RGB(255, 0, 0)); // 创建一个红色画 ``` 2.在你的对话框类中添加一个OnCtlColor函数,用于处理WM_CTLCOLOR消息,例如: ```cpp HBRUSH CMyDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor); if (nCtlColor == CTLCOLOR_BTN) // 如果是按钮控件 { pDC->SetBkColor(RGB(255, 0, 0)); // 设置按钮控件的背景色为红色 hbr = m_hBrush; // 返回画句柄 } return hbr; } ``` 3.在你的OnInitDialog函数中添加以下代码,将按钮控件的背景色设置为透明: ```cpp CButton* pButton = (CButton*)GetDlgItem(IDC_BUTTON1); // 获取按钮控件的指针 pButton->SetWindowText(_T("")); // 设置按钮控件的文本为空 pButton->ModifyStyle(0, BS_OWNERDRAW); // 设置按钮控件为自绘模式 ``` 4.在你的对话框类的头文件中添加以下代码,用于声明OnCtlColor函数: ```cpp afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor); ``` 5.在你的对话框类的消息映射中添加以下代码,用于将WM_CTLCOLOR消息映射到OnCtlColor函数: ```cpp ON_WM_CTLCOLOR() ``` 至此,你已经成功地设置了按钮控件的背景色。需要注意的是,如果你想设置其他类型控件的背景色,只需要在OnCtlColor函数中根据nCtlColor参数的值进行判断即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值