﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-智慧掩盖真相-文章分类-MFC学习</title><link>http://www.cppblog.com/birdshover/category/9380.html</link><description /><language>zh-cn</language><lastBuildDate>Thu, 15 Jan 2009 11:59:04 GMT</lastBuildDate><pubDate>Thu, 15 Jan 2009 11:59:04 GMT</pubDate><ttl>60</ttl><item><title>MFC绘制按钮</title><link>http://www.cppblog.com/birdshover/articles/72079.html</link><dc:creator>BirdsHover</dc:creator><author>BirdsHover</author><pubDate>Thu, 15 Jan 2009 04:41:00 GMT</pubDate><guid>http://www.cppblog.com/birdshover/articles/72079.html</guid><wfw:comment>http://www.cppblog.com/birdshover/comments/72079.html</wfw:comment><comments>http://www.cppblog.com/birdshover/articles/72079.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/birdshover/comments/commentRss/72079.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/birdshover/services/trackbacks/72079.html</trackback:ping><description><![CDATA[<p>界面上添加一个按钮，把按钮设置为Owner Draw = true;<br>添加WM_DRAWITEM消息映射，映射函数为OnDrawItem<br></p>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;CmfcAppDlg::OnDrawItem(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;nIDCtl,&nbsp;LPDRAWITEMSTRUCT&nbsp;lpDrawItemStruct)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;TODO:&nbsp;在此添加消息处理程序代码和/或调用默认值</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;CDC&nbsp;ButtonDC;<br>&nbsp;&nbsp;&nbsp;&nbsp;CRect&nbsp;rc;<br>&nbsp;&nbsp;&nbsp;&nbsp;ButtonDC.Attach(lpDrawItemStruct</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">hDC);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">清空绘制</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;ButtonDC.FloodFill(</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,TRANSPARENT);<br>&nbsp;&nbsp;&nbsp;&nbsp;rc&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;lpDrawItemStruct</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">rcItem;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">按钮状态</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;UINT&nbsp;state&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;lpDrawItemStruct</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">itemState;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">绘制一个边框</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;ButtonDC.Draw3dRect(rc,</span><span style="COLOR: #000000">0x0</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">0x100</span><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">背景色透明</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;ButtonDC.SetBkMode(TRANSPARENT);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">当按钮为焦点状态</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(state&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">&nbsp;ODS_FOCUS){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CString&nbsp;str&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;L</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">焦点状态</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ButtonDC.DrawText(str,str.GetLength(),</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">rc,DT_CENTER&nbsp;</span><span style="COLOR: #000000">|</span><span style="COLOR: #000000">&nbsp;DT_VCENTER&nbsp;</span><span style="COLOR: #000000">|</span><span style="COLOR: #000000">&nbsp;DT_SINGLELINE);<br>&nbsp;&nbsp;&nbsp;&nbsp;}</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CString&nbsp;str&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;L</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">失去焦点</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ButtonDC.DrawText(str,str.GetLength(),</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">rc,DT_CENTER&nbsp;</span><span style="COLOR: #000000">|</span><span style="COLOR: #000000">&nbsp;DT_VCENTER&nbsp;</span><span style="COLOR: #000000">|</span><span style="COLOR: #000000">&nbsp;DT_SINGLELINE);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;CDialog::OnDrawItem(nIDCtl,&nbsp;lpDrawItemStruct);<br>}</span></div>
<img src ="http://www.cppblog.com/birdshover/aggbug/72079.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/birdshover/" target="_blank">BirdsHover</a> 2009-01-15 12:41 <a href="http://www.cppblog.com/birdshover/articles/72079.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>