visual c++编程技巧之七.htm
来自「载之“阿蒙编程乐园”」· HTM 代码 · 共 363 行 · 第 1/2 页
HTM
363 行
<P><FONT face="Times New Roman" size=+0>VARIABLE_PITCH | FF-SWISS,_T
("Arial"));</FONT> </P>
<P><A name=tip52></A><B><FONT size=+2><FONT
face="Times New Roman">52</FONT><FONT face=黑体>、如何计算一个串的大小</FONT></FONT></B> </P>
<P><FONT size=+0>函数<FONT face="Times New Roman">CDC:: Det text Extent
</FONT>根据当前选择的字体计算一个串的高度和宽度。如果使用的不是系统字体而是其他字体,则在调用<FONT
face="Times New Roman">GetTextExtent</FONT>之前将字体选进设备上下文中是很重要的,否则计算高度和宽度时将依据系统字体,由此得出的结果当然是不正确的。下述样板程序当改变下压按钮的标题时动态调整按钮的大小,按钮的大小由按钮的字体和标题的大小而定。响应消息<FONT
face="Times New Roman">WM_SETTEXT</FONT>时调用<FONT
face="Times New Roman">OnSetText</FONT>,该消息使用<FONT
face="Times New Roman">ON_MESSAE</FONT>宏指令定义的用户自定义消息。</FONT> </P>
<P><FONT face="Times New Roman" size=+0>LRESULT CMyButton:: OnSettext (WPARAM
wParam, LPARAM lParam)</FONT> </P>
<P><FONT face="Times New Roman" size=+0>{</FONT> </P>
<P><FONT face="Times New Roman" size=+0>//Pass message to window
procedure.</FONT> </P>
<P><FONT face="Times New Roman" size=+0>LRESULT bResult=CallWindowProc
(*GetSuperWndProcAddr (),</FONT> </P>
<P><FONT face="Times New Roman" size=+0>m_hWnd, GetCurrentMessage ()
->message,wParam,lParam);</FONT> </P>
<P><FONT face="Times New Roman" size=+0>//Get title of push button.</FONT> </P>
<P><FONT face="Times New Roman" size=+0>CString strTitle;</FONT> </P>
<P><FONT face="Times New Roman" size=+0>GetWindowText (strTitle);</FONT> </P>
<P><FONT face="Times New Roman" size=+0>//Select current font into device
context.</FONT> </P>
<P><FONT face="Times New Roman" size=+0>CDC* pDC=GetDc ();</FONT> </P>
<P><FONT face="Times New Roman" size=+0>CFont*pFont=GetFont ();</FONT> </P>
<P><FONT face="Times New Roman" size=+0>CFont*pOldFont=pDC->SelectObject
(pFont);</FONT> </P>
<P><FONT face="Times New Roman" size=+0>//Calculate size of title.</FONT> </P>
<P><FONT face="Times New Roman" size=+0>CSize size=pDC->GetTextExent
(strTitle,strTitle.GetLength ());</FONT> </P>
<P><FONT face="Times New Roman" size=+0>//Adjust the button's size based on its
title.</FONT> </P>
<P><FONT face="Times New Roman" size=+0>//Add a 5-pixel border around the
button.</FONT> </P>
<P><FONT face="Times New Roman" size=+0>SetWindowPos (NULL, 0, 0, size.cx+10,
size.cy+10,</FONT> </P>
<P><FONT face="Times New Roman" size=+0>SWP_NOMOVE | SWP_NOZORDER |
SWP_NOACTIVATE);</FONT> </P>
<P><FONT face="Times New Roman" size=+0>//Clean up.</FONT> </P>
<P><FONT face="Times New Roman" size=+0>pDC->SelectFont (pOldFont);</FONT>
</P>
<P><FONT face="Times New Roman" size=+0>ReleaseDC (pDC);</FONT> </P>
<P><FONT face="Times New Roman" size=+0>return bResult;</FONT> </P>
<P><FONT face="Times New Roman" size=+0>}</FONT> </P>
<P><A name=tip53></A><B><FONT size=+2><FONT
face="Times New Roman">53</FONT><FONT face=黑体>、如何显示旋转文本</FONT></FONT></B> </P>
<P><FONT size=+0>只要用户使用<FONT face="Times New Roman">TrueType</FONT>或者<FONT
face="Times New Roman">GDI</FONT>笔或字体就可以显示旋转文本<FONT
face="Times New Roman">(</FONT>有些硬件设备也支持旋转光栅字体)。<FONT
face="Times New Roman">LOGFONT</FONT>结构中的<FONT
face="Times New Roman">ifEscapement</FONT>成员指定了文本行和<FONT
face="Times New Roman">x</FONT>轴的角度,角度的单位是十分之一度而不是度,例如,<FONT
face="Times New Roman">ifEscapement</FONT>为<FONT
face="Times New Roman">450</FONT>表示字体旋转<FONT
face="Times New Roman">45</FONT>度。为确保所有的字体沿坐标系统的同一方向旋转,一定要设置<FONT
face="Times New Roman">ifEscapement</FONT>成员的<FONT
face="Times New Roman">CLIP_LH_ANGLES</FONT>位,否则,有些字体可能反向旋转。下例使用了<FONT
face="Times New Roman">14</FONT>点<FONT
face="Times New Roman">Arial</FONT>字体每间隔<FONT
face="Times New Roman">15</FONT>度画一个串。</FONT> </P>
<P><FONT face="Times New Roman" size=+0>void CSampleView:: OnDraw (CDC*
pDC)</FONT> </P>
<P><FONT face="Times New Roman" size=+0>{</FONT> </P>
<P><FONT face="Times New Roman" size=+0>//Determine the size of the
window.</FONT> </P>
<P><FONT face="Times New Roman" size=+0>CRect rcClient;</FONT> </P>
<P><FONT face="Times New Roman" size=+0>GetClientRect (rcClient);</FONT> </P>
<P><FONT face="Times New Roman" size=+0>//Create sample string.</FONT> </P>
<P><FONT size=+0><FONT face="Times New Roman">CString str (_T
("Wheeee</FONT>...<FONT face="Times New Roman">I am rotating!"));</FONT></FONT>
</P>
<P><FONT face="Times New Roman" size=+0>//Draw transparent, red text.</FONT>
</P>
<P><FONT face="Times New Roman" size=+0>pDC->SetBkMode (TRANSPARENT);</FONT>
</P>
<P><FONT face="Times New Roman" size=+0>pDC->SetTextColor (RGB
(255,0,0));</FONT> </P>
<P><FONT face="Times New Roman" size=+0>CFont font; //font object</FONT> </P>
<P><FONT face="Times New Roman" size=+0>LOGFONT stFont; //font definition</FONT>
</P>
<P><FONT face="Times New Roman" size=+0>//Set font attributes that will not
change.</FONT> </P>
<P><FONT face="Times New Roman" size=+0>memset (&stFont, 0, sizeof
(LOGFONT));</FONT> </P>
<P><FONT face="Times New Roman" size=+0>stFont.ifheight=MulDiv (14,
-pDC->GetDeviceCaps (LOGPIXELSY), 72);</FONT> </P>
<P><FONT face="Times New Roman" size=+0>stFont.ifWeight=FW_NORMAL;</FONT> </P>
<P><FONT face="Times New Roman"
size=+0>stFont.ifClipPrecision=LCIP_LH_ANGLES;</FONT> </P>
<P><FONT face="Times New Roman" size=+0>strcpy (stFont.lfFaceName,
"Arial");</FONT> </P>
<P><FONT face="Times New Roman" size=+0>//Draw text at 15degree
intervals.</FONT> </P>
<P><FONT face="Times New Roman" size=+0>for (int nAngle=0; nAngle<3600;
nAngle+=150)</FONT> </P>
<P><FONT face="Times New Roman" size=+0>{</FONT> </P>
<P><FONT face="Times New Roman" size=+0>//Specify new angle.</FONT> </P>
<P><FONT face="Times New Roman" size=+0>stFont.lfEscapement=nAngle;</FONT> </P>
<P><FONT face="Times New Roman" size=+0>//Create and select font into dc.</FONT>
</P>
<P><FONT face="Times New Roman" size=+0>font.CreateFontIndirect
(&stfont);</FONT> </P>
<P><FONT face="Times New Roman" size=+0>CFont* pOldFont=pDC->SelectObject
(&font);</FONT> </P>
<P><FONT face="Times New Roman" size=+0>//Draw the text.</FONT> </P>
<P><FONT face="Times New Roman" size=+0>pDC->SelectObject (pOldFont);</FONT>
</P>
<P><FONT face="Times New Roman" size=+0>font.DelectObjext ();</FONT> </P>
<P><FONT face="Times New Roman" size=+0>}</FONT> </P>
<P><FONT face="Times New Roman" size=+0>}</FONT> </P>
<P><A name=tip54></A><FONT face=黑体 size=+2>54、如何正确显示包含标签字符的串</FONT> </P>
<P><FONT size=+0>调用<FONT
face="Times New Roman">GDI</FONT>文本绘画函数时需要展开标签字符,这可以通过调用<FONT
face="Times New Roman">CDC:: TabbedTextOut</FONT>或者<FONT
face="Times New Roman">CDC:: DrawText</FONT>并指定<FONT
face="Times New Roman">DT_EXPANDTABS</FONT>标志来完成。<FONT
face="Times New Roman">TabbedTextOut</FONT>函数允许指定标签位的数组,下例指定每<FONT
face="Times New Roman">20</FONT>设备单位展开一个标签:</FONT> </P>
<P><FONT face="Times New Roman" size=+0>void CSampleView:: OnDraw (CDC*
pDC)</FONT> </P>
<P><FONT face="Times New Roman" size=+0>{</FONT> </P>
<P><FONT face="Times New Roman" size=+0>CTestDoc* pDoc=GetDocument ();</FONT>
</P>
<P><FONT face="Times New Roman" size=+0>ASSERT_VALID (pDoC);</FONT> </P>
<P><FONT face="Times New Roman" size=+0>CString str;</FONT> </P>
<P><FONT face="Times New Roman" size=+0>str.Format (_T
("Cathy\tNorman\tOliver"));</FONT> </P>
<P><FONT face="Times New Roman" size=+0>int nTabStop=20; //tabs are every 20
pixels</FONT> </P>
<P><FONT face="Times New Roman" size=+0>pDC->TabbedtextOut (10, 10, str, 1,
&nTabStop, 10);</FONT> </P>
<P><FONT face="Times New Roman" size=+0>}</FONT> </P>
<P><A name=tip55></A><B><FONT size=+2><FONT
face="Times New Roman">55</FONT><FONT
face=黑体>、串太长时如何在其末尾显示一个省略号</FONT></FONT></B> </P>
<P><FONT size=+0>调用<FONT face="Times New Roman">CDC:: DrawText</FONT>并指定<FONT
face="Times New Roman">DT_END_ELLIPSIS</FONT>标志,这样就可以用小略号取代串末尾的字符使其适合于指定的边界矩形。如果要显示路径信息,指定<FONT
face="Times New Roman">DT_END_ELLIPSIS</FONT>标志并省略号取代串中间的字符。</FONT> </P>
<P><FONT face="Times New Roman" size=+0>void CSampleView:: OnDraw (CDC*
pDC)</FONT> </P>
<P><FONT face="Times New Roman" size=+0>{</FONT> </P>
<P><FONT face="Times New Roman" size=+0>CTestDoc* pDoc=GetDocument ();</FONT>
</P>
<P><FONT face="Times New Roman" size=+0>ASSERT_VALID (pDoc);</FONT> </P>
<P><FONT face="Times New Roman" size=+0>//Add ellpsis to end of string if it
does not fit</FONT> </P>
<P><FONT face="Times New Roman" size=+0>pDC->Drawtext (CString ("This is a
long string"),</FONT> </P>
<P><FONT face="Times New Roman" size=+0>CRect (10, 10, 80, 30), DT_LEFT |
DT_END_ELLIPSIS);</FONT> </P>
<P><FONT face="Times New Roman" size=+0>//Add ellpsis to middle of string if it
does not fit</FONT> </P>
<P><FONT face="Times New Roman" size=+0>pDC->DrawText (AfxgetApp ()
->m_pszhelpfilePath,</FONT> </P>
<P><FONT face="Times New Roman" size=+0>CRect (10, 40, 200, 60), DT_LEFT |
DT_PATH_ELLIPSIS);</FONT> </P>
<P><FONT face="Times New Roman" size=+0>}</FONT> </P>
<P><A name=tip56></A><B><FONT size=+2><FONT
face="Times New Roman">56</FONT><FONT face=黑体>、如何快速地格式化一个</FONT><FONT
face="Times New Roman">CString</FONT><FONT face=黑体>对象</FONT></FONT></B> </P>
<P><FONT size=+0>调用<FONT face="Times New Roman">CString::
Format</FONT>,该函数和<FONT
face="Times New Roman">printf</FONT>函数具有相同的参数,下例说明了如何使用<FONT
face="Times New Roman">Format</FONT>函数:</FONT> </P>
<P><FONT face="Times New Roman" size=+0>//Get size of window.</FONT> </P>
<P><FONT face="Times New Roman" size=+0>CRect rcWindow;</FONT> </P>
<P><FONT face="Times New Roman" size=+0>GetWindowRect (rcWindow);</FONT> </P>
<P><FONT face="Times New Roman" size=+0>//Format message string.</FONT> </P>
<P><FONT face="Times New Roman" size=+0>CString strMessage;</FONT> </P>
<P><FONT face="Times New Roman" size=+0>strMessage.Format (_T ("Window Size (%d,
%d)"),</FONT> </P>
<P><FONT face="Times New Roman" size=+0>rcWindow.Width (), rcWindow.Height
());</FONT> </P>
<P><FONT face="Times New Roman" size=+0>//Display the message.</FONT> </P>
<P><FONT face="Times New Roman" size=+0>MessageBox (strmessage);</FONT> </P>
<HR>
<P> <A
href="http://www.vchome.net/tech/skill.htm">返回上页</A> </P></BODY></HTML>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?