更改按钮的颜色 [英] Changing the colour of push button

查看:96
本文介绍了更改按钮的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在做一个程序,通过单击按钮从组合框中选择颜色来更改按钮的颜色,编辑,复选框.我的问题是按钮的颜色没有改变.请帮助我使用所有者绘制来更改按钮的颜色,我没有错误,但是没有显示输出,它显示了一个弹出窗口,指出
调试断言失败"
程序:D:\ output \ assign \ debug \ combotest.exe
文件:winctrl1.cpp
第72行
有关程序如何导致断言失败的信息,请参见有关断言的可视c ++文档 (按重试以调试应用程序)

为什么会这样来?我是MFC的新手.如果我很抱歉浪费您的时间,请更正我的错误.

这是我的代码

Hi,

I am doing a program to change the colour of push button, edit, check box by choosing the colour from combo box by clicking push button. My problem is the push button colour is not changing. Please help me to change the colour of the push button using owner draw, I have no error but the output is not displaying,, It shows a pop up window stating
"Debug Assertion Failed"
Prog: D:\output\assign\debug\combotest.exe
File:winctrl1.cpp
Line 72
For information on how your program can cause an assertion failure, see the visual c++ doc on asserts (Press retry to debug the app)

Why it is coming like this? I am new to MFC. If I am very silly sorry for wasting your time, please correct my mistake.

here is my code

void CAboutDlg::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct) 
{
	// TODO: Add your message handler code here and/or call default
    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(255,0,0));
   ::DrawText(lpDrawItemStruct->hDC, strText, strText.GetLength(), 
      &lpDrawItemStruct->rcItem, DT_SINGLELINE|DT_VCENTER|DT_CENTER);
   ::SetTextColor(lpDrawItemStruct->hDC, crOldColor);
}


谢谢
AJ


thank you
AJ

推荐答案

它不是自描述的吗? CtlType 不是 ODT_BUTTON.为什么要写断言(如果您是第一位的话)不知道断言在运行时失败意味着什么.您正在尝试编写MCF代码,该代码将所有者绘图应用于窗口上的所有按钮,而没有控件类型的子类.您应该修改此代码以不仅处理按钮,或者确保仅按钮使用*OWNERDRAW*样式(对于按钮,它是BS_OWNERDRAW)样式.

另一个(更好)的选项是对需要自定义呈现的控件进行子类化.

—SA
Isn''t it self-describing? CtlType is not ODT_BUTTON. Why writing assertion if first place is you don''t know what the failure of assertion during run time means. You''re trying to write MCF code which applies owner draw to all buttons on the windows without sub-classing of control types. You should either adapt this code to handle not only buttons, or make sure that only buttons use *OWNERDRAW* (for buttons, it is BS_OWNERDRAW) style.

Another (better) option is to sub-class control which need custom rendering.

—SA


这篇关于更改按钮的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆