實現目標:在狀態欄中,使得一個矩形的字體不斷的滾動顯示,矩形區域字體不斷右移變化,然后達到目的地后又從左邊開始顯示。
代碼:
在CMainFrame的OnCreate函數中添加如下代碼,其中,IDS_MOVE是滾動顯示的內容,屬于一個String Table資源。
????CString?str?=?"Hello,world";
????CString?str2?=?"";

????CClientDC?dc(this);
????CSize?sz?=?dc.GetTextExtent(str);

????m_wndStatusBar.SetPaneInfo(1,IDS_MOVE,SBPS_NORMAL,sz.cx?*?5);
????m_wndStatusBar.SetPaneText(1,str2);
????SetTimer(1,200,NULL);

????CRect?rect;
????m_wndStatusBar.GetItemRect(1,&rect);
????m_str.Create(str,WS_CHILD|WS_VISIBLE,rect,&m_wndStatusBar,126);


然后添加WM_TIMER的響應函數,如下:
void?CMainFrame::OnTimer(UINT?nIDEvent)?


{
????//?TODO:?Add?your?message?handler?code?here?and/or?call?default
????CRect?rect;
????CString?str?=?"Hello,world!";
????CClientDC?dc(this);
????CSize?sz?=?dc.GetTextExtent(str);

????m_wndStatusBar.GetItemRect(1,&rect);?//得到顯示的矩形區域
????static?CRect?CurRect?=?rect;
????CurRect.right?=?CurRect.left?+?sz.cx;????//當前的矩形區域就是一個滑動變化的區域

????if(CurRect.right>=rect.right)
????????CurRect?=?rect;

????else
{
????????CurRect.left?+=?10;
????????CurRect.right?+=?10;
????}

????m_str.MoveWindow(CurRect);????//移動控件的位置

????CFrameWnd::OnTimer(nIDEvent);
}

于是出現了所需要的效果。
注:兩個遇到的API函數:
CStatusBar::GetItemRect
void?GetItemRect(?int?nIndex,?LPRECT?lpRect?)?const;

Parameters

nIndex

Index?of?the?indicator?whose?rectangle?coordinates?are?to?be?retrieved.

lpRect

Points?to?aRECT?structure?or?a?CRect?object?that?will?receive?the?coordinates?of?the?indicator?specified?by?nIndex.

Remarks

Copies?the?coordinates?of?the?indicator?specified?by?nIndex?into?the?structure?pointed?to?by?lpRect.?Coordinates?are?in?pixels?relative?to?the?upper-left?corner?of?the?status?bar.


BOOL?MoveWindow(?LPCRECT?lpRect,?BOOL?bRepaint?=?TRUE?);

SeeMoveWindow?in?the?Win32?SDK.

Remarks

Changes?the?window's?size?and?position.?The?second?version?of?this?method?uses?aRECT?structure?to?determine?the?window's?new?position,?width,?and?height.


posted on 2010-02-21 19:15
deercoder 閱讀(440)
評論(1) 編輯 收藏 引用