改变按钮的背景色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);

}


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值