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

📄 0511002.htm

📁 VC知识库5_chm_decompile_20040520_210715
💻 HTM
字号:
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title></title>
<link rel="stylesheet" type="text/css" href="../../vckbase.css">
</head>

<body>

<div align="justify">
  <table border="0" width="100%" class="font" height="57">
    <tr>
      <td width="27%" height="6" class="bigfont" bgcolor="#B8CFE7" align="center" bordercolor="#800080">
      <font color="#800080">VC知识库(五)</font>
      </td>
      <td width="73%" height="6" class="bigfont" bgcolor="#B8CFE7" align="center" bordercolor="#800080">
      <font color="#800080">www.vckbase.com</font>
      </td>
    </tr>
    <tr>
      <td width="100%" height="4" class="header" valign="top" align="center" colspan="2">
      <hr>
      </td>
    </tr>
    <tr>
      <td width="100%" height="17" class="header" valign="top" align="center" colspan="2">
      <strong><big><font color="#000080">改变Push Button背景色</font></big></strong> 
      </td> 
    </tr> 
    <tr> 
      <td width="100%" height="17" class="info" align="center" colspan="2"> 
      <strong><big><font color="#000080">闻怡洋</font></big></strong> 
      </td>   
    </tr>   
    <tr>  
      <td width="100%" height="22" class="font" colspan="2"> 
        <hr> 
      </td>   
    </tr>  
    <tr>  
      <td width="100%" height="5" class="font" colspan="2">  
 
 
 
<p>在Windows中Edit,StaticBox的背景色都可以通过处理WM_CTLCOLOR消息来改变,但Push  
 
Button却不行。</p> 
 
 
 
<p>唯一的方法是使用OwnerDraw风格的按钮。本文讲述的方法是使用CButton的派生类。</p> 
 
 
 
<p>class CCButton : public CButton<br> 
 
{<br> 
 
DECLARE_DYNAMIC(CCButton)<br> 
 
public:<br> 
 
&nbsp;&nbsp;&nbsp; CCButton(); <br>

&nbsp;&nbsp;&nbsp; virtual ~CCButton(); <br> 
 
&nbsp;&nbsp;&nbsp; BOOL CCButton::Attach(const UINT nID, CWnd* pParent)<br> 
 
<br> 
 
protected:<br> 
 
&nbsp;&nbsp;&nbsp; virtual void DrawItem(LPDRAWITEMSTRUCT lpDIS);//必需重载的函数<br> 
 
<br> 
 
public:<br> 
 
&nbsp;&nbsp;&nbsp; COLORREF m_fg, m_bg, m_disabled_fg, m_disabled_bg;//四种颜色分别为文字,背景,失效时文字,失效时背景<br> 
 
};<br> 
 
      </p> 
 
 
 
<p>实现DrawItem</p> 
 
 
 
<p>void CCButton::DrawItem(LPDRAWITEMSTRUCT lpDIS)<br> 
 
{<br> 
 
&nbsp;&nbsp;&nbsp; CDC* pDC = CDC::FromHandle(lpDIS-&gt;hDC);//???????DC<br> 
 
<br> 
 
&nbsp;&nbsp;&nbsp; UINT state = lpDIS-&gt;itemState; //得到状态<br> 
 
&nbsp;&nbsp;&nbsp; CRect focusRect, btnRect;//两个矩形,表示得当输入焦点时的虚线矩形和按钮矩形  
 
&nbsp;&nbsp;&nbsp; <br> 
 
&nbsp;&nbsp;&nbsp; focusRect.CopyRect(&amp;lpDIS-&gt;rcItem); <br>

&nbsp;&nbsp;&nbsp; btnRect.CopyRect(&amp;lpDIS-&gt;rcItem); <br>

<br>

&nbsp;&nbsp;&nbsp; //<br>

&nbsp;&nbsp;&nbsp; //调整虚线矩形<br>

&nbsp;&nbsp;&nbsp; //<br>

&nbsp;&nbsp;&nbsp; focusRect.left += 4;<br> 
 
&nbsp;&nbsp; focusRect.right -= 4;<br> 
 
&nbsp;&nbsp; focusRect.top += 4;<br> 
 
&nbsp;&nbsp; focusRect.bottom -= 4;<br> 
 
<br> 
 
&nbsp;&nbsp;&nbsp; //<br>

&nbsp;&nbsp;&nbsp; // 得当Button上文字<br> 
 
&nbsp;&nbsp;&nbsp; //<br>

&nbsp;&nbsp; const int bufSize = 512;<br> 
 
&nbsp;&nbsp; TCHAR buffer[bufSize];<br> 
 
&nbsp;&nbsp; GetWindowText(buffer, bufSize);<br> 
 
&nbsp;&nbsp;&nbsp; <br> 
 
&nbsp;&nbsp; // 使用m_fg, m_bg颜色利用 Draw3dRect(...)绘制按钮边框</p> 
 
 
 
<p>&nbsp;&nbsp; // FillRect(...)填充按钮内部</p> 
 
 
 
<p>&nbsp;&nbsp; // DrawText(...)绘制文字<br> 
 
<br> 
 
&nbsp;&nbsp;&nbsp; //根据当前状态调整显示<br>

&nbsp;&nbsp;&nbsp; //<br>

&nbsp;&nbsp;&nbsp; if (state &amp; ODS_FOCUS) {<br> 
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .........//得到输入焦点,通过focusRect画虚线<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (state &amp; ODS_SELECTED){ <br> 
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .....// 被按下,绘制下陷边框<br> 
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>

&nbsp;&nbsp;&nbsp; }<br>

&nbsp;&nbsp;&nbsp; else if (state &amp; ODS_DISABLED) {<br> 
 
&nbsp;&nbsp;&nbsp; //失效,通过m_disabled_fg, m_disabled_bg 重绘按钮内部<br> 
 
&nbsp;&nbsp;&nbsp; }<br>

} <br>

</p>



<p>CCButton是CButton派生类,具有CButton的全部成员函数,但在创建时需要使用BS_OWNERDRAW风格。</p>



<p>如果按钮不是动态生成,使用Attach函数使CCButton代替原来按钮的窗口过程。</p>



<p>BOOL CCButton::Attach(const UINT nID, CWnd* pParent)<br> 
 
{<br> 
 
&nbsp;&nbsp;&nbsp; GetDlgItem(nID)-&gt;ModifyStyle(0,BS_OWNERDRAW,0);</p>



<p>&nbsp;&nbsp;&nbsp; if (!SubclassDlgItem(nID, pParent))<br> 
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return FALSE;<br> 
 
<br> 
 
&nbsp;&nbsp;&nbsp; return TRUE;<br> 
 
} </p> 
 
 
 
<p>如在一对话框的InitDialog(...)中加入下面几行</p> 
 
 
 
<p>{// 假定 m_cbBtn为成员变量 IDC_BUTTON1为一按钮ID值</p> 
 
 
 
<p>&nbsp;&nbsp; m_cbBtn.Attach(IDC_BUTTON1,this);</p>



<p>}

</p>

      </td>    
    </tr>   
    <tr>
      <td width="100%" height="12" class="font" colspan="2"> 
      </td>    
    </tr>
    <tr>
      <td width="100%" height="6" class="font" colspan="2"> 
      </td>    
    </tr>
    <tr>
      <td width="100%" height="8" class="font" colspan="2"> 
      </td>    
    </tr>
    <tr>   
      <td width="100%" height="17" class="font" colspan="2"></td>    
    </tr>   
  </table>    
</div>    
    
</body>    
    
</html>    

⌨️ 快捷键说明

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