📄 subject_25548.htm
字号:
<p>
序号:25548 发表者:flyerlhj 发表日期:2002-12-23 18:28:52
<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>
回复者:songxinye 回复日期:2002-12-23 19:25:26
<br>内容:Handling Reflected Messages: An Example of a Reusable control<BR><BR>This simple example creates a reusable control called CYellowEdit. The control works the same as a regular edit control except that it displays black text on a yellow background. It would be easy to add member functions that would allow the CYellowEdit control to display different colors.<BR><BR>To try this example, do the following steps: <BR><BR>Create a new dialog box in an existing application. For more information seedialog editor in the Visual C++ User’s Guide.<BR>You must have an application in which to develop the reusable control. If you don’t have an existing application to use, create a dialog-based application using AppWizard.<BR><BR>With your project loaded into Visual C++, use ClassWizard to create a new class called CYellowEdit based on CEdit. Leave the “Add to Component Gallery” box checked.<BR><BR><BR>Add three member variables to your CYellowEdit class. The first two will be COLORREF variables to hold the text color and the background color. The third will be a CBrush object which will hold the brush for painting the background. The CBrush object allows you to create the brush once, merely referencing it after that, and to destroy the brush automatically when the CYellowEdit control is destroyed.<BR><BR><BR>Initialize the member variables by writing the constructor as follows:<BR>CYellowEdit::CYellowEdit()<BR>{<BR>m_clrText = RGB( 0, 0, 0 );<BR>m_clrBkgnd = RGB( 255, 255, 0 );<BR>m_brBkgnd.CreateSolidBrush( m_clrBkgnd );<BR>}<BR><BR>Using ClassWizard, add a handler for the reflected WM_CTLCOLOR message to your CYellowEdit class. Note that the equal sign in front of the message name in the list of messages you can handle indicates that the message is reflected. This is described inDefining a Message Handler for a Reflected Message in the Visual C++ Programmer's Guide.<BR>ClassWizard adds the following message-map macro and skeleton function for you:<BR><BR>ON_WM_CTLCOLOR_REFLECT()<BR><BR>// Note: other code will be in between....<BR><BR>HBRUSH CYellowEdit::CtlColor(CDC* pDC, UINT nCtlColor) <BR>{<BR>// TODO: Change any attributes of the DC here<BR><BR>// TODO: Return a non-NULL brush if the<BR>// parent's handler should not be called<BR>return NULL;<BR>}<BR><BR>Replace the body of the function with the following code. The code specifies the text color, the text background color, and the background color for rest of the control.<BR>pDC->SetTextColor( m_clrText ); // text<BR>pDC->SetBkColor( m_clrBkgnd ); // text bkgnd<BR>return m_brBkgnd; // ctl bkgnd<BR><BR>Create an edit control in your dialog box, then attach it to a member variable by double-clicking the edit control while holding a control key down. In the Add Member Variable dialog box, finish the variable name and choose “Control” for the category, then “CYellowEdit” for the variable type. Don’t forget to set the tab order in the dialog box. Also, be sure to include the header file for the CYellowEdit control in your dialog box’s header file.<BR><BR><BR>Build and run your application. The edit control will have a yellow background.<BR><BR><BR>You can now use Component Gallery to add your CYellowEdit control class to other projects. <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>
回复者:flyerlhj 回复日期:2002-12-27 11:23:45
<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-27 16:22:50
<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>
回复者:songxinye 回复日期:2002-12-27 18:34:23
<br>内容:这是MSDN中的
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:HeartManager 回复日期:2002-12-27 19:38:12
<br>内容:自己写一些DLL,继承控件类就可以实现。
<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>回复者:Shadow_wsh 回复日期:2002-12-27 19:46:09
<br>内容:具体参见MSDN中的OnCtlColor:<BR><BR>HBRUSH CDialogDiscount::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) <BR>{<BR> HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);<BR> <BR> // TODO: Change any attributes of the DC here<BR> <BR> if(nCtlColor==CTLCOLOR_EDIT)<BR> {<BR> pDC->SetBkColor(RGB(0,130,130));<BR> return m_hGrayBrush;<BR> }<BR> if(nCtlColor==CTLCOLOR_STATIC)<BR> {<BR> pDC->SetBkColor(RGB(0,0,0));<BR> pDC->SetTextColor(RGB(255, 255, 255));<BR> return m_hBlackBrush;<BR> }<BR> if(nCtlColor==CTLCOLOR_DLG)<BR> {<BR> pDC->SetBkColor(RGB(0,0,0));<BR> pDC->SetTextColor(RGB(255, 255, 255));<BR> return m_hBlackBrush;<BR> }<BR> // TODO: Return a different brush if the default is not desired<BR> 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>
回复者:flyerlhj 回复日期:2002-12-29 13:12:13
<br>内容:谢谢各位, 问题找到了,原因是onctlcolor()函数就返回HBRUSH 类型的数据。<BR>而第一个回复中返回的是一个CBrush对象, 所以不行。<BR><BR>Add three member variables to your CYellowEdit class. The first two will be COLORREF variables to hold the text color and the background color. The third will be a CBrush object which will hold the brush for painting the background. The CBrush object allows you to create the brush once, merely referencing it after that, and to destroy the brush automatically when the CYellowEdit control is destroyed.<BR><BR><BR><BR>Replace the body of the function with the following code. The code specifies the text color, the text background color, and the background color for rest of the control.<BR>pDC->SetTextColor( m_clrText ); // text<BR>pDC->SetBkColor( m_clrBkgnd ); // text bkgnd<BR>return m_brBkgnd; // ctl bkgnd<BR><BR><BR>谢谢各位<BR><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 + -