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> COLORREF m_Color;<BR>public:<BR> CColorButton():CButton(),m_Color(0){};<BR> void ChangeColor(COLORREF color); //改变颜色<BR> virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct); //重载<BR>};<BR>void CColorButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)<BR>{<BR> CDC dc;<BR> dc.Attach(lpDrawItemStruct->hDC); //得到绘制的设备环境CDC<BR> VERIFY(lpDrawItemStruct->CtlType==ODT_BUTTON);<BR> if(lpDrawItemStruct->itemAction & ODA_DRAWENTIRE)<BR> {//重绘整个控件<BR> CBrush brush(m_Color);<BR> dc.FillRect(&(lpDrawItemStruct->rcItem),&brush);<BR> }<BR> if((lpDrawItemStruct->itemState & ODS_SELECTED) && (lpDrawItemStruct->itemAction <BR> & (ODA_SELECT | ODA_DRAWENTIRE)))<BR> {//高亮边框<BR> COLORREF fc=RGB(255-GetRValue(m_Color),255-GetGValue(m_Color),255-GetBValue(m_Color));<BR> CBrush brush(fc);<BR> dc.FrameRect(&(lpDrawItemStruct->rcItem),&brush);<BR> }<BR> if(!(lpDrawItemStruct->itemState & ODS_SELECTED) && (lpDrawItemStruct->itemAction & ODA_SELECT))<BR> {<BR> CBrush brush(m_Color);<BR> dc.FrameRect(&lpDrawItemStruct->rcItem,&brush);<BR> }<BR> dc.Detach();<BR>}<BR>//改变颜色<BR>void CColorButton::ChangeColor(COLORREF color)<BR>{<BR> CRect Rect;<BR> m_Color=color;<BR> GetClientRect(&Rect);<BR>}<BR>在对话框中定义一个按钮IDC_COLORBUTTON<BR>BOOL CMyDialogDlg::OnInitDialog()<BR>{<BR> CDialog::OnInitDialog();<BR><BR> // Set the icon for this dialog. The framework does this automatically<BR> // when the application's main window is not a dialog<BR> m_ColorButton.SubclassDlgItem(IDC_COLORBUTTON,this); //接管消息<BR> m_ColorButton.ChangeColor(RGB(255,0,0)); //设置为红色<BR><BR> SetIcon(m_hIcon, TRUE); // Set big icon<BR> SetIcon(m_hIcon, FALSE); // Set small icon<BR> <BR> // TODO: Add extra initialization here<BR> <BR> return TRUE; // return TRUE unless you set the focus to a control<BR>}<BR>void CMyDialogDlg::OnColorbutton() <BR>{<BR> // TODO: Add your control notification handler code here<BR> int r=int(((float)rand()/RAND-MAX)*255);<BR> int g=int(((float)rand()/RAND-MAX)*255);<BR> int b=int(((float)rand()/RAND-MAX)*255);<BR> m_ColorButton.ChangeColor(RGB(r,g,b));<BR> <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 + -
显示快捷键?