随笔-145  评论-173  文章-70  trackbacks-0
topic1:浮动工具条。
在前面说到浮动工具条,实际上是新建了一个工具栏,可以拖放到菜单中的。而此处是,初始化创建程序的时候,工具栏上面不是固定在菜单下面的,而是在浮动的,最终,如果拖放的话,可以固定在原来的位置上面。代码很简单。
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
        
return -1;
    
    
if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
        
| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
        
!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
    
{
        TRACE0(
"Failed to create toolbar\n");
        
return -1;      // fail to create
    }


    
if (!m_wndStatusBar.Create(this||
        
!m_wndStatusBar.SetIndicators(indicators,
          
sizeof(indicators)/sizeof(UINT)))
    
{
        TRACE0(
"Failed to create status bar\n");
        
return -1;      // fail to create
    }


    
// TODO: Delete these three lines if you don't want the toolbar to
    
//  be dockable
    m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
    EnableDocking(CBRS_ALIGN_ANY);
    DockControlBar(
&m_wndToolBar);
    

    CRect rect;
    GetWindowRect(
&rect);
    CPoint point(rect.left,rect.top
+250);
    FloatControlBar(
&m_wndToolBar,point,CBRS_ALIGN_LEFT);


    
return 0;
}
添加的代码如上,这样,只是改变了初始化的工具栏,用到的是默认的那个m_wndToolBar,自己并没有新建一个。

Topic 2:  调整工具栏按钮的位置。
工具栏上面有很多按钮实现不同的功能,如何调整他们的顺序呢?比如将新建和保存两个按钮的顺序对调,当然,按钮的外观和功能也要对调啊。
通过SetButtonInfo函数来实现。
void CMainFrame::OnChange() 
{
    
// TODO: Add your command handler code here
    UINT firstID,secondID;
    firstID 
= m_wndToolBar.GetItemID(0);
    secondID 
= m_wndToolBar.GetItemID(1);
    m_wndToolBar.SetButtonInfo(
2,firstID,0,0);
    m_wndToolBar.SetButtonInfo(
0,secondID,0,2);
}

此处添加了一个菜单,对应的函数来响应,OK。相关的函数就是那个,不需要多说明。

Topic 3: 状态栏中显示鼠标的位置。
如何在移动鼠标后,马上显示出鼠标的位置呢?
添加消息WM_MOUSEMOVE的响应函数OnMouseMove函数。
void CTest23View::OnMouseMove(UINT nFlags, CPoint point) 
{
    
// TODO: Add your message handler code here and/or call default
    CString str;
    CMainFrame 
*pMainFrame = (CMainFrame *)AfxGetApp()->m_pMainWnd;    //获取主窗口的指针
    CStatusBar *pState = &pMainFrame->m_wndStatusBar;    
    str.Format(
"X=%d,Y=%d",point.x,point.y);    //文本格式化
    pState->SetPaneText(1,str,TRUE);        //更新状态栏第二项内容
    pState->SetPaneText (1"My New Status Bar Text", TRUE);
//    ((CMainFrame*)GetParent())->m_wndStatusBar.SetWindowText(str);
    CView::OnMouseMove(nFlags, point);
}
注意的几点是:1,添加字符串资源IDS_MOUSE到MainFrm.cpp源代码中的indicators数组中。位置在第二个。
                          2.添加响应消息OnMouseMove函数,其中如上代码。
                          3.要添加对于的头文件#include "MainFrm.h"文件。
                          4.将CMainFrame中成员变为public,否则的话访问权限有误!
(一个疑问就是:为何我的状态栏很窄,显示出来的信息就只要前面的X=xx,而例子中的代码却可以使得状态很宽,显示的够多呢?应该是其他方面的设置问题,待弄清!)
修改后的代码如下(使得可以正确的显示出来,长度合适):
void CTest23View::OnMouseMove(UINT nFlags, CPoint point) 
{
    
// TODO: Add your message handler code here and/or call default
    CString str;
    CMainFrame 
*pMainFrame = (CMainFrame *)AfxGetApp()->m_pMainWnd;    //获取主窗口的指针
    CStatusBar *pState = &pMainFrame->m_wndStatusBar;    
    str.Format(
"X=%d,Y=%d",point.x,point.y);    //文本格式化
    CClientDC dc(this);
    CSize sz 
= dc.GetTextExtent(str);    //用这个得到要显示的字符串的长度
    pState->SetPaneInfo(1,IDS_MOUSE,SBPS_NORMAL,sz.cx);
    pState
->SetPaneText(1,str,TRUE);        //更新状态栏第二项内容
//    ((CMainFrame*)GetParent())->m_wndStatusBar.SetWindowText(str);
    CView::OnMouseMove(nFlags, point);
}

上面就是用到了CClientDC的一个函数特征,得到长度和宽度。然后调用SetPaneInfo函数设置宽度。

参考:http://www.cppblog.com/deercoder/archive/2010/02/17/107985.html
posted on 2010-02-17 21:30 deercoder 阅读(1028) 评论(0)  编辑 收藏 引用

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