?? vctip7.html
字號:
<p><font SIZE="+0" FACE="Times New Roman">VARIABLE_PITCH | FF-SWISS,_T ("Arial"));</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">函數(shù)<font FACE="Times New Roman">CDC:: Det text Extent </font>根據(jù)當前選擇的字體計算一個串的高度和寬度。如果使用的不是系統(tǒng)字體而是其他字體,則在調(diào)用<fontFACE="Times New Roman">GetTextExtent</font>之前將字體選進設備上下文中是很重要的,否則計算高度和寬度時將依據(jù)系統(tǒng)字體,由此得出的結(jié)果當然是不正確的。下述樣板程序當改變下壓按鈕的標題時動態(tài)調(diào)整按鈕的大小,按鈕的大小由按鈕的字體和標題的大小而定。響應消息<fontFACE="Times New Roman">WM_SETTEXT</font>時調(diào)用<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 () ->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->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->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->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="黑體">、如何顯示旋轉(zhuǎn)文本</font></font></b> </p><p><font SIZE="+0">只要用戶使用<font FACE="Times New Roman">TrueType</font>或者<fontFACE="Times New Roman">GDI</font>筆或字體就可以顯示旋轉(zhuǎn)文本<fontFACE="Times New Roman">(</font>有些硬件設備也支持旋轉(zhuǎn)光柵字體)。<fontFACE="Times New Roman">LOGFONT</font>結(jié)構(gòu)中的<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>表示字體旋轉(zhuǎn)<fontFACE="Times New Roman">45</font>度。為確保所有的字體沿坐標系統(tǒng)的同一方向旋轉(zhuǎn),一定要設置<fontFACE="Times New Roman">ifEscapement</font>成員的<font FACE="Times New Roman">CLIP_LH_ANGLES</font>位,否則,有些字體可能反向旋轉(zhuǎn)。下例使用了<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 ("Wheeee</font>...<fontFACE="Times New Roman">I am rotating!"));</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->SetBkMode (TRANSPARENT);</font> </p><p><font SIZE="+0" FACE="Times New Roman">pDC->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 (&stFont, 0, sizeof (LOGFONT));</font> </p><p><font SIZE="+0" FACE="Times New Roman">stFont.ifheight=MulDiv (14, -pDC->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, "Arial");</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<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 (&stfont);</font> </p><p><font SIZE="+0" FACE="Times New Roman">CFont* pOldFont=pDC->SelectObject (&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->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">調(diào)用<font FACE="Times New Roman">GDI</font>文本繪畫函數(shù)時需要展開標簽字符,這可以通過調(diào)用<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>函數(shù)允許指定標簽位的數(shù)組,下例指定每<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 ("Cathy\tNorman\tOliver"));</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->TabbedtextOut (10, 10, str, 1, &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">調(diào)用<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->Drawtext (CString ("This is a long string"),</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->DrawText (AfxgetApp () ->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">調(diào)用<font FACE="Times New Roman">CString:: Format</font>,該函數(shù)和<fontFACE="Times New Roman">printf</font>函數(shù)具有相同的參數(shù),下例說明了如何使用<fontFACE="Times New Roman">Format</font>函數(shù):</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 ("Window Size (%d, %d)"),</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> <a HREF="skill.htm">返回上頁</a> </p></body></html>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -