⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainfrm.cpp

📁 Visual_C++[1].NET_Bible1 Visual_C++宝典书中的全部源码
💻 CPP
字号:
// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "BarsDemo.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)
	//{{AFX_MSG_MAP(CMainFrame)
	ON_WM_CREATE()
	ON_COMMAND(ID_COLOR_RED, OnColorRed)
	ON_UPDATE_COMMAND_UI(ID_COLOR_RED, OnUpdateColorRed)
	ON_COMMAND(ID_COLOR_BLUE, OnColorBlue)
	ON_UPDATE_COMMAND_UI(ID_COLOR_BLUE, OnUpdateColorBlue)
	ON_COMMAND(ID_COLOR_GREEN, OnColorGreen)
	ON_UPDATE_COMMAND_UI(ID_COLOR_GREEN, OnUpdateColorGreen)
	ON_COMMAND(ID_OPERATION_LINE, OnOperationLine)
	ON_COMMAND(ID_OPERATION_RECTANGLE, OnOperationRectangle)
	ON_COMMAND(ID_OPERATION_ELLIPSE, OnOperationEllipse)
	ON_COMMAND(ID_EDIT_CLEAR, OnEditClear)
	ON_COMMAND(ID_EDIT_UNDO, OnEditUndo)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,
	ID_SEPARATOR,
	ID_SEPARATOR,
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{

	m_nCurrentDrawEvent = 0;
	m_cColor = RGB( 255, 0, 0 );
	m_nOperation = OPERATION_LINE;

}

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
	}

	m_wndStatusBar.SetPaneInfo( 1, ID_SEPARATOR,
		SBPS_NORMAL, 150 );
	m_wndStatusBar.SetPaneInfo( 2, ID_SEPARATOR,
		SBPS_NORMAL, 150);

	// Set the initial color pane to show the
	// red is selected.
	m_wndStatusBar.SetPaneText( 1,
		"Red: RGB( 255, 0, 0 )", TRUE );
	// Call the function to update the
	// status bar with the event count.
	ShowCount();

	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;
	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::OnColorRed() 
{

	// Set the color variable to red and
	// show the color selection in the
	// status bar.
	m_cColor = RGB( 255, 0, 0 );
	m_wndStatusBar.SetPaneText( 1,
		"Red: RGB( 255, 0, 0 )", TRUE );

}

void CMainFrame::OnUpdateColorRed(CCmdUI* pCmdUI) 
{

	// Show that red is selected.
	pCmdUI->SetCheck( m_cColor == RGB( 255, 0, 0 ) );

}

void CMainFrame::OnColorBlue() 
{

	// Set the color variable to blue and
	// show the color selection in the
	// status bar.
	m_cColor = RGB( 0, 0, 255 );
	m_wndStatusBar.SetPaneText( 1,
		"Blue: RGB( 0, 0, 255 )", TRUE );

}

void CMainFrame::OnUpdateColorBlue(CCmdUI* pCmdUI) 
{

	// Show that blue is selected.
	pCmdUI->SetCheck( m_cColor == RGB( 0, 0, 255 ) );

}

void CMainFrame::OnColorGreen() 
{

	// Set the color variable to green and
	// show the color selection in the
	// status bar.
	m_cColor = RGB( 0, 255, 0 );
	m_wndStatusBar.SetPaneText( 1,
		"Green: RGB( 0, 255, 0 )", TRUE );

}

void CMainFrame::OnUpdateColorGreen(CCmdUI* pCmdUI) 
{

	// Show that green is selected.
	pCmdUI->SetCheck( m_cColor == RGB( 0, 255, 0 ) );

}

void CMainFrame::OnOperationLine() 
{

	// Make sure we didn't exceed the limits
	// of the structure array.
	if( m_nCurrentDrawEvent < MAX_DRAW_EVENTS - 1 ){

		// Store the color.
		m_DrawEvent[m_nCurrentDrawEvent].cColor =
			m_cColor;

		// Store the operation.
		m_DrawEvent[m_nCurrentDrawEvent].nOperation =
			OPERATION_LINE;

		// Generate and store the x1, y1, x2, y2
		// coordinates.
		m_DrawEvent[m_nCurrentDrawEvent].nX1 =
			rand() & 0x7f;
		m_DrawEvent[m_nCurrentDrawEvent].nY1 =
			rand() & 0x7f;
		m_DrawEvent[m_nCurrentDrawEvent].nX2 =
			m_DrawEvent[m_nCurrentDrawEvent].nX1 +
			20 + ( rand() & 0x7f );
		m_DrawEvent[m_nCurrentDrawEvent].nY2 =
			m_DrawEvent[m_nCurrentDrawEvent].nY1 +
			20 + ( rand() & 0x7f );

		// Increment the counter.
		m_nCurrentDrawEvent++;

		// Cause a redraw in the view class.
		CView *pView = GetActiveView();
		pView->InvalidateRect( NULL, FALSE );
		pView->UpdateWindow();

		// Call the function to update the
		// status bar with the event count.
		ShowCount();

		}

}

void CMainFrame::OnOperationRectangle() 
{

	// Make sure we didn't exceed the limits
	// of the structure array.
	if( m_nCurrentDrawEvent < MAX_DRAW_EVENTS - 1 ){

		// Store the color.
		m_DrawEvent[m_nCurrentDrawEvent].cColor =
			m_cColor;

		// Store the operation.
		m_DrawEvent[m_nCurrentDrawEvent].nOperation =
			OPERATION_RECTANGLE;

		// Generate and store the x1, y1, x2, y2
		// coordinates.
		m_DrawEvent[m_nCurrentDrawEvent].nX1 =
			rand() & 0x7f;
		m_DrawEvent[m_nCurrentDrawEvent].nY1 =
			rand() & 0x7f;
		m_DrawEvent[m_nCurrentDrawEvent].nX2 =
			m_DrawEvent[m_nCurrentDrawEvent].nX1 +
			20 + ( rand() & 0x7f );
		m_DrawEvent[m_nCurrentDrawEvent].nY2 =
			m_DrawEvent[m_nCurrentDrawEvent].nY1 +
			20 + ( rand() & 0x7f );

		// Increment the counter.
		m_nCurrentDrawEvent++;

		// Cause a redraw in the view class.
		CView *pView = GetActiveView();
		pView->InvalidateRect( NULL, FALSE );
		pView->UpdateWindow();

		// Call the function to update the
		// status bar with the event count.
		ShowCount();

		}

}

void CMainFrame::OnOperationEllipse() 
{

	// Make sure we didn't exceed the limits
	// of the structure array.
	if( m_nCurrentDrawEvent < MAX_DRAW_EVENTS - 1 ){

		// Store the color.
		m_DrawEvent[m_nCurrentDrawEvent].cColor =
			m_cColor;

		// Store the operation.
		m_DrawEvent[m_nCurrentDrawEvent].nOperation =
			OPERATION_ELLIPSE;

		// Generate and store the x1, y1, x2, y2
		// coordinates.
		m_DrawEvent[m_nCurrentDrawEvent].nX1 =
			rand() & 0xff;
		m_DrawEvent[m_nCurrentDrawEvent].nY1 =
			rand() & 0xff;
		m_DrawEvent[m_nCurrentDrawEvent].nX2 =
			m_DrawEvent[m_nCurrentDrawEvent].nX1 +
			30 + ( rand() & 0xff );
		m_DrawEvent[m_nCurrentDrawEvent].nY2 =
			m_DrawEvent[m_nCurrentDrawEvent].nY1 +
			30 + ( rand() & 0xff );

		// Increment the counter.
		m_nCurrentDrawEvent++;

		// Cause a redraw in the view class.
		CView *pView = GetActiveView();
		pView->InvalidateRect( NULL, FALSE );
		pView->UpdateWindow();

		// Call the function to update the
		// status bar with the event count.
		ShowCount();

		}

}

void CMainFrame::OnEditClear() 
{

	if( m_nCurrentDrawEvent > 0 ){

		// Set the counter to zero.
		m_nCurrentDrawEvent = 0;

		// Cause a redraw in the view class.
		CView *pView = GetActiveView();
		pView->InvalidateRect( NULL, TRUE );
		pView->UpdateWindow();

		// Call the function to update the
		// status bar with the event count.
		ShowCount();

		}

}

void CMainFrame::OnEditUndo() 
{

	if( m_nCurrentDrawEvent > 0 ){

		// Decrement the counter;
		m_nCurrentDrawEvent--;

		// Cause a redraw in the view class.
		CView *pView = GetActiveView();
		pView->InvalidateRect( NULL, TRUE );
		pView->UpdateWindow();

		// Call the function to update the
		// status bar with the event count.
		ShowCount();

		}

}

void CMainFrame::ShowCount( void )
{

	// Format a string so that it shows
	// the current count and the max count.
	CString strCount;
	strCount.Format( "Count:%d of %d",
		m_nCurrentDrawEvent, MAX_DRAW_EVENTS );

	// Set the status bar pane to show the count.
	m_wndStatusBar.SetPaneText( 2, strCount, TRUE );

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -