www.gusucode.com > 基于VC编程界面编程高级应用技术源码程序 > VC界面编程高级应用技术/code/3/MyStatusBar/MainFrm.cpp
// MainFrm.cpp : implementation of the CMainFrame class // #include "stdafx.h" #include "MyStatusBar.h" #include "MainFrm.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CMainFrame IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd) BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd) //加入对时间区域进行界面维护的宏申明 ON_UPDATE_COMMAND_UI(ID_INDICATOR_DATE,OnUpdateDate) ON_WM_CREATE() //}}AFX_MSG_MAP END_MESSAGE_MAP() static UINT indicators[] = { ID_SEPARATOR, // status line indicator ID_INDICATOR_CAPS, ID_INDICATOR_NUM, ID_INDICATOR_SCRL, }; ///////////////////////////////////////////////////////////////////////////// // CMainFrame construction/destruction CMainFrame::CMainFrame() { // TODO: add member initialization code here } CMainFrame::~CMainFrame() { } 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); //在状态栏中添加新的区域用以显示时间 //得到目前状态栏共有多少个区域 int nOrigSize = sizeof(indicators) / sizeof(UINT); //构建对话框ID标识数组 UINT* pIndicators = new UINT[nOrigSize + 1]; memcpy(pIndicators, indicators, sizeof(indicators)); IndexOfTimeZone = nOrigSize++; //大状态栏的右侧部份设置成时间区域 pIndicators[IndexOfTimeZone] = ID_INDICATOR_DATE; m_wndStatusBar.SetIndicators(pIndicators, nOrigSize); delete[] pIndicators; } BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs) { if( !CFrameWnd::PreCreateWindow(cs) ) return FALSE; // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs return TRUE; } ///////////////////////////////////////////////////////////////////////////// // CMainFrame diagnostics #ifdef _DEBUG void CMainFrame::AssertValid() const { CFrameWnd::AssertValid(); } void CMainFrame::Dump(CDumpContext& dc) const { CFrameWnd::Dump(dc); } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CMainFrame message handlers void CMainFrame::OnUpdateDate(CCmdUI *pCmdUI) { //得到系统当前时间 CTime time = CTime::GetCurrentTime(); CString strDate = time.Format(_T("%A, %B %d, %y ")); // 根据时间字 //符串的长度计算它在状态栏输出时所需要的长度 CSize size; HGDIOBJ hOldFont = NULL; //得到当前状态栏字体 HFONT hFont = (HFONT)m_wndStatusBar.SendMessage(WM_GETFONT); CClientDC dc(NULL); if (hFont != NULL) hOldFont = dc.SelectObject(hFont); //得到当前文本所需要的显示宽度 size = dc.GetTextExtent(strDate); if (hOldFont != NULL) dc.SelectObject(hOldFont); // 修改状态栏 //设置时间区域的宽度size.cx m_wndStatusBar.SetPaneInfo(IndexOfTimeZone, ID_INDICATOR_DATE,0, size.cx); //设定时间区域的标题 pCmdUI->SetText(strDate); //启用时间区域 pCmdUI->Enable(TRUE); }