📄 mainfrm.cpp
字号:
// MainFrm.cpp : implementation of the CMainFrame class
//
#include "stdafx.h"
#include "mbpc.h"
#include "mbpcDoc.h"
#include "MainFrm.h"
//#include "mbpcView.h"
#include "DeviceInfoView.h"
#include "AnalogInfoView.h"
#include "AnaValueView1.h"
#include "EnergyValView.h"
#include "SetupView.h"
#include "PswDlg.h"
#include "DOStatusView.h"
#include "SettingView.h"
#include "KEYFuntionView.h"
#include "RecordInfoView.h"
#include "SoeView.h"
//#include "FrameView.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_COMMAND_RANGE(ID_DEVICEINFO, ID_SOE, OnTest)
ON_UPDATE_COMMAND_UI_RANGE(ID_DEVICEINFO, ID_SOE, OnUpdateTestUI)
//{{AFX_MSG_MAP(CMainFrame)
ON_WM_CREATE()
ON_WM_DESTROY()
ON_WM_TIMER()
ON_UPDATE_COMMAND_UI(ID_INDICATOR_STA, OnUpdateIndicatorSta)
//}}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_STA,
};
/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction
CMainFrame::CMainFrame()
{
// TODO: add member initialization code here
m_nTimerFlag = 0;
memset(this->m_View,0,sizeof(m_View));
}
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_wndStatusBar.SetFont(1,"MS Sans Serif", 10);
m_wndStatusBar.SetFont(0,"MS Sans Serif", 10);
m_wndStatusBar.SetText(1, " 通信建立中...");
m_wndStatusBar.SetBkColor(1,RGB(255,255,0));
COLORREF col = ::GetSysColor(COLOR_3DFACE);
int x = GetRValue(col);
int y = GetGValue(col);
int z = GetBValue(col);
col = RGB(x,y,z);
m_wndStatusBar.SetBkColor(0, col,RGB( 236, 233, 216));
m_View[0].id = ID_DEVICEINFO;
m_View[1].id = ID_ANALOGINFO;
m_View[2].id = ID_ANAVALUE;
m_View[3].id = ID_ENERGYVAL;
m_View[4].id = ID_SETUP;
m_View[5].id = ID_DOSTATUS;
m_View[6].id = ID_SETTINGSEC;
m_View[7].id = ID_KEYFUN;
m_View[8].id = ID_RECORD;
m_View[9].id = ID_SOE;
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::OnDestroy()
{
CFrameWnd::OnDestroy();
// TODO: Add your message handler code here
this->KillTimer(1);
}
BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
// TODO: Add your specialized code here and/or call the base class
if(!m_tabView.CreateStatic(this))
{
TRACE0("Failed to CreatePropertySheet\n");
return FALSE;
}
// CRuntimeClass* pNewViewClass = RUNTIME_CLASS(CDeviceInfoView);
CRuntimeClass* pNewViewClass = RUNTIME_CLASS(CSoeView);
m_tabView.SetTab(9);
CSize size(160, 180);
if (!m_tabView.CreateView(pNewViewClass, size, pContext))
{
TRACE0("Failed to create first pane\n");
return FALSE;
}
SetActiveView(m_tabView.GetActiveView());
return TRUE;
return CFrameWnd::OnCreateClient(lpcs, pContext);
}
void CMainFrame::OnTest(UINT nCmdID)
{
if (nCmdID == m_nCurrentExample)
return; // already selected
CMbpcDoc* m_pDoc = (CMbpcDoc*)GetActiveView()->GetDocument();
// Set the child window ID of the active view to AFX_IDW_PANE_FIRST.
// This is necessary so that CFrameWnd::RecalcLayout will allocate
// this "first pane" to that portion of the frame window's client
// area not allocated to control bars. Set the child ID of
// the previously active view to some other ID; we will use the
// command ID as the child ID.
CView* pOldActiveView = GetActiveView();
::SetWindowLong(pOldActiveView->m_hWnd, GWL_ID, m_nCurrentExample);
if(m_nCurrentExample == ID_SETTINGSEC)
{
CSettingView *pSettingView = (CSettingView*)pOldActiveView;
if(pSettingView->m_bSaved)
{
}
else
{
int rc = MessageBox("你已更改定值但是未保存,是否保存?",NULL, MB_YESNOCANCEL);
switch(rc)
{
case IDYES:
{
pSettingView->Chgset();
break;
}
case IDNO:
{
break;
}
case IDCANCEL:
{
m_tabView.SetTab(6);
return;
break;
}
}
}
}
CRuntimeClass* pNewViewClass;
switch (nCmdID)
{
case ID_DEVICEINFO:
pNewViewClass = RUNTIME_CLASS(CDeviceInfoView);
break;
case ID_ANALOGINFO:
pNewViewClass = RUNTIME_CLASS(CAnalogInfoView);
break;
case ID_ANAVALUE:
pNewViewClass = RUNTIME_CLASS(CAnaValueView1);
break;
case ID_ENERGYVAL:
pNewViewClass = RUNTIME_CLASS(CEnergyValView);
break;
case ID_SETUP:
{
CPswDlg dlg;
/*if(dlg.DoModal() == IDOK)
{
if(m_pDoc->m_bSetuped)
{
if(m_pDoc->m_bLogIn)
pNewViewClass = RUNTIME_CLASS(CSetupView);
else if(m_pDoc->LogIn(dlg.m_nPswType, dlg.m_nPsw))
{
pNewViewClass = RUNTIME_CLASS(CSetupView);
}
else
{
AfxMessageBox("登陆失败");
pNewViewClass = RUNTIME_CLASS(CDeviceInfoView);
}
}
else
{
AfxMessageBox("连接未建立");
pNewViewClass = RUNTIME_CLASS(CDeviceInfoView);
}
}
else
pNewViewClass = RUNTIME_CLASS(CDeviceInfoView);*/
if(m_pDoc->m_bSetuped)
{
if(m_pDoc->m_bLogIn)
{
pNewViewClass = RUNTIME_CLASS(CSetupView);
}
else
{
CPswDlg dlg;
if(dlg.DoModal() == IDOK)
{
if(m_pDoc->LogIn(dlg.m_nPswType, dlg.m_nPsw))
{
pNewViewClass = RUNTIME_CLASS(CSetupView);
}
else
{
AfxMessageBox("登陆失败");
pNewViewClass = RUNTIME_CLASS(CDeviceInfoView);
m_tabView.SetTab(0);
nCmdID = ID_DEVICEINFO;
}
}
else
{
pNewViewClass = RUNTIME_CLASS(CDeviceInfoView);
m_tabView.SetTab(0);
nCmdID = ID_DEVICEINFO;
}
}
}
else
{
AfxMessageBox("连接未建立");
pNewViewClass = RUNTIME_CLASS(CDeviceInfoView);
m_tabView.SetTab(0);
nCmdID = ID_DEVICEINFO;
}
break;
}
case ID_DOSTATUS:
pNewViewClass = RUNTIME_CLASS(CDOStatusView);
break;
case ID_SETTINGSEC:
pNewViewClass = RUNTIME_CLASS(CSettingView);
break;
case ID_KEYFUN:
pNewViewClass = RUNTIME_CLASS(CKEYFuntionView);
break;
case ID_RECORD:
pNewViewClass = RUNTIME_CLASS(CRecordInfoView);
break;
case ID_SOE:
pNewViewClass = RUNTIME_CLASS(CSoeView);
break;
default:
ASSERT(0);
return;
}
// create the new view
CCreateContext context;
context.m_pNewViewClass = pNewViewClass;
context.m_pCurrentDoc = GetActiveDocument();
context.m_pCurrentFrame = this;
context.m_pLastView = GetActiveView();
POSITION posOfTemplete = AfxGetApp()->m_pDocManager->GetFirstDocTemplatePosition();
context.m_pNewDocTemplate = AfxGetApp()->m_pDocManager->GetNextDocTemplate(posOfTemplete);
// New Code below
int position;
for(int i = 0; i < 10; i++)
{
if(nCmdID == m_View[i].id)
{
position = i;
i = 10;
}
}
CView* pNewView;
if(m_View[position].pView == NULL)
{
pNewView = m_tabView.CreateView(pNewViewClass, CSize(100,100), &context);
m_View[position].pView = pNewView;
if (pNewView != NULL)
{
// the new view is there, but invisible and not active...
pNewView->ShowWindow(SW_SHOW);
pNewView->OnInitialUpdate();
SetActiveView(pNewView);
m_tabView.RecalcLayout(); //<-- New Code
RecalcLayout();
m_nCurrentExample = nCmdID;
// finally destroy the old view...
//pOldActiveView->DestroyWindow();
if(pOldActiveView != m_View[position].pView)
{
pOldActiveView->ShowWindow(SW_HIDE);
}
}
}
else
{
m_View[position].pView->ShowWindow(SW_SHOW);
SetActiveView(m_View[position].pView);
m_tabView.RecalcLayout(); //<-- New Code
RecalcLayout();
m_nCurrentExample = nCmdID;
if(pOldActiveView != m_View[position].pView)
{
pOldActiveView->ShowWindow(SW_HIDE);
}
}
}
void CMainFrame::OnUpdateTestUI(CCmdUI *pCmdUI)
{
pCmdUI->SetCheck(pCmdUI->m_nID == m_nCurrentExample);
}
void CMainFrame::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
CMbpcDoc *pDoc = (CMbpcDoc*)GetActiveDocument();
CString str;
if(1 == nIDEvent)
{
m_nTimerFlag++;
if(m_nTimerFlag > 1)
{
m_nTimerFlag --;
}
else
{
if(pDoc->m_pComThread != NULL)
{
pDoc->m_pComThread->m_nTimeCntr++;
if(pDoc->m_pComThread->m_nTimeCntr >= 5)
{
str.Format("通信超时");
SetMessageText(str);
m_wndStatusBar.SetText(1," 通信超时");
m_wndStatusBar.SetBkColor(1,RGB(255,0,0));
//m_wndStatusBar.EnablePane(1, FALSE);
}
else
{
if(pDoc->m_nMsgtype == 0)
{
str.Format("通信建立中...");
SetMessageText(str);
m_wndStatusBar.SetText(1," 通信建立中...");
m_wndStatusBar.SetBkColor(1,RGB(255,255,0));
}
else
{
str.Format("通信正常");
SetMessageText(str);
m_wndStatusBar.SetText(1," 通信正常");
m_wndStatusBar.SetBkColor(1, RGB(0,255,0));
}
}
}
else
{
SetMessageText("未连接");
m_wndStatusBar.SetText(1,"未连接");
m_wndStatusBar.SetBkColor(1, RGB(236, 233, 216));
}
}
m_nTimerFlag--;
} CFrameWnd::OnTimer(nIDEvent);
}
void CMainFrame::OnUpdateIndicatorSta(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
}
void CMainFrame::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{ // storing code
}
else
{ // loading code
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -