随笔-145  评论-173  文章-70  trackbacks-0
实现目标:在状态栏中,使得一个矩形的字体不断的滚动显示,矩形区域字体不断右移变化,然后达到目的地后又从左边开始显示。

代码:
在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'new position, width, and height.


posted on 2010-02-21 19:15 deercoder 阅读(427) 评论(1)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理