⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 subject_23785.htm

📁 一些关于vc的问答
💻 HTM
字号:
<p>
序号:23785 发表者:王胜 发表日期:2002-12-06 14:18:52
<br>主题:在用OnCtlColor设置背景色时遇到的问题。请大家帮我解答。
<br>内容:我在设置对话框里的控件的背景色时遇到了一些颜色不能显示的问题。我发现若RGB(X,Y,Z)的x,y.z中若有一个为0时,背京色可以正常显示出来,否则他不能显示出来,不能显示的就以白色代替。<BR>各位帮我看看是什么原因???<BR>HBRUSH DIACOLOR::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) <BR>{<BR>backcolor=RGB(255,255,255);<BR>&nbsp;&nbsp;&nbsp;&nbsp;forcolor=RGB(0,0,0);<BR>&nbsp;&nbsp;&nbsp;&nbsp;gridcolor=RGB(255,120,100);<BR>&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;HBRUSH hbr= CDialog::OnCtlColor(pDC, pWnd, CTLCOLOR_EDIT&amp;CTLCOLOR_MSGBOX ) ;<BR>&nbsp;&nbsp;&nbsp;&nbsp;// TODO: Change any attributes of the DC here<BR>&nbsp;&nbsp;&nbsp;&nbsp;CBrush brush,brush1,brush2;<BR>&nbsp;&nbsp;&nbsp;&nbsp;brush.CreateSolidBrush(forcolor);<BR>&nbsp;&nbsp;&nbsp;&nbsp;brush1.CreateSolidBrush(backcolor);<BR>&nbsp;&nbsp;&nbsp;&nbsp;brush2.CreateSolidBrush(gridcolor);<BR>&nbsp;&nbsp;&nbsp;&nbsp;int nID = pWnd-&gt;GetDlgCtrlID();<BR>&nbsp;&nbsp;&nbsp;&nbsp;if(nID == IDC_EDIT_FORCOLOR)<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hbr = brush;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;if(nID == IDC_EDIT_BACKCOLOR)<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hbr = brush1;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;if(nID == IDC_EDIT_GRIDCOLOR)<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hbr = brush2;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;// TODO: Return a different brush if the default is not desired<BR>&nbsp;&nbsp;&nbsp;&nbsp;return hbr;<BR>}<BR><BR>在用如下的方法在OnInitDialog里初始化刷新背景时根本不会响应。为什么????<BR>&nbsp;&nbsp;&nbsp;&nbsp;CWnd *pWnd=GetDlgItem(IDC_EDIT_FORCOLOR);<BR>&nbsp;&nbsp;&nbsp;&nbsp;CDC *pDC=pWnd-&gt;GetDC();<BR>&nbsp;&nbsp;&nbsp;&nbsp;CBrush br(forcolor);<BR>&nbsp;&nbsp;&nbsp;&nbsp;CRect rc;<BR>&nbsp;&nbsp;&nbsp;&nbsp;m_forcolor.GetClientRect(&amp;rc);<BR>&nbsp;&nbsp;&nbsp;&nbsp;pDC-&gt;FillRect(rc,&amp;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>
回复者:小年 回复日期:2002-12-06 14:49:04
<br>内容:CBrush brush,brush1,brush2;<BR>&nbsp;&nbsp;&nbsp;&nbsp;brush.CreateSolidBrush(forcolor);<BR>&nbsp;&nbsp;&nbsp;&nbsp;brush1.CreateSolidBrush(backcolor);<BR>&nbsp;&nbsp;&nbsp;&nbsp;brush2.CreateSolidBrush(gridcolor);<BR>&nbsp;&nbsp;&nbsp;&nbsp;int nID = pWnd-&gt;GetDlgCtrlID();<BR>&nbsp;&nbsp;&nbsp;&nbsp;if(nID == IDC_EDIT_FORCOLOR)<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hbr = brush;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;if(nID == IDC_EDIT_BACKCOLOR)<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hbr = brush1;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;if(nID == IDC_EDIT_GRIDCOLOR)<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hbr = brush2;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;// TODO: Return a different brush if the default is not desired<BR>&nbsp;&nbsp;&nbsp;&nbsp;return hbr;<BR><BR><BR>一个对象怎么可以直接符值给一个句柄变量呢?<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-12-07 12:56:09
<br>内容:年兄,我在用红黄蓝等颜色的时候他的背景色按照我预想的显示出来了,说明这样是可以赋值的啊!问题并不在这里<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-12-07 15:06:11
<br>内容:HBRUSH CDialogLOG::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) <BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);<BR>&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;switch (nCtlColor)<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;//case CTLCOLOR_DLG:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//ereturn (HBRUSH)brush.GetSafeHandle();<BR>&nbsp;&nbsp;&nbsp;&nbsp;case CTLCOLOR_EDIT:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pDC-&gt;SetBkMode(TRANSPARENT);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pDC-&gt;SetTextColor(RGB(0, 0, 255));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return (HBRUSH)brush.GetSafeHandle();<BR>&nbsp;&nbsp;&nbsp;&nbsp;case CTLCOLOR_STATIC:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pDC-&gt;SetBkMode(TRANSPARENT);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pDC-&gt;SetTextColor(RGB(0, 0, 255));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return (HBRUSH)brush.GetSafeHandle();<BR>&nbsp;&nbsp;&nbsp;&nbsp;case CTLCOLOR_LISTBOX:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pDC-&gt;SetBkMode(TRANSPARENT);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pDC-&gt;SetTextColor(RGB(0, 0, 255));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return (HBRUSH)brush.GetSafeHandle();<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;return hbr;<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-12-08 14:56:19
<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>
<font color=red>答案被接受</font><br>回复者:小年 回复日期:2002-12-08 18:19:21
<br>内容:那请给分
<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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -