Program: Class Name: CDLG - CPP Project Name: Statusbar in Mainfrm.H

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 7

PROGRAM

Class Name: CDlg.cpp


Project Name: StatusBar

In MainFrm.h

// MainFrm.h : interface of the CMainFrame class

#if !
defined(AFX_MAINFRM_H__36FB7EA7_3622_404C_88A1_A4AE22EBB7C5__INCLU
DED_)
#define
AFX_MAINFRM_H__36FB7EA7_3622_404C_88A1_A4AE22EBB7C5__INCLUDED_

#if _MSC_VER > 1000


#pragma once
#endif // _MSC_VER > 1000

class CMainFrame : public CFrameWnd


{

protected: // create from serialization only


CMainFrame();
DECLARE_DYNCREATE(CMainFrame)

// Attributes
public:

// Operations
public:

// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMainFrame)
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
//}}AFX_VIRTUAL

// Implementation
public:
virtual ~CMainFrame();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif

public: // control bar embedded members


CStatusBar m_wndStatusBar;
CToolBar m_wndToolBar;

// Generated message map functions


protected:
//{{AFX_MSG(CMainFrame)
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnUpdateLeft(CCmdUI *pCmdUI);
afx_msg void OnUpdateRight(CCmdUI *pCmdUI);
// NOTE - the ClassWizard will add and remove member functions
here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the
previous line.

#endif // !
defined(AFX_MAINFRM_H__36FB7EA7_3622_404C_88A1_A4AE22EBB7C5__INCLU
DED_)

In MainFrm.cpp

// MainFrm.cpp : implementation of the CMainFrame class

#include "stdafx.h"
#include "StatusBar.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_LEFT,OnUpdateLeft)
ON_UPDATE_COMMAND_UI(ID_INDICATOR_RIGHT,OnUpdateRight)
//{{AFX_MSG_MAP(CMainFrame)
// NOTE - the ClassWizard will add and remove mapping macros
here.
// DO NOT EDIT what you see in these blocks of generated code !
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,
ID_INDICATOR_LEFT,
ID_INDICATOR_RIGHT
};

// 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,WS_CHILD|WS_VISIBLE|
CBRS_BOTTOM,ID_MY_STATUS_BAR) ||
!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);

return 0;
}

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::OnUpdateLeft(CCmdUI *pCmdUI)


{
pCmdUI->Enable(::GetKeyState(VK_LBUTTON)<0);
}

void CMainFrame::OnUpdateRight(CCmdUI *pCmdUI)


{
pCmdUI->Enable(::GetKeyState(VK_RBUTTON)<0);
}

In View.cpp

// StatusBarView.cpp : implementation of the CStatusBarView class

#include "stdafx.h"
#include "StatusBar.h"

#include "StatusBarDoc.h"
#include "StatusBarView.h"
#include "MainFrm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// CStatusBarView

IMPLEMENT_DYNCREATE(CStatusBarView, CView)

BEGIN_MESSAGE_MAP(CStatusBarView, CView)
//{{AFX_MSG_MAP(CStatusBarView)
ON_WM_MOUSEMOVE()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

// CStatusBarView construction/destruction

CStatusBarView::CStatusBarView()
{
}

CStatusBarView::~CStatusBarView()
{
}

BOOL CStatusBarView::PreCreateWindow(CREATESTRUCT& cs)


{
return CView::PreCreateWindow(cs);
}

// CStatusBarView drawing

void CStatusBarView::OnDraw(CDC* pDC)


{
CStatusBarDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
}

// CStatusBarView printing

BOOL CStatusBarView::OnPreparePrinting(CPrintInfo* pInfo)


{
return DoPreparePrinting(pInfo);
}

void CStatusBarView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)


{
}

void CStatusBarView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)


{
}
// CStatusBarView diagnostics

#ifdef _DEBUG
void CStatusBarView::AssertValid() const
{
CView::AssertValid();
}

void CStatusBarView::Dump(CDumpContext& dc) const


{
CView::Dump(dc);
}

CStatusBarDoc* CStatusBarView::GetDocument() // non-debug version is inline


{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CStatusBarDoc)));
return (CStatusBarDoc*)m_pDocument;
}
#endif //_DEBUG

// CStatusBarView message handlers

void CStatusBarView::OnMouseMove(UINT nFlags, CPoint point)


{
CString str;
CMainFrame* pFrame=(CMainFrame *)AfxGetApp()->m_pMainWnd;
CStatusBar* pStatus=&pFrame->m_wndStatusBar;
if(pStatus)
{
str.Format("x=%d,y=%d",point.x,point.y);
pStatus->SetPaneText(0,str);
}
CView::OnMouseMove(nFlags, point);
}
OUTPUT

RESULT:

You might also like