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

📄 vctip7.html

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

⌨️ 快捷键说明

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