subject_20949.htm

来自「一些关于vc的问答」· HTM 代码 · 共 24 行

HTM
24
字号
<p>
序号:20949 发表者:壮志凌云 发表日期:2002-11-12 13:04:53
<br>主题:关于重绘控件的问题
<br>内容:各位大虾,请帮帮我。我自己制作了一个控件类(主要是改变按钮颜色),但不知道为什么无法实现,请帮我看一看<BR>按钮类CColorButton<BR>class CColorButton : public CButton<BR>{<BR>private:<BR>&nbsp;&nbsp;&nbsp;&nbsp;COLORREF m_Color;<BR>public:<BR>&nbsp;&nbsp;&nbsp;&nbsp;CColorButton():CButton(),m_Color(0){};<BR>&nbsp;&nbsp;&nbsp;&nbsp;void ChangeColor(COLORREF color);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//改变颜色<BR>&nbsp;&nbsp;&nbsp;&nbsp;virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);&nbsp;&nbsp;&nbsp;&nbsp;//重载<BR>};<BR>void CColorButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;CDC dc;<BR>&nbsp;&nbsp;&nbsp;&nbsp;dc.Attach(lpDrawItemStruct-&gt;hDC);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//得到绘制的设备环境CDC<BR>&nbsp;&nbsp;&nbsp;&nbsp;VERIFY(lpDrawItemStruct-&gt;CtlType==ODT_BUTTON);<BR>&nbsp;&nbsp;&nbsp;&nbsp;if(lpDrawItemStruct-&gt;itemAction & ODA_DRAWENTIRE)<BR>&nbsp;&nbsp;&nbsp;&nbsp;{//重绘整个控件<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CBrush brush(m_Color);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dc.FillRect(&(lpDrawItemStruct-&gt;rcItem),&brush);<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;if((lpDrawItemStruct-&gt;itemState & ODS_SELECTED) && (lpDrawItemStruct-&gt;itemAction <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& (ODA_SELECT | ODA_DRAWENTIRE)))<BR>&nbsp;&nbsp;&nbsp;&nbsp;{//高亮边框<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;COLORREF fc=RGB(255-GetRValue(m_Color),255-GetGValue(m_Color),255-GetBValue(m_Color));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CBrush brush(fc);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dc.FrameRect(&(lpDrawItemStruct-&gt;rcItem),&brush);<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;if(!(lpDrawItemStruct-&gt;itemState & ODS_SELECTED) && (lpDrawItemStruct-&gt;itemAction & ODA_SELECT))<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CBrush brush(m_Color);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dc.FrameRect(&lpDrawItemStruct-&gt;rcItem,&brush);<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;dc.Detach();<BR>}<BR>//改变颜色<BR>void CColorButton::ChangeColor(COLORREF color)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;CRect Rect;<BR>&nbsp;&nbsp;&nbsp;&nbsp;m_Color=color;<BR>&nbsp;&nbsp;&nbsp;&nbsp;GetClientRect(&Rect);<BR>}<BR>在对话框中定义一个按钮IDC_COLORBUTTON<BR>BOOL CMyDialogDlg::OnInitDialog()<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;CDialog::OnInitDialog();<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;// Set the icon for this dialog.&nbsp;&nbsp;The framework does this automatically<BR>&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;&nbsp;when the application's main window is not a dialog<BR>&nbsp;&nbsp;&nbsp;&nbsp;m_ColorButton.SubclassDlgItem(IDC_COLORBUTTON,this);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//接管消息<BR>&nbsp;&nbsp;&nbsp;&nbsp;m_ColorButton.ChangeColor(RGB(255,0,0));&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//设置为红色<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;SetIcon(m_hIcon, TRUE);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Set big icon<BR>&nbsp;&nbsp;&nbsp;&nbsp;SetIcon(m_hIcon, FALSE);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Set small icon<BR>&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;// TODO: Add extra initialization here<BR>&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;return TRUE;&nbsp;&nbsp;// return TRUE&nbsp;&nbsp;unless you set the focus to a control<BR>}<BR>void CMyDialogDlg::OnColorbutton() <BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;// TODO: Add your control notification handler code here<BR>&nbsp;&nbsp;&nbsp;&nbsp;int r=int(((float)rand()/RAND-MAX)*255);<BR>&nbsp;&nbsp;&nbsp;&nbsp;int g=int(((float)rand()/RAND-MAX)*255);<BR>&nbsp;&nbsp;&nbsp;&nbsp;int b=int(((float)rand()/RAND-MAX)*255);<BR>&nbsp;&nbsp;&nbsp;&nbsp;m_ColorButton.ChangeColor(RGB(r,g,b));<BR>&nbsp;&nbsp;&nbsp;&nbsp;<BR>}<BR>
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
<font color=red>答案被接受</font><br>回复者:iwill 回复日期:2002-11-12 13:13:54
<br>内容:重载button类的PreSubClassWindow,将button类的style加上属性BS_OWNERDRAW
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:CDMA2000 回复日期:2002-11-12 13:22:07
<br>内容:看看这里的代码
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:壮志凌云 回复日期:2002-11-12 13:49:31
<br>内容:感谢 iwill,做出来了
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?