?? visual c++編程技巧之七.htm
字號:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0038)http://www.vchome.net/tech/vctip7.html -->
<HTML><HEAD><TITLE>Visual C++編程技巧之七</TITLE>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<META content="MSHTML 5.50.4134.100" name=GENERATOR>
<META content=sss name=doccomm>
<META content=C:\MSOffice\Template\Sl_BOOK.dot name=Template>
<META content="Cheng Jinxing" name=Author></HEAD>
<BODY text=#000000 vLink=#551a8b aLink=#ff0000 link=#0000ee bgColor=#ccffff>
<P align=center><BR><FONT face=楷體_GB2312 size=+3>Visual C++編程技巧之七</FONT> </P>
<P><A href="http://www.vchome.net/tech/vctip7.html#tip49"><FONT
face="Times New Roman" size=+0>49</FONT><FONT size=+0>、如何實現(xiàn)一個橡皮區(qū)矩形</FONT></A>
</P>
<P><A href="http://www.vchome.net/tech/vctip7.html#tip50"><FONT
face="Times New Roman" size=+0>50</FONT><FONT size=+0>、如何更新翻轉背景顏色的文本</FONT></A>
</P>
<P><A href="http://www.vchome.net/tech/vctip7.html#tip51"><FONT
face="Times New Roman" size=+0>51</FONT><FONT
size=+0>、如何創(chuàng)建一個具有特定點大小的字體</FONT></A> </P>
<P><A href="http://www.vchome.net/tech/vctip7.html#tip52"><FONT
face="Times New Roman" size=+0>52</FONT><FONT size=+0>、如何計算一個串的大小</FONT></A>
</P>
<P><A href="http://www.vchome.net/tech/vctip7.html#tip53"><FONT
face="Times New Roman" size=+0>53</FONT><FONT size=+0>、如何顯示旋轉文本</FONT></A> </P>
<P><FONT face=宋體><A
href="http://www.vchome.net/tech/vctip7.html#tip54">54、如何正確顯示包含標簽字符的串</A></FONT>
</P>
<P><A href="http://www.vchome.net/tech/vctip7.html#tip55"><FONT
face="Times New Roman" size=+0>55</FONT><FONT
size=+0>、串太長時如何在其末尾顯示一個省略號</FONT></A> </P>
<P><A href="http://www.vchome.net/tech/vctip7.html#tip56"><FONT
face="Times New Roman" size=+0>56</FONT><FONT size=+0>、如何快速地格式化一個</FONT><FONT
face="Times New Roman" size=+0>CString</FONT><FONT size=+0>對象</FONT></A> </P>
<P><FONT size=+2><B> </B></FONT> </P>
<P><A name=tip49></A><B><FONT size=+2><FONT
face="Times New Roman">49</FONT><FONT face=黑體>、如何實現(xiàn)一個橡皮區(qū)矩形</FONT></FONT></B>
</P>
<P><FONT size=+0><FONT
face="Times New Roman">CRectTracker</FONT>是一個很有用的類,可以通過調用<FONT
face="Times New Roman">CRectTracker:: TrackRubberBand</FONT>響應<FONT
face="Times New Roman">WM_LBUTTONDOWN</FONT>消息來創(chuàng)建一個橡皮區(qū)矩形。下例表明使用<FONT
face="Times New Roman">CRectTracker</FONT>移動和重置視窗中的藍色橢圓的大小是很容易的事情。</FONT> </P>
<P><FONT size=+0>首先,在文件檔中聲明一個<FONT
face="Times New Roman">CRectTracker</FONT>數據成員:</FONT> </P>
<P><FONT face="Times New Roman" size=+0>class CSampleView : Public CView</FONT>
</P>
<P><FONT face="Times New Roman" size=+0>{</FONT> </P>
<P><FONT size=+0>…</FONT> </P>
<P><FONT face="Times New Roman" size=+0>public :</FONT> </P>
<P><FONT face="Times New Roman" size=+0>CrectTracker m_tracker;</FONT> </P>
<P><FONT size=+0>…</FONT> </P>
<P><FONT face="Times New Roman" size=+0>};</FONT> </P>
<P><FONT size=+0>其次,在文檔類的構造函數中初始化<FONT face="Times New Roman">CRectTracker
</FONT>對象:</FONT> </P>
<P><FONT face="Times New Roman" size=+0>CSampleDoc:: CSampleDOC ()</FONT> </P>
<P><FONT face="Times New Roman" size=+0>{</FONT> </P>
<P><FONT face="Times New Roman" size=+0>//Initialize tracker position, size and
style.</FONT> </P>
<P><FONT face="Times New Roman" size=+0>m_tracker.m_rect.SetRect (0, 0, 10,
10);</FONT> </P>
<P><FONT face="Times New Roman" size=+0>m_tracker.m_nStyle=CRectTracker::
resizeInside |</FONT> </P>
<P><FONT face="Times New Roman" size=+0>CRectTracker:: dottedLine;</FONT> </P>
<P><FONT face="Times New Roman" size=+0>}</FONT> </P>
<P><FONT size=+0>然后,在<FONT
face="Times New Roman">OnDraw</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>CSampleDoc* 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>//Select blue brush into device
context.</FONT> </P>
<P><FONT face="Times New Roman" size=+0>CBrush brush (RGB (0, 0, 255));</FONT>
</P>
<P><FONT face="Times New Roman" size=+0>CBrush* pOldBrush=pDC->SelectObject
(&brush);</FONT> </P>
<P><FONT face="Times New Roman" size=+0>//draw ellipse in tracking
rectangle.</FONT> </P>
<P><FONT face="Times New Roman" size=+0>Crect rcEllipse;</FONT> </P>
<P><FONT face="Times New Roman" size=+0>pDoc->m_tracker.GetTrueRect
(rcEllipse);</FONT> </P>
<P><FONT face="Times New Roman" size=+0>pDC->Ellipse (rcEllipse);</FONT> </P>
<P><FONT face="Times New Roman" size=+0>//Draw tracking rectangle.</FONT> </P>
<P><FONT face="Times New Roman" size=+0>pDoc->m_tracker.Draw (pDC);</FONT>
</P>
<P><FONT face="Times New Roman" size=+0>//Select blue brush out of device
context.</FONT> </P>
<P><FONT face="Times New Roman" size=+0>pDC->Selectobject (pOldBrush);</FONT>
</P>
<P><FONT face="Times New Roman" size=+0>}</FONT> </P>
<P><FONT size=+0>最后,使用<FONT face="Times New Roman">ClassWizard</FONT>處理<FONT
face="Times New Roman">WM_LBUTTONDOWN</FONT>消息,并增加下述代碼。該段代碼根據鼠標擊鍵情況可以拖放、移動或者重置橢圓的大小。</FONT>
</P>
<P><FONT face="Times New Roman" size=+0>void CSampleView::OnLButtonDown (UINT
nFlags, CPoint point)</FONT> </P>
<P><FONT face="Times New Roman" size=+0>{</FONT> </P>
<P><FONT face="Times New Roman" size=+0>//Get pointer to document.</FONT> </P>
<P><FONT face="Times New Roman" size=+0>CSampleDoc* 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>//If clicked on ellipse, drag or resize
it. Otherwise create a</FONT> </P>
<P><FONT face="Times New Roman" size=+0>//rubber-band rectangle nd create a new
ellipse.</FONT> </P>
<P><FONT face="Times New Roman" size=+0>BOOL bResult=pDoc->m_tracker.HitTest
(point)!=</FONT> </P>
<P><FONT face="Times New Roman" size=+0>CRectTracker::hitNothing;</FONT> </P>
<P><FONT face="Times New Roman" size=+0>//Tracker rectangle changed so update
views.</FONT> </P>
<P><FONT face="Times New Roman" size=+0>if (bResult)</FONT> </P>
<P><FONT face="Times New Roman" size=+0>{</FONT> </P>
<P><FONT face="Times New Roman" size=+0>pDoc->m_tracker.Track
(this,point,TRue);</FONT> </P>
<P><FONT face="Times New Roman" size=+0>pDoc->SetModifiedFlag ();</FONT> </P>
<P><FONT face="Times New Roman" size=+0>pDoc->UpdateAllViews (NULL);</FONT>
</P>
<P><FONT face="Times New Roman" size=+0>}</FONT> </P>
<P><FONT face="Times New Roman" size=+0>else</FONT> </P>
<P><FONT face="Times New Roman" size=+0>pDoc->m-tracker.TrackRubberBand
(this,point,TRUE);</FONT> </P>
<P><FONT face="Times New Roman" size=+0>CView:: onLButtonDown
(nFlags,point);</FONT> </P>
<P><FONT face="Times New Roman" size=+0>}</FONT> </P>
<P><A name=tip50></A><B><FONT size=+2><FONT
face="Times New Roman">50</FONT><FONT face=黑體>、如何更新翻轉背景顏色的文本</FONT></FONT></B>
</P>
<P><FONT size=+0>調用<FONT face="Times New Roman">CDC:: SetBkmode</FONT>并傳送<FONT
face="Times New Roman">OPAQUE</FONT>用當前的背景顏色填充背景,或者調用<FONT
face="Times New Roman">CDC::SetBkMode</FONT>并傳送<FONT
face="Times New Roman">TRANSPAARENT</FONT>使背景保持不變,這兩種方法都可以設置背景模式。下例設置背景模式為<FONT
face="Times New Roman">TRANSPARENT</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>//Determint size of view.</FONT> </P>
<P><FONT face="Times New Roman" size=+0>CRect rcView;</FONT> </P>
<P><FONT face="Times New Roman" size=+0>GetClientRect (rcVieew);</FONT> </P>
<P><FONT face="Times New Roman" size=+0>//Create sample string to
display.</FONT> </P>
<P><FONT size=+0><FONT face="Times New Roman">CString str (_T ("Awesome Shadow
Text</FONT>...<FONT face="Times New Roman">"));</FONT></FONT> </P>
<P><FONT face="Times New Roman" size=+0>//Set the background mode to
transparent.</FONT> </P>
<P><FONT face="Times New Roman" size=+0>pDC->SetBKMode (TRANSPARENT);</FONT>
</P>
<P><FONT face="Times New Roman" size=+0>//Draw black shadow text.</FONT> </P>
<P><FONT face="Times New Roman" size=+0>rcView.OffsetRect (1, 1);</FONT> </P>
<P><FONT face="Times New Roman" size=+0>pDc->SetTextColor (RGB (0, 0,
0));</FONT> </P>
<P><FONT face="Times New Roman" size=+0>pDC->DrawText (str, str.GetLength (),
rcView,</FONT> </P>
<P><FONT face="Times New Roman" size=+0>DT_SINGLELINE | DT_CENTER |
DT_VCENTER);</FONT> </P>
<P><FONT face="Times New Roman" size=+0>//Draw red text.</FONT> </P>
<P><FONT face="Times New Roman" size=+0>rcView.OffsetRect (-1,-1);</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>pDC->DrawText (str, str.GetLength (),
rcView,</FONT> </P>
<P><FONT face="Times New Roman" size=+0>DT_SINGLELINE | DT_CENTER |
DT_VCENTER);</FONT> </P>
<P><FONT face="Times New Roman" size=+0>}</FONT> </P>
<P><A name=tip51></A><B><FONT size=+2><FONT
face="Times New Roman">51</FONT><FONT
face=黑體>、如何創(chuàng)建一個具有特定點大小的字體</FONT></FONT></B> </P>
<P><FONT size=+0>可以指定字體邏輯單位的大小,但有時指定字體的點的大小可能會更方便一些。可以如下將字體的點轉換為字體的高度:</FONT>
</P>
<P><FONT face="Times New Roman" size=+0>int nHeigth=mulDiv (nPointSize,
-dc.GetDeviceCaps (LOGPIXELSY), 72);</FONT> </P>
<P><FONT size=+0>下例創(chuàng)建了一個<FONT face="Times New Roman">8</FONT>點的<FONT
face="Times New Roman">Apial</FONT>字體:</FONT> </P>
<P><FONT face="Times New Roman" size=+0>CClientDC dc (AqfxGetMainWnd ());</FONT>
</P>
<P><FONT face="Times New Roman" size=+0>m_font. CreateFont (MulDiv (8,
-dc.GetDeviceCaps (LOGPIXELSY),</FONT> </P>
<P><FONT face="Times New Roman"
size=+0>72),0,0,0,FW_NORMAL,0,0,0,ANSI_CHARSET,</FONT> </P>
<P><FONT face="Times New Roman"
size=+0>OUT_STROKE_PRECIS,CLIP_STROKE_PRECIS,DRAFT_QUALITY,</FONT> </P>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -