界面编程学习-在状态栏上滚动文字

找到状态栏的特定位置创建一个静态框:

CWnd 
*  pWnd  =  CWnd::FromHandle(hWnd);  //  取主窗口
CStatusBar  *  pStatusBar  =  (CStatusBar * )pWnd -> GetDescendantWindow(AFX_IDW_STATUS_BAR);
CStatusBarCtrl 
*  pStatusCtr  =   & pStatusBar -> GetStatusBarCtrl();
// int pParts[2];
// int iParts = pStatusCtr->GetParts(2, pParts);
CRect rect;
// pStatusCtr->GetRect(iParts-1, &rect);
pStatusCtr -> GetClientRect(rect);
rect.DeflateRect(
760 2 15 2 );
hWnd 
=  ::CreateWindow( _T( " static " ), _T( "" ), WS_CHILD | WS_VISIBLE | SS_LEFT | SS_SUNKEN,
                      rect.left, rect.top, rect.Width(), rect.Height(), pStatusCtr
-> GetSafeHwnd(), (HMENU)(UINT_PTR) 65535 0 0 );
m_sStatic.SubclassWindow(hWnd);
m_sStatic.SetFont(pStatusCtr
-> GetFont());

/对静态框重写:

class  CScrollStatic :  public  CStatic
{
public:
    CScrollStatic()
        : m_iExtend(
0)
        , m_strText(_T(
""))
    
{
    }
;
    
~CScrollStatic(){};

protected:
    DECLARE_MESSAGE_MAP()
public:
    afx_msg 
void OnTimer(UINT nIDEvent);
    afx_msg 
void OnPaint();
    
int m_iExtend;
    CString m_strText;
}
;

BEGIN_MESSAGE_MAP(CScrollStatic, CStatic)
    ON_WM_TIMER()
    ON_WM_PAINT()
END_MESSAGE_MAP()

// void CALLBACK EXPORT TimerProc(
//                                HWND hWnd,       //  handle of CWnd that called SetTimer
//                                UINT nMsg,       //  WM_TIMER
//                                UINT nIDEvent,    //  timer identification
//                                DWORD dwTime     //  system time
//                                )
// {
//     CScrollStatic * pWnd = (CScrollStatic*)CWnd::FromHandle(hWnd);
//     if (pWnd == NULL) return;
//     
//     CString strText;  
//     pWnd->GetWindowText(strText);  
//     if  (strText == pWnd->m_strText)  
//     {  
//         CDC* pDC = pWnd->GetDC();  
//         pDC->SelectObject(pWnd->GetFont());  
//         CSize size = pDC->GetTextExtent(strText);  
//         pWnd->ReleaseDC(pDC);  
//         CRect rect;  
//         pWnd->GetClientRect(rect);  
//         int iWidth = rect.Width();  
//         if (size.cx > iWidth)  
//         {                          
//             pWnd->Invalidate();  
//             pWnd->UpdateWindow();  
//             pWnd->m_iExtend += 2;  
//             if (pWnd->m_iExtend > size.cx)  
//                 pWnd->m_iExtend -= size.cx + 8;  
//         }  
//     }  
//     else  
//     {  
//         pWnd->m_iExtend = 0;  
//         pWnd->m_strText = strText;  
//     }  
// }

void  CScrollStatic::OnTimer(UINT nIDEvent)
{
    CString strText;  
    GetWindowText(strText);  
    
if  (strText == m_strText)  
    
{  
        CDC
* pDC = GetDC();  
        pDC
->SelectObject(GetFont());  
        CSize size 
= pDC->GetTextExtent(strText);  
        ReleaseDC(pDC);  
        CRect rect;  
        GetClientRect(rect);  
        
int iWidth = rect.Width();  
        
if (size.cx > iWidth)  
        
{                          
            Invalidate();  
            UpdateWindow();  
            m_iExtend 
+= 2;  
            
if (m_iExtend > size.cx)  
                m_iExtend 
-= size.cx + 8;  
        }
  
    }
  
    
else  
    
{  
        m_iExtend 
= 0;  
        m_strText 
= strText;  
    }
  

    CStatic::OnTimer(nIDEvent);
}


void  CScrollStatic::OnPaint()
{
    CPaintDC dc(
this); // device context for painting
    
// TODO: 在此处添加消息处理程序代码
    CRect rc;  
    GetClientRect(rc);  
    CString strText;  
    GetWindowText(strText);  
    CDC  memDC;  
    memDC.CreateCompatibleDC(
&dc);  
    CBitmap  bmp;  
    bmp.CreateCompatibleBitmap(
&dc, rc.Width(), rc.Height());  
    memDC.SelectObject(
&bmp);  
    memDC.SelectObject(GetFont());  
    dc.SelectObject(GetFont());  
    memDC.FillSolidRect(rc, ::GetSysColor(COLOR_BTNFACE));  
    CSize size 
= memDC.GetTextExtent(strText);  
    
if (size.cx > rc.Width())  
    
{  
        
if  (size.cx - m_iExtend > 0)  
            memDC.TextOut(rc.left 
- m_iExtend, rc.top + (rc.Height() - size.cy)/2, strText);  
        
if  (rc.left - m_iExtend + 8 + size.cx < rc.right)  
            memDC.TextOut(rc.left 
- m_iExtend + 8 + size.cx, rc.top + (rc.Height() - size.cy)/2, strText);  
    }
  
    
else  
        memDC.TextOut(rc.left, rc.top 
+ (rc.Height() - size.cy)/2, strText);  

    dc.BitBlt(rc.left, rc.top, rc.Width(), rc.Height(), 
&memDC, rc.left, rc.top, SRCCOPY);  
    memDC.DeleteDC();  
    bmp.DeleteObject();  
}

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值