📄 mainfrm.cpp
字号:
// MainFrm.cpp : implementation of the CMainFrame class
//
#include "stdafx.h"
#include "ProgressBar.h"
#include "MainFrm.h"
#include <afxpriv.h> // for WM_SETMESSAGESTRING message
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMainFrame
IMPLEMENT_DYNAMIC(CMainFrame, CMDIFrameWnd)
BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
//{{AFX_MSG_MAP(CMainFrame)
ON_WM_CREATE()
ON_COMMAND(ID_VIEW_SHAPES_TOOLBAR, OnViewShapesToolbar)
ON_UPDATE_COMMAND_UI(ID_VIEW_SHAPES_TOOLBAR, OnUpdateViewShapesToolbar)
ON_COMMAND(ID_DEMO_LONG_TASK_1, OnDemoLongTask1)
ON_COMMAND(ID_DEMO_LONG_TASK_2, OnDemoLongTask2)
ON_COMMAND(ID_DEMO_LONG_TASK_3, OnDemoLongTask3)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
static UINT indicators[] =
{
ID_SEPARATOR, // status line indicator
ID_INDICATOR_PROGRESS,
ID_INDICATOR_CAPS,
ID_INDICATOR_NUM,
ID_INDICATOR_SCRL,
};
///////////////////////////////////////////////////////////
// EkIsBarVisible
BOOL EkIsBarVisible( CControlBar* pBar )
{
ASSERT_VALID( pBar );
return ( ( pBar->GetStyle() & WS_VISIBLE ) != 0 );
}
///////////////////////////////////////////////////////////
// EkDockBarNextTo
void EkDockBarNextTo( CControlBar* pNewBar,
CControlBar* pDockedBar,
UINT nDockBarID = AFX_IDW_DOCKBAR_TOP )
{
ASSERT_VALID( pDockedBar );
ASSERT_VALID( pNewBar );
// 1 - Find the frame where we will dock
CFrameWnd* pFrame = pDockedBar->GetDockingFrame();
// 2 - Force MFC to compute the positions
// of the docked control bar(s)
pFrame->RecalcLayout();
// 3 - Compute rectangle of already "docked" bar
CRect rect;
pDockedBar->GetWindowRect( &rect );
// 4 - Offset the rectangle slightly to the bottom right
// so that the new bar will dock either to the right or
// to the bottom of the existing bar (depending on the
// side where this last bar is already docked)
rect.OffsetRect(1,1);
// 5 - Dock new bar to specified position
pFrame->DockControlBar( pNewBar, nDockBarID, &rect );
}
/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction
CMainFrame::CMainFrame()
{
// TODO: add member initialization code here
}
CMainFrame::~CMainFrame()
{
}
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
if (!m_wndToolBar.Create(this) ||
!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
{
TRACE0("Failed to create standard toolbar\n");
return -1; // fail to create
}
if (!m_wndTBshapes.Create(this, WS_CHILD | WS_VISIBLE | CBRS_TOP, IDR_SHAPES) ||
!m_wndTBshapes.LoadToolBar(IDR_SHAPES))
{
TRACE0("Failed to create shapes toolbar\n");
return -1; // fail to create
}
if (!m_wndTBdemo.Create(this, WS_CHILD | WS_VISIBLE | CBRS_TOP, IDR_DEMO_TOOLBAR) ||
!m_wndTBdemo.LoadToolBar(IDR_DEMO_TOOLBAR))
{
TRACE0("Failed to create demo 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
}
// Mainframe docking
EnableDocking(CBRS_ALIGN_ANY);
// Standard toolbar docking
m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle() |
CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndToolBar);
// Shapes toolbar docking
m_wndTBshapes.SetBarStyle(m_wndTBshapes.GetBarStyle() |
CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);
m_wndTBshapes.EnableDocking(CBRS_ALIGN_ANY);
EkDockBarNextTo( &m_wndTBshapes, &m_wndToolBar, AFX_IDW_DOCKBAR_TOP );
m_wndTBshapes.SetWindowText( CString( LPCSTR( IDR_SHAPES ) ) );
// Demo toolbar docking
m_wndTBdemo.SetBarStyle(m_wndTBdemo.GetBarStyle() |
CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);
m_wndTBdemo.EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndTBdemo, AFX_IDW_DOCKBAR_LEFT);
return 0;
}
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CMDIFrameWnd::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics
#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
CMDIFrameWnd::AssertValid();
}
void CMainFrame::Dump(CDumpContext& dc) const
{
CMDIFrameWnd::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers
void CMainFrame::OnViewShapesToolbar()
{
ShowControlBar( &m_wndTBshapes, !EkIsBarVisible( &m_wndTBshapes ), FALSE );
}
void CMainFrame::OnUpdateViewShapesToolbar(CCmdUI* pCmdUI)
{
pCmdUI->SetCheck( EkIsBarVisible( &m_wndTBshapes ) );
}
///////////////////////////////////////////////////////////
// EkCreateProgressBar
BOOL EkCreateProgressBar( CProgressCtrl* pProgressCtrl,
CStatusBar* pStatusBar,
LPCTSTR szMessage = NULL,
int nPaneIndex = 0,
int cxMargin = 10,
int cxMaxWidth = -1,
UINT nIDControl = 1 )
{
ASSERT_VALID( pProgressCtrl );
ASSERT_VALID( pStatusBar );
// 1 - Calculate destination rectangle for progress control
CRect rc;
pStatusBar->GetItemRect( nPaneIndex, &rc );
// 2 - Define progress bar horizontal offset
if( szMessage != NULL )
{
// Compute message text extent
CClientDC dc( pStatusBar );
CFont* pFont = pStatusBar->GetFont();
CFont* pOldFont = dc.SelectObject( pFont );
CSize sizeText = dc.GetTextExtent( szMessage );
dc.SelectObject( pOldFont );
rc.left += sizeText.cx + cxMargin;
}
// 3 - Compute progress bar width
if( cxMaxWidth != -1 )
{
rc.right = rc.left + min( cxMaxWidth, rc.Width() );
}
// 4 - Display message text
pStatusBar->SetPaneText( nPaneIndex, szMessage );
pStatusBar->RedrawWindow();
// 5 - Create progress control
return pProgressCtrl->Create( WS_CHILD | WS_VISIBLE,
rc, pStatusBar, nIDControl );
}
void CMainFrame::OnDemoLongTask1()
{
// 1 - Create "progress bar"
// (default options)
CProgressCtrl wndProgress;
EkCreateProgressBar( &wndProgress, &m_wndStatusBar,
_T( "Executing a long task (1), press Esc to cancel..." ) );
// 2 - Initialize progress control range and step size
wndProgress.SetRange( 0, 100 );
wndProgress.SetStep( 1 );
// 3 - Here is where you would perform your long task and regularly
// call wndProgress.StepIt() to update the progress control.
// For this demonstration, we simulate the long task
// with a for loop and the Sleep() function.
srand( (unsigned)time( NULL ) ); // seed random number generator
for( int i = 0; i < 100; ++i )
{
// 4 - Test for "Esc" key
if( ::GetAsyncKeyState( VK_ESCAPE ) < 0 )
{
break;
}
int nSleepTime = MulDiv( 100, rand(), RAND_MAX );
Sleep( nSleepTime );
// 5 - Update progress control
wndProgress.StepIt();
}
// 6 - Ensures that idle message is displayed again
PostMessage( WM_SETMESSAGESTRING, (WPARAM) AFX_IDS_IDLEMESSAGE, 0L );
}
void CMainFrame::OnDemoLongTask2()
{
// 1 - Create "progress bar"
// (limit progress bar width to 200 pixels)
CProgressCtrl wndProgress;
EkCreateProgressBar( &wndProgress, &m_wndStatusBar,
_T( "Executing a long task (2), press Esc to cancel..." ),
0, 10, 200 );
// 2 - Initialize progress control range and step size
wndProgress.SetRange( 0, 100 );
wndProgress.SetStep( 1 );
// 3 - Here is where you would perform your long task and regularly
// call wndProgress.StepIt() to update the progress control.
// For this demonstration, we simulate the long task
// with a for loop and the Sleep() function.
srand( (unsigned)time( NULL ) ); // seed random number generator
for( int i = 0; i < 100; ++i )
{
// 4 - Test for "Esc" key
if( ::GetAsyncKeyState( VK_ESCAPE ) < 0 )
{
break;
}
int nSleepTime = MulDiv( 100, rand(), RAND_MAX );
Sleep( nSleepTime );
// 5 - Update progress control
wndProgress.StepIt();
}
// 6 - Ensures that idle message is displayed again
PostMessage( WM_SETMESSAGESTRING, (WPARAM) AFX_IDS_IDLEMESSAGE, 0L );
}
void CMainFrame::OnDemoLongTask3()
{
// 1 - Create "progress bar"
// (in pane 1, without message text)
CProgressCtrl wndProgress;
EkCreateProgressBar( &wndProgress, &m_wndStatusBar,
NULL, 1 );
// 2 - Initialize progress control range and step size
wndProgress.SetRange( 0, 100 );
wndProgress.SetStep( 1 );
// 3 - Here is where you would perform your long task and regularly
// call wndProgress.StepIt() to update the progress control.
// For this demonstration, we simulate the long task
// with a for loop and the Sleep() function.
m_wndStatusBar.SetWindowText( _T( "Executing a long task (3), press Esc to cancel..." ) );
m_wndStatusBar.RedrawWindow();
srand( (unsigned)time( NULL ) ); // seed random number generator
for( int i = 0; i < 100; ++i )
{
// 4 - Test for "Esc" key
if( ::GetAsyncKeyState( VK_ESCAPE ) < 0 )
{
break;
}
int nSleepTime = MulDiv( 100, rand(), RAND_MAX );
Sleep( nSleepTime );
// 5 - Update progress control
wndProgress.StepIt();
}
// 6 - Ensures that idle message is displayed again
PostMessage( WM_SETMESSAGESTRING, (WPARAM) AFX_IDS_IDLEMESSAGE, 0L );
// 7 - Restore indicator string
m_wndStatusBar.SetPaneText( 1, CString( (LPCSTR) ID_INDICATOR_PROGRESS ) );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -