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

📄 mainfrm.cpp

📁 VC编写的一个WinSocket及串口通信的例程代码
💻 CPP
字号:
// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "ExBars.h"

#include "MainFrm.h"
#include "Timer.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_WM_CLOSE()
	ON_COMMAND(ID_SHOWPOPUP, OnShowpopup)
	ON_UPDATE_COMMAND_UI(ID_SHOWPOPUP, OnUpdateShowpopup)
	ON_COMMAND(ID_SHOWMYTOOLBAR, OnShowmytoolbar)
	ON_UPDATE_COMMAND_UI(ID_SHOWMYTOOLBAR, OnUpdateShowmytoolbar)
	//}}AFX_MSG_MAP
	ON_UPDATE_COMMAND_UI(ID_INDICATOR_TIMER, OnShowTimer)
	ON_CBN_SELCHANGE(IDR_COMBO, OnComboChange)
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_TIMER,
	//ID_INDICATOR_CAPS,
	//ID_INDICATOR_NUM,
	//ID_INDICATOR_SCRL,
};

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

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
	m_PopupVisible = TRUE;
}

CMainFrame::~CMainFrame()
{
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	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
	}
	
	UINT nID, nStyle;
	int cxWidth;
	m_wndStatusBar.GetPaneInfo(1, nID, nStyle, cxWidth);
	m_wndStatusBar.SetPaneInfo(1, nID, SBPS_POPOUT, cxWidth);

	m_wndStatusBar.SetTimer(2, 900, (TIMERPROC) TimerProc);
	
	
	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_RIGHT
		| 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
	}
	// TODO: Delete these three lines if you don't want the toolbar to
	//  be dockable
	m_wndToolBar.EnableDocking(CBRS_ALIGN_RIGHT);

	if(!m_MyToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP | 
		CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_MyToolBar.LoadToolBar(IDR_MYTOOLBAR))
	{
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create
	}
	m_MyToolBar.SetButtonInfo(0, ID_DUMMY, TBBS_SEPARATOR, 190);
	CRect rect;
	m_MyToolBar.GetItemRect(0, &rect);
	rect.top = 1;
	rect.bottom = rect.top + 100;
	m_MyToolBar.m_ComboBox.Create(CBS_DROPDOWNLIST | WS_VISIBLE | WS_TABSTOP, rect,
		&m_MyToolBar, IDR_COMBO);
	CString lName;
	if(lName.LoadString(IDC_SHOWPOPUP))
		m_MyToolBar.m_ComboBox.AddString((LPCTSTR) lName);
	if(lName.LoadString(IDC_HIDEPOPUP))
		m_MyToolBar.m_ComboBox.AddString((LPCTSTR) lName);
	m_MyToolBar.m_ComboBox.SetCurSel(0);
	m_MyToolBar.EnableDocking(CBRS_ALIGN_TOP);

	
	EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar(&m_wndToolBar);
	DockControlBar(&m_MyToolBar);

	CreatePopupToolBar();
	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::OnShowTimer(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	CTime t = CTime::GetCurrentTime();
	m_wndStatusBar.SetPaneText(m_wndStatusBar.CommandToIndex(ID_INDICATOR_TIMER), t.Format("%P %I:%M:%S"));
}

void CMainFrame::OnClose() 
{
	// TODO: Add your message handler code here and/or call default
	m_wndStatusBar.KillTimer(2);
	CFrameWnd::OnClose();
}

int CMainFrame::CreatePopupToolBar()
{
	if(!m_wndPopupToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP |
		CBRS_SIZE_FIXED | CBRS_TOOLTIPS) || !m_wndPopupToolBar.LoadToolBar(IDR_POPUPTOOLBAR))
	{
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create
	}
	m_wndPopupToolBar.SetWindowText("弹出式工具栏");
	m_wndPopupToolBar.EnableDocking(0);

	int nItem = m_wndPopupToolBar.GetToolBarCtrl().GetButtonCount();
	for(int i = 0; i < nItem; i++)
	{
		UINT nStyle = m_wndPopupToolBar.GetButtonStyle(i);
		if(((i + 1) % 5) == 0)
			nStyle |= TBBS_WRAPPED;
		else
			nStyle &= ~TBBS_WRAPPED;
		m_wndPopupToolBar.SetButtonStyle(i, nStyle);
	}
	Invalidate();
	RecalcLayout();

	FloatControlBar(&m_wndPopupToolBar, CPoint(::GetSystemMetrics(SM_CXSCREEN) / 2,
		::GetSystemMetrics(SM_CYSCREEN) / 2));
	return TRUE;
}

void CMainFrame::OnShowpopup() 
{
	// TODO: Add your command handler code here
	m_PopupVisible = !m_PopupVisible;
	ShowControlBar(&m_wndPopupToolBar, m_PopupVisible, FALSE);
	RecalcLayout();
	m_MyToolBar.m_ComboBox.SetCurSel(m_PopupVisible ? 0 : 1);
}

void CMainFrame::OnUpdateShowpopup(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->Enable();
	BOOL show = ((m_wndPopupToolBar.GetStyle() & WS_VISIBLE) != 0);
	pCmdUI->SetCheck(show);
	if(show != m_PopupVisible)
	{
		m_MyToolBar.m_ComboBox.SetCurSel(show ? 0 : 1);
		m_PopupVisible = show;
	}
}

void CMainFrame::OnComboChange()
{
	int nID = m_MyToolBar.m_ComboBox.GetCurSel();
	if(((nID == 0) && (!m_PopupVisible)) || ((nID == 1) && (m_PopupVisible)))
		OnShowpopup();
}

void CMainFrame::OnShowmytoolbar() 
{
	// TODO: Add your command handler code here
	m_MyToolBar.ShowWindow((m_MyToolBar.GetStyle() & WS_VISIBLE) ? SW_HIDE : SW_SHOW);
	RecalcLayout();
}

void CMainFrame::OnUpdateShowmytoolbar(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_MyToolBar.GetStyle() & WS_VISIBLE);
}

⌨️ 快捷键说明

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