// 这段 MFC 示例源代码演示如何使用 MFC Microsoft Office Fluent 用户界面
// (“Fluent UI”)。该示例仅供参考，
// 用以补充《Microsoft 基础类参考》和
// MFC C++ 库软件随附的相关电子文档。
// 复制、使用或分发 Fluent UI 的许可条款是单独提供的。
// 若要了解有关 Fluent UI 许可计划的详细信息，请访问
// https://go.microsoft.com/fwlink/?LinkId=238214.
//
// 版权所有(C) Microsoft Corporation
// 保留所有权利。

// MainFrm.cpp: CMainFrame 类的实现
//

#include "stdafx.h"
#include "framework.h"
#include "SATHelper.h"

#include "MainFrm.h"
#include "DlgLogin.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

CMainFrame* g_pMainFrame = NULL;

#if USE_TRAYICON
#define WM_MY_TRAY_NOTIFICATION WM_USER + 108
const UINT WM_TASKBARCREATED = ::RegisterWindowMessage(_T("UB530"));
#endif

// CMainFrame

IMPLEMENT_DYNAMIC(CMainFrame, CMDIFrameWndEx)

BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWndEx)
	ON_WM_CREATE()
	ON_COMMAND(ID_WINDOW_MANAGER, &CMainFrame::OnWindowManager)
	ON_COMMAND_RANGE(ID_VIEW_APPLOOK_WIN_2000, ID_VIEW_APPLOOK_WINDOWS_7, &CMainFrame::OnApplicationLook)
	ON_UPDATE_COMMAND_UI_RANGE(ID_VIEW_APPLOOK_WIN_2000, ID_VIEW_APPLOOK_WINDOWS_7, &CMainFrame::OnUpdateApplicationLook)
	ON_COMMAND(ID_VIEW_CAPTION_BAR, &CMainFrame::OnViewCaptionBar)
	ON_UPDATE_COMMAND_UI(ID_VIEW_CAPTION_BAR, &CMainFrame::OnUpdateViewCaptionBar)
	ON_COMMAND(ID_TOOLS_OPTIONS, &CMainFrame::OnOptions)
	ON_WM_SETTINGCHANGE()
#if USE_TRAYICON
	ON_MESSAGE(WM_MY_TRAY_NOTIFICATION, OnTrayNotification)
	ON_REGISTERED_MESSAGE(WM_TASKBARCREATED, OnTaskBarCreated)
#endif
	ON_COMMAND(ID_TRAYMENU_EXIT, &CMainFrame::OnTraymenuExit)
	ON_MESSAGE(MSG_STATUS_BAR, &CMainFrame::OnMsgStatusBar)
	ON_REGISTERED_MESSAGE(AFX_WM_ON_CHANGE_RIBBON_CATEGORY, &CMainFrame::OnRibbonChanged)
	ON_WM_CLOSE()
	ON_WM_DEVICECHANGE()
	// 全步骤截图;
	ON_COMMAND(ID_CHECK_FULL_CUT, &CMainFrame::OnCheckFullCut)
	ON_UPDATE_COMMAND_UI(ID_CHECK_FULL_CUT, &CMainFrame::OnUpdateCheckFullCut)
	// 有声按钮;
	ON_COMMAND(BTN_HAD_SOUND, &CMainFrame::OnHadSound)
	ON_UPDATE_COMMAND_UI(BTN_HAD_SOUND, &CMainFrame::OnUpdateHadSound)
	// 声音默认按钮;
	ON_COMMAND(BTN_DEF_SOUND, &CMainFrame::OnDefSound)
	ON_UPDATE_COMMAND_UI(BTN_DEF_SOUND, &CMainFrame::OnUpdateDefSound)
	// 声音等级编辑框;
	// OCR账号下拉框;
	ON_COMMAND(ID_COMBOX_OCR, &CMainFrame::OnComboxOcr)
	ON_UPDATE_COMMAND_UI(ID_COMBOX_OCR, &CMainFrame::OnUpdateComboxOcr)
	// MenuTree项目下拉框;
	ON_COMMAND(ID_COMBO_PRODUCT, &CMainFrame::OnComboProduct)
	ON_UPDATE_COMMAND_UI(ID_COMBO_PRODUCT, &CMainFrame::OnUpdateComboProduct)
	// MenuTree UI下拉框;
	ON_COMMAND(ID_COMBO_UI, &CMainFrame::OnComboUi)
	ON_UPDATE_COMMAND_UI(ID_COMBO_UI, &CMainFrame::OnUpdateComboUi)
	// MenuTree 子UI下拉框;
	ON_COMMAND(ID_COMBO_SUBUI, &CMainFrame::OnComboSubUi)
	ON_UPDATE_COMMAND_UI(ID_COMBO_SUBUI, &CMainFrame::OnUpdateComboSubUi)
	// 信号仪1下拉框;
	ON_COMMAND(ID_COMBO1_22293, &CMainFrame::OnCombo1_22293)
	ON_UPDATE_COMMAND_UI(ID_COMBO1_22293, &CMainFrame::OnUpdateCombo1_22293)
	// 信号仪2下拉框;
	ON_COMMAND(ID_COMBO2_22293, &CMainFrame::OnCombo2_22293)
	ON_UPDATE_COMMAND_UI(ID_COMBO2_22293, &CMainFrame::OnUpdateCombo2_22293)
	// 信号仪1CheckBox;
	ON_COMMAND(ID_CHECK1_22293, &CMainFrame::OnCheck1_22293)
	ON_UPDATE_COMMAND_UI(ID_CHECK1_22293, &CMainFrame::OnUpdateCheck1_22293)
	// 信号仪2CheckBox;
	ON_COMMAND(ID_CHECK2_22293, &CMainFrame::OnCheck2_22293)
	ON_UPDATE_COMMAND_UI(ID_CHECK2_22293, &CMainFrame::OnUpdateCheck2_22293)
	//////////////////////////////////////////////////////////////////////////
	ON_COMMAND(ID_COMBO_TVPORT, &CMainFrame::OnCombo2Tv)
	ON_UPDATE_COMMAND_UI(ID_COMBO_TVPORT, &CMainFrame::OnUpdateCombo2Tv)
	ON_COMMAND(ID_CHECK_WATCHTV, &CMainFrame::OnCheckWatchtv)
	ON_UPDATE_COMMAND_UI(ID_CHECK_WATCHTV, &CMainFrame::OnUpdateCheckWatchtv)
	ON_COMMAND(ID_TRAYMENU_LOGIN, &CMainFrame::OnTraymenuLogin)
END_MESSAGE_MAP()

// CMainFrame 构造/析构

CMainFrame::CMainFrame() noexcept :m_trayIcon(IDR_MAINFRAME)
{
	// TODO: 在此添加成员初始化代码
	theApp.m_nAppLook = theApp.GetInt(_T("ApplicationLook"), ID_VIEW_APPLOOK_VS_2008);
#if USE_TRAYICON
	m_isNotify = TRUE;
	m_bNoticeTray = TRUE;
#endif

	m_pDoc = NULL;
	m_pActiveView = NULL;
	for ( int i = 0; i < 10; i++ )
	{
		m_pViewIDE[i] = NULL;
	}

	m_bShotIR = FALSE;
	m_pUB530View = m_pDeviceView = NULL;

	ReadBaiduCfg(m_bdcfg);
	ReadRsRunCfg(m_rscfg);
	ReadTreePath();
}

CMainFrame::~CMainFrame()
{
}


#if USE_TRAYICON
void CMainFrame::InitTrayIcon()
{
	m_trayIcon.SetNotificationWnd(this, WM_MY_TRAY_NOTIFICATION);
	m_trayIcon.SetIcon(IDR_MAINFRAME, _T("UB530"));
	m_trayIcon.SetIconInfos(IDR_MAINFRAME, IDR_MAINFRAME, IDR_MAINFRAME);
	m_trayIcon.SetDefaultTip(_T("UB530"));
}

LRESULT CMainFrame::OnTaskBarCreated(WPARAM wp, LPARAM lp)
{
	Shell_NotifyIcon(NIM_ADD, &m_trayIcon.m_nid);
	return 0;
}

// Handle notification from tray icon: display a message.
LRESULT CMainFrame::OnTrayNotification(WPARAM uID, LPARAM lEvent)
{
	if (m_isNotify)
	{
		static LPCSTR MouseMessages[] = { "WM_MOUSEMOVE",
			"WM_LBUTTONDOWN", "WM_LBUTTONUP", "WM_LBUTTONDBLCLK",
			"WM_RBUTTONDOWN", "WM_RBUTTONUP", "WM_RBUTTONDBLCLK",
			"WM_MBUTTONDOWN", "WM_MBUTTONUP", "WM_MBUTTONDBLCLK" };
	}

	if (uID != IDR_MAINFRAME)
		return m_trayIcon.OnTrayNotification(uID, lEvent);

	switch (lEvent)
	{
	case WM_RBUTTONUP:
		TrayRight();
		break;
	case WM_LBUTTONUP:
		break;
	case WM_LBUTTONDBLCLK:
		TaskNotifyIcon();
		break;
	default:
		break;
	}
	return 0;
}

void CMainFrame::TrayRight()
{
	CMenu menu;
	if (!menu.LoadMenu(IDR_MAINFRAME))
		return;

	CMenu* pSubMenu = menu.GetSubMenu(3);
	if (!pSubMenu)
		return;

	CPoint point;
	GetCursorPos(&point);
	::SetForegroundWindow(m_hWnd);
	pSubMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, this);
}

void CMainFrame::TaskNotifyIcon()
{
	if (!m_bNoticeTray)
	{
		m_trayIcon.SetIcon(IDR_MAINFRAME, _T("UB530采集程序"));
		ShowWindow(SW_SHOWNORMAL);
		m_bNoticeTray = TRUE;
	}
	else
	{
		m_trayIcon.SetIcon(IDR_MAINFRAME, _T("UB530采集程序"));
		ShowWindow(SW_HIDE);
		m_bNoticeTray = FALSE;
	}
}
#endif

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CMDIFrameWndEx::OnCreate(lpCreateStruct) == -1)
		return -1;

	g_pMainFrame = this;
#if USE_TRAYICON
	InitTrayIcon();
#endif

	BOOL bNameValid;

#if __TAB_BAR__
	CMDITabInfo mdiTabParams;
	mdiTabParams.m_style = CMFCTabCtrl::STYLE_3D_ONENOTE; // 其他可用样式...
	mdiTabParams.m_bActiveTabCloseButton = TRUE;      // 设置为 FALSE 会将关闭按钮放置在选项卡区域的右侧
	mdiTabParams.m_bTabIcons = FALSE;    // 设置为 TRUE 将在 MDI 选项卡上启用文档图标
	mdiTabParams.m_bAutoColor = TRUE;    // 设置为 FALSE 将禁用 MDI 选项卡的自动着色
	mdiTabParams.m_bDocumentMenu = TRUE; // 在选项卡区域的右边缘启用文档菜单
	EnableMDITabbedGroups(TRUE, mdiTabParams);
#endif

	m_wndRibbonBar.Create(this);
	m_wndRibbonBar.LoadFromResource(IDR_RIBBON);

#if !__QUICKACCESS_COMMAND__
	// 添加以下代码段,禁用快速启动栏;
	CMFCRibbonQuickAccessToolBar* pQAToolbar = m_wndRibbonBar.GetQuickAccessToolbar();
	pQAToolbar->RemoveAll();
#endif

	if (!m_wndStatusBar.Create(this))
	{
		TRACE0("未能创建状态栏\n");
		return -1;      // 未能创建
	}

	CString strTitlePane1;
	CString strTitlePane2;
	bNameValid = strTitlePane1.LoadString(IDS_STATUS_PANE1);
	ASSERT(bNameValid);
	bNameValid = strTitlePane2.LoadString(IDS_STATUS_PANE2);
	ASSERT(bNameValid);
	m_wndStatusBar.AddElement(new CMFCRibbonStatusBarPane(ID_STATUSBAR_PANE1, strTitlePane1, TRUE), strTitlePane1);
	m_wndStatusBar.AddExtendedElement(new CMFCRibbonStatusBarPane(ID_STATUSBAR_PANE2, strTitlePane2, TRUE), strTitlePane2);

	// 启用 Visual Studio 2005 样式停靠窗口行为
	CDockingManager::SetDockingMode(DT_SMART);
	// 启用 Visual Studio 2005 样式停靠窗口自动隐藏行为
	EnableAutoHidePanes(CBRS_ALIGN_ANY);

#if __OUTLOOKBAR__
	// 导航窗格将创建在左侧，因此将暂时禁用左侧的停靠: 
	EnableDocking(CBRS_ALIGN_TOP | CBRS_ALIGN_BOTTOM | CBRS_ALIGN_RIGHT);

	// 创建并设置“Outlook”导航栏: 
	if (!CreateOutlookBar(m_wndNavigationBar, ID_VIEW_NAVIGATION, m_wndTree, m_wndCalendar, 250))
	{
		TRACE0("未能创建导航窗格\n");
		return -1;      // 未能创建
	}
#endif

#if __CAPTIONBAR__
	// 创建标题栏: 
	if (!CreateCaptionBar())
	{
		TRACE0("未能创建标题栏\n");
		return -1;      // 未能创建
	}
#endif

#if __OUTLOOKBAR__
	// 已创建 Outlook 栏，应允许在左侧停靠。
	EnableDocking(CBRS_ALIGN_LEFT);
	EnableAutoHidePanes(CBRS_ALIGN_RIGHT);
#endif

	// 加载菜单项图像(不在任何标准工具栏上): 
	CMFCToolBar::AddToolBarForImageCollection(IDR_MENU_IMAGES, theApp.m_bHiColorIcons ? IDB_MENU_IMAGES_24 : 0);

#if __DOCKINGBAR__
	// 创建停靠窗口
	if (!CreateDockingWindows())
	{
		TRACE0("未能创建停靠窗口\n");
		return -1;
	}

#if __FC_WIN__
	m_wndFileView.EnableDocking(CBRS_ALIGN_ANY);
	m_wndClassView.EnableDocking(CBRS_ALIGN_ANY);
	DockPane(&m_wndFileView);
	CDockablePane* pTabbedBar = nullptr;
	m_wndClassView.AttachToTabWnd(&m_wndFileView, DM_SHOW, TRUE, &pTabbedBar);
#endif
#if __OUT_PUT_WIN__
	m_wndOutput.EnableDocking(CBRS_ALIGN_ANY);
	DockPane(&m_wndOutput);
#endif
	m_wndIRControl.EnableDocking(CBRS_ALIGN_ANY);
	DockPane(&m_wndIRControl);
	m_wndDevices.EnableDocking(CBRS_ALIGN_ANY);
	CDockablePane* pTabbedBar = nullptr;
	m_wndDevices.AttachToTabWnd(&m_wndIRControl, DM_SHOW, TRUE, &pTabbedBar);
#endif

	// 基于持久值设置视觉管理器和样式
	OnApplicationLook(theApp.m_nAppLook);

	// 启用增强的窗口管理对话框
	EnableWindowsDialog(ID_WINDOW_MANAGER, ID_WINDOW_MANAGER, TRUE);

#if __TAB_BAR__
	// 将文档名和应用程序名称在窗口标题栏上的顺序进行交换。这
	// 将改进任务栏的可用性，因为显示的文档名带有缩略图。
	ModifyStyle(0, FWS_PREFIXTITLE);
#endif

#if _USE_DLGVIEW_ // 第一个视图;
	CCreateContext context;
	m_pUB530View = new CChildFrame();
	context.m_pLastView = NULL;
	context.m_pCurrentFrame = this;
	context.m_pCurrentDoc = m_pDoc;
	context.m_pNewViewClass = RUNTIME_CLASS(CUB530View);
	context.m_pNewDocTemplate = m_pDoc->GetDocTemplate();
	if (m_pUB530View->LoadFrame(DLG_UB530VIEW, WS_MAXIMIZE | WS_CHILDWINDOW, this, &context))
	{
		//m_pUB530View->ShowWindow(SW_SHOWMAXIMIZED);
		m_pUB530View->InitialUpdateFrame(context.m_pCurrentDoc, FALSE);
		//m_pUB530View->MDIActivate();
	}
#else
	CCreateContext context;
	m_pUB530View = new CChildFrame();
	context.m_pLastView = NULL;
	context.m_pCurrentFrame = this;
	context.m_pCurrentDoc = m_pDoc;
	context.m_pNewViewClass = RUNTIME_CLASS(CSATHelperView);
	context.m_pNewDocTemplate = m_pDoc->GetDocTemplate();
	if (m_pUB530View->LoadFrame(1100, WS_MAXIMIZE | WS_OVERLAPPEDWINDOW /*WS_CHILDWINDOW*/, this, &context))
	{
		//m_pUB530View->ShowWindow(SW_SHOWMAXIMIZED);
		m_pUB530View->InitialUpdateFrame(context.m_pCurrentDoc, TRUE);
		//m_pUB530View->MDIActivate();
	}
#endif

#if 0 // 第二个视图;
	CCreateContext context2;
	m_pDeviceView = new CChildFrame();
	context2.m_pLastView = NULL;
	context2.m_pCurrentFrame = this;
	context2.m_pCurrentDoc = m_pDoc;
	context2.m_pNewViewClass = RUNTIME_CLASS(CDeviceView);
	context2.m_pNewDocTemplate = m_pDoc->GetDocTemplate();
	// 说明：DLG_DEVICEMANAGER对话框的Style属性必须设置成Child才能LoadFrame成功;
	if (m_pDeviceView->LoadFrame(DLG_DEVICEMANAGER, WS_MAXIMIZE | WS_CHILDWINDOW, this, &context2))
	{
		//m_pDeviceView->ShowWindow(SW_SHOWMAXIMIZED);
		m_pDeviceView->InitialUpdateFrame(NULL, FALSE);
		m_pDeviceView->MDIActivate();
	}
#endif

	m_pUB530View->MDIActivate();
	m_pUB530View->ShowWindow(SW_SHOWMAXIMIZED);


	InitDevicePanel();

	return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CMDIFrameWndEx::PreCreateWindow(cs) )
		return FALSE;
	// TODO: 在此处通过修改
	//  CREATESTRUCT cs 来修改窗口类或样式

	//不把子框架标题添加到主框架标题(主框架中就不会再处理WM_SETTEXT消息)
	cs.style &= ~FWS_ADDTOTITLE;

	return TRUE;
}

#if __DOCKINGBAR__
BOOL CMainFrame::CreateDockingWindows()
{
#if __FC_WIN__
	// 创建类视图
	CString strClassView;
	bNameValid = strClassView.LoadString(IDS_CLASS_VIEW);
	ASSERT(bNameValid);
	if (!m_wndClassView.Create(strClassView, this, CRect(0, 0, 200, 200), TRUE, ID_VIEW_CLASSVIEW, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | CBRS_LEFT | CBRS_FLOAT_MULTI))
	{
		TRACE0("未能创建“类视图”窗口\n");
		return FALSE; // 未能创建
	}

	// 创建文件视图
	CString strFileView;
	bNameValid = strFileView.LoadString(IDS_FILE_VIEW);
	ASSERT(bNameValid);
	if (!m_wndFileView.Create(strFileView, this, CRect(0, 0, 200, 200), TRUE, ID_VIEW_FILEVIEW, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | CBRS_LEFT| CBRS_FLOAT_MULTI))
	{
		TRACE0("未能创建“文件视图”窗口\n");
		return FALSE; // 未能创建
	}
#endif 

#if __OUT_PUT_WIN__
	// 创建输出窗口
	CString strOutputWnd;
	bNameValid = strOutputWnd.LoadString(IDS_OUTPUT_WND);
	ASSERT(bNameValid);
	if (!m_wndOutput.Create(strOutputWnd, this, CRect(0, 0, 100, 100), TRUE, ID_VIEW_OUTPUTWND, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | CBRS_BOTTOM | CBRS_FLOAT_MULTI))
	{
		TRACE0("未能创建输出窗口\n");
		return FALSE; // 未能创建
	}
#endif

	// 创建遥控器窗口
	CString strPropertiesWnd = _T("遥控器");
	if (!m_wndIRControl.Create(strPropertiesWnd, this, CRect(0, 0, 230, 200), TRUE, ID_VIEW_PROPERTIESWND, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | CBRS_RIGHT | CBRS_FLOAT_MULTI))
	{
		TRACE0("未能创建“遥控器”窗口\n");
		return FALSE; // 未能创建
	}

	// 创建设备管理窗口
	CString strDevicesWnd = _T("执行器");
	if (!m_wndDevices.Create(strDevicesWnd, this, CRect(0, 0, 230, 200), TRUE, ID_VIEW_OUTPUTWND, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | CBRS_RIGHT | CBRS_FLOAT_MULTI))
	{
		TRACE0("未能创建“执行器”窗口\n");
		return FALSE; // 未能创建
	}

	SetDockingWindowIcons(theApp.m_bHiColorIcons);
	return TRUE;
}

void CMainFrame::SetDockingWindowIcons(BOOL bHiColorIcons)
{
#if __FC_WIN__
	HICON hFileViewIcon = (HICON) ::LoadImage(::AfxGetResourceHandle(), MAKEINTRESOURCE(bHiColorIcons ? IDI_FILE_VIEW_HC : IDI_FILE_VIEW), IMAGE_ICON, ::GetSystemMetrics(SM_CXSMICON), ::GetSystemMetrics(SM_CYSMICON), 0);
	m_wndFileView.SetIcon(hFileViewIcon, FALSE);

	HICON hClassViewIcon = (HICON) ::LoadImage(::AfxGetResourceHandle(), MAKEINTRESOURCE(bHiColorIcons ? IDI_CLASS_VIEW_HC : IDI_CLASS_VIEW), IMAGE_ICON, ::GetSystemMetrics(SM_CXSMICON), ::GetSystemMetrics(SM_CYSMICON), 0);
	m_wndClassView.SetIcon(hClassViewIcon, FALSE);
#endif

#if __OUT_PUT_WIN__
	HICON hOutputBarIcon = (HICON) ::LoadImage(::AfxGetResourceHandle(), MAKEINTRESOURCE(bHiColorIcons ? IDI_OUTPUT_WND_HC : IDI_OUTPUT_WND), IMAGE_ICON, ::GetSystemMetrics(SM_CXSMICON), ::GetSystemMetrics(SM_CYSMICON), 0);
	m_wndOutput.SetIcon(hOutputBarIcon, FALSE);
#endif

	HICON hPropertiesBarIcon = (HICON) ::LoadImage(::AfxGetResourceHandle(), MAKEINTRESOURCE(bHiColorIcons ? IDI_PROPERTIES_WND_HC : IDI_PROPERTIES_WND), IMAGE_ICON, ::GetSystemMetrics(SM_CXSMICON), ::GetSystemMetrics(SM_CYSMICON), 0);
	m_wndIRControl.SetIcon(hPropertiesBarIcon, FALSE);

	HICON hOutputBarIcon = (HICON) ::LoadImage(::AfxGetResourceHandle(), MAKEINTRESOURCE(bHiColorIcons ? IDI_OUTPUT_WND_HC : IDI_OUTPUT_WND), IMAGE_ICON, ::GetSystemMetrics(SM_CXSMICON), ::GetSystemMetrics(SM_CYSMICON), 0);
	m_wndDevices.SetIcon(hOutputBarIcon, FALSE);

#if __TAB_BAR__
	UpdateMDITabbedBarsIcons();
#endif
}
#endif

#if __OUTLOOKBAR__
BOOL CMainFrame::CreateOutlookBar(CMFCOutlookBar& bar, UINT uiID, CMFCShellTreeCtrl& tree, CCalendarBar& calendar, int nInitialWidth)
{
	bar.SetMode2003();

	BOOL bNameValid;
	CString strTemp;
	bNameValid = strTemp.LoadString(IDS_SHORTCUTS);
	ASSERT(bNameValid);
	if (!bar.Create(strTemp, this, CRect(0, 0, nInitialWidth, 32000), uiID, WS_CHILD | WS_VISIBLE | CBRS_LEFT))
	{
		return FALSE; // 未能创建
	}

	CMFCOutlookBarTabCtrl* pOutlookBar = (CMFCOutlookBarTabCtrl*)bar.GetUnderlyingWindow();

	if (pOutlookBar == nullptr)
	{
		ASSERT(FALSE);
		return FALSE;
	}

	pOutlookBar->EnableInPlaceEdit(TRUE);

	static UINT uiPageID = 1;

	// 可浮动，可自动隐藏，可调整大小，但不能关闭
	DWORD dwStyle = AFX_CBRS_FLOAT | AFX_CBRS_AUTOHIDE | AFX_CBRS_RESIZE;

	CRect rectDummy(0, 0, 0, 0);
	const DWORD dwTreeStyle = WS_CHILD | WS_VISIBLE | TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS;

	tree.Create(dwTreeStyle, rectDummy, &bar, 1200);
	bNameValid = strTemp.LoadString(IDS_FOLDERS);
	ASSERT(bNameValid);
	pOutlookBar->AddControl(&tree, strTemp, 2, TRUE, dwStyle);

	calendar.Create(rectDummy, &bar, 1201);
	bNameValid = strTemp.LoadString(IDS_CALENDAR);
	ASSERT(bNameValid);
	pOutlookBar->AddControl(&calendar, strTemp, 3, TRUE, dwStyle);

	bar.SetPaneStyle(bar.GetPaneStyle() | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);

	pOutlookBar->SetImageList(theApp.m_bHiColorIcons ? IDB_PAGES_HC : IDB_PAGES, 24);
	pOutlookBar->SetToolbarImageList(theApp.m_bHiColorIcons ? IDB_PAGES_SMALL_HC : IDB_PAGES_SMALL, 16);
	pOutlookBar->RecalcLayout();

	BOOL bAnimation = theApp.GetInt(_T("OutlookAnimation"), TRUE);
	CMFCOutlookBarTabCtrl::EnableAnimation(bAnimation);

	bar.SetButtonsFont(&afxGlobalData.fontBold);

	return TRUE;
}
#endif

#if __CAPTIONBAR__
BOOL CMainFrame::CreateCaptionBar()
{
	if (!m_wndCaptionBar.Create(WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS, this, ID_VIEW_CAPTION_BAR, -1, TRUE))
	{
		TRACE0("未能创建标题栏\n");
		return FALSE;
	}

	BOOL bNameValid;

	CString strTemp, strTemp2;
	bNameValid = strTemp.LoadString(IDS_CAPTION_BUTTON);
	ASSERT(bNameValid);
	m_wndCaptionBar.SetButton(strTemp, ID_TOOLS_OPTIONS, CMFCCaptionBar::ALIGN_LEFT, FALSE);
	bNameValid = strTemp.LoadString(IDS_CAPTION_BUTTON_TIP);
	ASSERT(bNameValid);
	m_wndCaptionBar.SetButtonToolTip(strTemp);

	bNameValid = strTemp.LoadString(IDS_CAPTION_TEXT);
	ASSERT(bNameValid);
	m_wndCaptionBar.SetText(strTemp, CMFCCaptionBar::ALIGN_LEFT);

	m_wndCaptionBar.SetBitmap(IDB_INFO, RGB(255, 255, 255), FALSE, CMFCCaptionBar::ALIGN_LEFT);
	bNameValid = strTemp.LoadString(IDS_CAPTION_IMAGE_TIP);
	ASSERT(bNameValid);
	bNameValid = strTemp2.LoadString(IDS_CAPTION_IMAGE_TEXT);
	ASSERT(bNameValid);
	m_wndCaptionBar.SetImageToolTip(strTemp, strTemp2);

	return TRUE;
}
#endif

// CMainFrame 诊断

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CMDIFrameWndEx::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CMDIFrameWndEx::Dump(dc);
}
#endif //_DEBUG


// CMainFrame 消息处理程序

void CMainFrame::OnWindowManager()
{
	ShowWindowsDialog();
}

void CMainFrame::OnApplicationLook(UINT id)
{
	CWaitCursor wait;

	theApp.m_nAppLook = id;

	switch (theApp.m_nAppLook)
	{
	case ID_VIEW_APPLOOK_WIN_2000:
		CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManager));
		m_wndRibbonBar.SetWindows7Look(FALSE);
		break;

	case ID_VIEW_APPLOOK_OFF_XP:
		CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerOfficeXP));
		m_wndRibbonBar.SetWindows7Look(FALSE);
		break;

	case ID_VIEW_APPLOOK_WIN_XP:
		CMFCVisualManagerWindows::m_b3DTabsXPTheme = TRUE;
		CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerWindows));
		m_wndRibbonBar.SetWindows7Look(FALSE);
		break;

	case ID_VIEW_APPLOOK_OFF_2003:
		CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerOffice2003));
		CDockingManager::SetDockingMode(DT_SMART);
		m_wndRibbonBar.SetWindows7Look(FALSE);
		break;

	case ID_VIEW_APPLOOK_VS_2005:
		CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerVS2005));
		CDockingManager::SetDockingMode(DT_SMART);
		m_wndRibbonBar.SetWindows7Look(FALSE);
		break;

	case ID_VIEW_APPLOOK_VS_2008:
		CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerVS2008));
		CDockingManager::SetDockingMode(DT_SMART);
		m_wndRibbonBar.SetWindows7Look(FALSE);
		break;

	case ID_VIEW_APPLOOK_WINDOWS_7:
		CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerWindows7));
		CDockingManager::SetDockingMode(DT_SMART);
		m_wndRibbonBar.SetWindows7Look(TRUE);
		break;

	default:
		switch (theApp.m_nAppLook)
		{
		case ID_VIEW_APPLOOK_OFF_2007_BLUE:
			CMFCVisualManagerOffice2007::SetStyle(CMFCVisualManagerOffice2007::Office2007_LunaBlue);
			break;

		case ID_VIEW_APPLOOK_OFF_2007_BLACK:
			CMFCVisualManagerOffice2007::SetStyle(CMFCVisualManagerOffice2007::Office2007_ObsidianBlack);
			break;

		case ID_VIEW_APPLOOK_OFF_2007_SILVER:
			CMFCVisualManagerOffice2007::SetStyle(CMFCVisualManagerOffice2007::Office2007_Silver);
			break;

		case ID_VIEW_APPLOOK_OFF_2007_AQUA:
			CMFCVisualManagerOffice2007::SetStyle(CMFCVisualManagerOffice2007::Office2007_Aqua);
			break;
		}

		CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerOffice2007));
		CDockingManager::SetDockingMode(DT_SMART);
		m_wndRibbonBar.SetWindows7Look(FALSE);
	}

#if __DOCKINGBAR__
#if __OUT_PUT_WIN__
	m_wndOutput.UpdateFonts();
#endif
#endif
	RedrawWindow(nullptr, nullptr, RDW_ALLCHILDREN | RDW_INVALIDATE | RDW_UPDATENOW | RDW_FRAME | RDW_ERASE);

	theApp.WriteInt(_T("ApplicationLook"), theApp.m_nAppLook);
}

void CMainFrame::OnUpdateApplicationLook(CCmdUI* pCmdUI)
{
	pCmdUI->SetRadio(theApp.m_nAppLook == pCmdUI->m_nID);
}

void CMainFrame::OnViewCaptionBar()
{
#if __CAPTIONBAR__
	m_wndCaptionBar.ShowWindow(m_wndCaptionBar.IsVisible() ? SW_HIDE : SW_SHOW);
	RecalcLayout(FALSE);
#endif
}

void CMainFrame::OnUpdateViewCaptionBar(CCmdUI* pCmdUI)
{
#if __CAPTIONBAR__
	pCmdUI->SetCheck(m_wndCaptionBar.IsVisible());
#endif
}

void CMainFrame::OnOptions()
{
	CMFCRibbonCustomizeDialog *pOptionsDlg = new CMFCRibbonCustomizeDialog(this, &m_wndRibbonBar);
	ASSERT(pOptionsDlg != nullptr);

	pOptionsDlg->DoModal();
	delete pOptionsDlg;
}


void CMainFrame::OnSettingChange(UINT uFlags, LPCTSTR lpszSection)
{
	CMDIFrameWndEx::OnSettingChange(uFlags, lpszSection);
#if __DOCKINGBAR__
#if __OUT_PUT_WIN__
	m_wndOutput.UpdateFonts();
#endif
#endif
}

LRESULT CMainFrame::OnMsgStatusBar(WPARAM wParam, LPARAM lParam)
{
	CString strMSG = *(CString*)wParam;
	int uId = lParam;

	SetRibbonStatusBarText(strMSG, uId);

	return LRESULT();
}

void CMainFrame::OnTraymenuExit()
{
	// TODO: 在此添加命令处理程序代码
	//GetActiveView();
	CFrameWnd::OnClose();
}


void CMainFrame::OnClose()
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
#ifndef _DEBUG
	ShowWindow(SW_HIDE);
	m_bNoticeTray = FALSE;
#else
	CFrameWnd::OnClose();
#endif
}


BOOL CMainFrame::PreTranslateMessage(MSG* pMsg)
{
	// TODO: 在此添加专用代码和/或调用基类
	static BOOL bTopWnd = FALSE;
	if (pMsg->message == WM_KEYDOWN)
	{
		if (pMsg->wParam == VK_F3)
		{
			if (bTopWnd == FALSE)
			{
				SetWindowPos(&wndTopMost, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);//窗口置顶
				SetWindowText(_T("SATHelper 视频采集卡-前置窗口（请按F3取消或开启前置）"));
			}
			else
			{
				SetWindowPos(&wndNoTopMost, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);//取消窗口置顶	
				SetWindowText(_T("SATHelper 视频采集卡-取消前置（请按F3取消或开启前置）"));
			}

			bTopWnd = !bTopWnd;

			// 必须退出,否则执行2次;
			return TRUE;
		}
	}

	return CMDIFrameWndEx::PreTranslateMessage(pMsg);
}

LRESULT CMainFrame::OnRibbonChanged(WPARAM wParam, LPARAM lParam)
{
	CMFCRibbonCategory* pCategory = m_wndRibbonBar.GetActiveCategory();
	int nCategory = m_wndRibbonBar.GetCategoryIndex(pCategory);
	if (nCategory == 1)
	{
		if (m_pUB530View)
		{
			m_pUB530View->MDIActivate();
			m_pUB530View->ShowWindow(SW_SHOWMAXIMIZED);
		}
	}
	else if (nCategory == 2)
	{
		if (m_pDeviceView)
		{
			m_pDeviceView->MDIActivate();
			m_pDeviceView->ShowWindow(SW_SHOWMAXIMIZED);
		}
	}

	return LRESULT();
}

void CMainFrame::ReadBaiduCfg(BaiduCfg& bdcfg)
{
	TCHAR szValue[MAX_PATH] = { 0 };
	TCHAR szBuffer[1024] = { 0 };
	_stprintf_s(m_szBaiduCfgPath, _T("%s%s"), Global::g_szPython27Dir, _T("Lib\\site-packages\\ssat_sdk\\config\\baidu.cfg"));

	GetPrivateProfileString(_T("CurStatus"), _T("Cur_Count"), NULL, szValue, MAX_PATH, m_szBaiduCfgPath);
	bdcfg.cur_count = szValue;

	GetPrivateProfileString(_T("CurStatus"), _T("Reset_Time"), NULL, szValue, MAX_PATH, m_szBaiduCfgPath);
	bdcfg.reset_time = szValue;

	// 读取所有Section值;
	DWORD dwLen = GetPrivateProfileString(NULL, NULL, NULL, szBuffer, 1024, m_szBaiduCfgPath);
	for (int i = 0, j = 0; i < dwLen; i++)
	{
		if (szBuffer[i] == '\0')
		{
			TCHAR szData[MAX_PATH] = { 0 };
			memcpy(szData, &szBuffer[j], i - j);
			if (_tcsicmp(szData, _T("CurStatus")))
			{
				CountInfo countInfo;
				GetPrivateProfileString(szData, _T("APP_ID"), NULL, szValue, MAX_PATH, m_szBaiduCfgPath);
				countInfo.app_id = szValue;

				GetPrivateProfileString(szData, _T("API_KEY"), NULL, szValue, MAX_PATH, m_szBaiduCfgPath);
				countInfo.api_key = szValue;

				GetPrivateProfileString(szData, _T("SECRET_KEY"), NULL, szValue, MAX_PATH, m_szBaiduCfgPath);
				countInfo.secret_key = szValue;

				GetPrivateProfileString(szData, _T("Ret_Count"), NULL, szValue, MAX_PATH, m_szBaiduCfgPath);
				countInfo.ret_count = szValue;

				bdcfg.list_count.insert(std::pair<std::string, CountInfo>(szData, countInfo));
			}
			OutputDebugString(szData);
			OutputDebugString("\n");
			j = i + 1;
		}
	}
}

void CMainFrame::ReadRsRunCfg(RsRunCfg& rscfg)
{
	TCHAR szValue[MAX_PATH] = { 0 };
	TCHAR szBuffer[1024] = { 0 };
	_stprintf_s(m_szRsCfgPath, _T("%s%s"), Global::g_szPython27Dir, _T("Lib\\site-packages\\ssat_sdk\\config\\resource_run.cfg"));

	GetPrivateProfileString(_T("Sound"), _T("sound_list"), NULL, szValue, MAX_PATH, m_szRsCfgPath);
	rscfg.sound_list = szValue;

	GetPrivateProfileString(_T("COMM"), _T("issendkeytakepicture_tester"), NULL, szValue, MAX_PATH, m_szRsCfgPath);
	rscfg.full_cut = szValue;

	GetPrivateProfileString(_T("COMM"), _T("tg39_port"), NULL, szValue, MAX_PATH, m_szRsCfgPath);
	rscfg.tg39_port = szValue;

	GetPrivateProfileString(_T("COMM"), _T("serial_communicator_port"), NULL, szValue, MAX_PATH, m_szRsCfgPath);
	rscfg.tv_port = szValue;

	GetPrivateProfileString(_T("COMM"), _T("sat_home"), NULL, szValue, MAX_PATH, m_szRsCfgPath);
	rscfg.sat_home = szValue;

	GetPrivateProfileString(_T("COMM"), _T("sat_result_dir"), NULL, szValue, MAX_PATH, m_szRsCfgPath);
	rscfg.sat_result_dir = szValue;

	GetPrivateProfileString(_T("COMM"), _T("chroma22293"), NULL, szValue, MAX_PATH, m_szRsCfgPath);
	rscfg.chroma22293 = szValue;

	//...
	GetPrivateProfileString(_T("MenuTree"), _T("menutreeselectedchip"), NULL, szValue, MAX_PATH, m_szRsCfgPath);
	rscfg.menutree_chip = szValue;

	GetPrivateProfileString(_T("MenuTree"), _T("menutreeselectedstyle"), NULL, szValue, MAX_PATH, m_szRsCfgPath);
	rscfg.menutree_style = szValue;

	GetPrivateProfileString(_T("MenuTree"), _T("menutreeselectedchannel"), NULL, szValue, MAX_PATH, m_szRsCfgPath);
	rscfg.menutree_channel = szValue;

	// SATHelper
	GetPrivateProfileString(_T("SATHelper"), _T("chroma1"), NULL, szValue, MAX_PATH, m_szRsCfgPath);
	rscfg.chroma1 = szValue;

	GetPrivateProfileString(_T("SATHelper"), _T("chroma2"), NULL, szValue, MAX_PATH, m_szRsCfgPath);
	rscfg.chroma2 = szValue;

	rscfg.bWatchTV = GetPrivateProfileInt(_T("Abnormal"), _T("Watch"), 0, m_szRsCfgPath);
}

void CMainFrame::ReadTreePath()
{
	// 必须在ReadRsRunCfg之后调用;
	TCHAR szRootDir[MAX_PATH] = { 0 };
	_stprintf_s(szRootDir, _T("%s%s"), m_rscfg.sat_home.c_str(), _T("resource\\MenuTree\\"));

	auto _get_dir = [](std::string dir) {
		int nIndex = dir.find_last_of('\\');
		if (nIndex != std::string::npos)
		{
			return dir.substr(nIndex + 1);
		}

		return dir;
	};

	auto _get_name = [](std::string dir) {
		int pos1 = dir.find_last_of('\\');
		if (pos1 != std::string::npos)
		{
			dir = dir.substr(pos1 + 1);
			int pos2 = dir.find_last_of('.');
			if (pos2 != std::string::npos)
			{
				dir = dir.substr(0, pos2);
				int pos3 = dir.find("MenuTree_");
				if (pos3 != std::string::npos)
				{
					return dir.substr(strlen("MenuTree_"));
				}
			}
		}

		return std::string();
	};

	// 查找根目录下的目录;
	filehelper fh;
	STR_VEC vt_dir;
	fh.getsubfolder(szRootDir, &vt_dir);
	for (auto it : vt_dir)
	{
		STR_VEC vt_subdir;
		std::vector<UITree> vtTree;
		fh.getsubfolder(it.c_str(), &vt_subdir);
		for (auto ui : vt_subdir)
		{
			// 查找xls文件;
			UITree uitree;
			uitree.ui = ui;
			fh.getfiles_findout_subfolder(ui.c_str(), _T("*.xls"), &uitree.vtSubUI);
			vtTree.push_back(uitree);
		}

		m_map_tree.insert(std::pair<std::string, std::vector<UITree>>(_get_dir(it), vtTree));
	}

	// 去除目录级;
	for (std::map<std::string, std::vector<UITree>>::iterator it = m_map_tree.begin(); it != m_map_tree.end(); it++)
	{
		for (std::vector<UITree>::iterator ui = it->second.begin(); ui != it->second.end(); ui++)
		{
			ui->ui = _get_dir(ui->ui);
			for (std::vector<std::string>::iterator xls = ui->vtSubUI.begin(); xls != ui->vtSubUI.end();)
			{
				*xls = _get_name(xls->c_str());
				if (xls->size() == 0)
				{
					xls = ui->vtSubUI.erase(xls);
				}
				else
				{
					xls++;
				}
			}
		}
	}
}

void CMainFrame::ParseJson()
{
	if (m_rscfg.chroma1.size() == 0)
	{
		cJSON* pJson = cJSON_Parse(m_rscfg.chroma22293.c_str());
		if (pJson)
		{
			cJSON* pArray = cJSON_GetObjectItem(pJson, "devices");
			if (pArray)
			{
				int nCount = cJSON_GetArraySize(pArray);
				for (int i = 0; i < nCount; i++)
				{
					cJSON* pItem = cJSON_GetArrayItem(pArray, i);
					if (pItem)
					{
						if (i == 0)
							m_rscfg.chroma1 = cJSON_GetObjectItem(pItem, "port") ? cJSON_GetObjectItem(pItem, "port")->valuestring : _T("");
						if (i == 1)
							m_rscfg.chroma2 = cJSON_GetObjectItem(pItem, "port") ? cJSON_GetObjectItem(pItem, "port")->valuestring : _T("");
					}
				}
			}
		}

		// 保存;
		WritePrivateProfileString(_T("SATHelper"), _T("chroma1"), m_rscfg.chroma1.c_str(), m_szRsCfgPath);
		WritePrivateProfileString(_T("SATHelper"), _T("chroma2"), m_rscfg.chroma2.c_str(), m_szRsCfgPath);
	}
}

void CMainFrame::RunDevice()
{
	CMFCRibbonBar* pRibbon = g_pMainFrame->GetRibbonBar();
	ASSERT_VALID(pRibbon);
	CMFCRibbonButton* pCheck1 = DYNAMIC_DOWNCAST(CMFCRibbonButton, pRibbon->FindByID(ID_CHECK1_22293));
	CMFCRibbonButton* pCheck2 = DYNAMIC_DOWNCAST(CMFCRibbonButton, pRibbon->FindByID(ID_CHECK2_22293));

	if (m_rscfg.chroma1.size())
	{
		CDevice* pdev = new CDevice();
		pdev->m_iBaudrate = 115200;
		pdev->m_iDatabit = 8;
		pdev->m_iPort = atoi(m_rscfg.chroma1.substr(3).c_str());
		pdev->m_iParitybit = 0;
		pdev->m_iStopbit = 0;
		pdev->m_iID = 1;
		pdev->m_strDeviceName = "Chroma22293";
		pdev->m_iDeviceType = 1;
		if (pdev->Open())
		{
			g_dmap.insert(std::pair<int, CDevice*>(pdev->m_iID, pdev));
		}
		else
		{
			delete pdev;
		}
	}

	if (m_rscfg.chroma2.size())
	{
		CDevice* pdev = new CDevice();
		pdev->m_iBaudrate = 115200;
		pdev->m_iDatabit = 8;
		pdev->m_iPort = atoi(m_rscfg.chroma2.substr(3).c_str());
		pdev->m_iParitybit = 0;
		pdev->m_iStopbit = 0;
		pdev->m_iID = 2;
		pdev->m_strDeviceName = "Chroma22293";
		pdev->m_iDeviceType = 1;
		if (pdev->Open())
		{
			g_dmap.insert(std::pair<int, CDevice*>(pdev->m_iID, pdev));
		}
		else
		{
			delete pdev;
		}
	}

	UpdateData(FALSE);
}

void CMainFrame::InitCombobox_port()
{
	CMFCRibbonBar* pRibbon = g_pMainFrame->GetRibbonBar();
	ASSERT_VALID(pRibbon);
	CMFCRibbonComboBox* pCommbox1 = DYNAMIC_DOWNCAST(CMFCRibbonComboBox, pRibbon->FindByID(ID_COMBO1_22293));
	CMFCRibbonComboBox* pCommbox2 = DYNAMIC_DOWNCAST(CMFCRibbonComboBox, pRibbon->FindByID(ID_COMBO2_22293));
	CMFCRibbonComboBox* pCommboxTV = DYNAMIC_DOWNCAST(CMFCRibbonComboBox, pRibbon->FindByID(ID_COMBO_TVPORT));
	for (std::vector<std::string>::iterator it = m_vtCOM.begin(); it != m_vtCOM.end(); it++)
	{
		if (!IsPortInserted(pCommbox1, it->c_str()))
			pCommbox1->AddItem(it->c_str());

		if (!IsPortInserted(pCommbox2, it->c_str()))
			pCommbox2->AddItem(it->c_str());

		if (!IsPortInserted(pCommboxTV, it->c_str()))
			pCommboxTV->AddItem(it->c_str());
	}

	if (m_rscfg.chroma1.size())
		pCommbox1->SelectItem(m_rscfg.chroma1.c_str());
	if (m_rscfg.chroma2.size())
		pCommbox2->SelectItem(m_rscfg.chroma2.c_str());
	if (m_rscfg.tv_port.size())
		pCommboxTV->SelectItem(m_rscfg.tv_port.c_str());

}

BOOL CMainFrame::IsPortInserted(CMFCRibbonComboBox* pCommbox, LPCTSTR lpPort)
{
	int nCount = pCommbox->GetCount();
	for (int i = 0; i < nCount; i++ )
	{
		if (_tcsicmp(pCommbox->GetItem(i), lpPort) == 0)
			return TRUE;
	}

	return FALSE;
}

BOOL CMainFrame::OnDeviceChange(UINT nEventType, DWORD_PTR dwData)
{
	switch (nEventType)
	{
		// 串口拨掉;
	case DBT_DEVICEREMOVECOMPLETE:
		break;
		// 串口插入;
	case DBT_DEVICEARRIVAL:
	{
		Global::GetSysSerialPort(m_vtCOM);
		InitCombobox_port();
	}
	break;
	default:
		break;
	}
	return TRUE;
}


void CMainFrame::InitDevicePanel()
{
	ParseJson();
	// Ribbon Combobox添加默认项;
	CMFCRibbonBar* pRibbon = g_pMainFrame->GetRibbonBar();
	ASSERT_VALID(pRibbon);
	CMFCRibbonComboBox* pOcrCombo = DYNAMIC_DOWNCAST(CMFCRibbonComboBox, pRibbon->FindByID(ID_COMBOX_OCR));
	if (pOcrCombo)
	{
		for (auto it : m_bdcfg.list_count)
		{
			pOcrCombo->AddItem(it.first.c_str());
		}

		// 选择默认项;
		pOcrCombo->SetEditText(m_bdcfg.cur_count.c_str());
	}

	// 剩余次数;
	CMFCRibbonEdit* pOcrCount = DYNAMIC_DOWNCAST(CMFCRibbonEdit, pRibbon->FindByID(ID_EDIT_COUNT));
	if (pOcrCount)
	{
		pOcrCount->SetEditText(m_bdcfg.reset_time.c_str());
	}

	// 声音等级;
	CMFCRibbonEdit* pSoundLevel = DYNAMIC_DOWNCAST(CMFCRibbonEdit, pRibbon->FindByID(ID_EDIT_SOUND_LEVEL));
	if (pSoundLevel)
	{
		pSoundLevel->SetEditText(m_bdcfg.reset_time.c_str());
	}

	// 全步骤截图;
// 	CMFCRibbonCheckBox* pCheckFullCut = DYNAMIC_DOWNCAST(CMFCRibbonCheckBox, pRibbon->FindByID(ID_CHECK_FULL_CUT));
// 	if (pCheckFullCut)
// 	{
// 		pCheckFullCut->SetCheck();
// 	}

	// MenuTree;
	CMFCRibbonComboBox* pChipCombo = DYNAMIC_DOWNCAST(CMFCRibbonComboBox, pRibbon->FindByID(ID_COMBO_PRODUCT));
	if (pChipCombo)
	{
		for (auto it : m_map_tree)
		{
			pChipCombo->AddItem(it.first.c_str());
		}

		// 选择默认项;
		pChipCombo->SetEditText(m_rscfg.menutree_chip.c_str());

		// UI下拉框;
		CString curText = pChipCombo->GetEditText();
		CMFCRibbonComboBox* pStyleCombo = DYNAMIC_DOWNCAST(CMFCRibbonComboBox, pRibbon->FindByID(ID_COMBO_UI));
		for (auto it : m_map_tree.find(curText.GetString())->second)
		{
			pStyleCombo->AddItem(it.ui.c_str());
		}
		pStyleCombo->SetEditText(m_rscfg.menutree_style.c_str());

		CString strCurChip = pChipCombo->GetEditText();
		CString strCurStyle = pStyleCombo->GetEditText();
		_stprintf_s(m_szCurMenuTreeDir, _T("%s%s%s\\%s\\"), m_rscfg.sat_home.c_str(), _T("resource\\MenuTree\\"), strCurChip.GetString(), strCurStyle.GetString());
	}



	// 启动设备;
	Global::GetSysSerialPort(m_vtCOM);
	InitCombobox_port();
	RunDevice();
}

void CMainFrame::OnCheckFullCut()
{
	// TODO: 在此添加命令处理程序代码
	CMFCRibbonBar* pRibbon = g_pMainFrame->GetRibbonBar();
	ASSERT_VALID(pRibbon);
	CMFCRibbonButton* pFullCutCheck = DYNAMIC_DOWNCAST(CMFCRibbonButton, pRibbon->FindByID(ID_CHECK_FULL_CUT));
	if (pFullCutCheck)
	{
		WritePrivateProfileString(_T("COMM"), _T("issendkeytakepicture_tester"), !pFullCutCheck->IsChecked() ? _T("True") : _T("False"), m_szRsCfgPath);
		m_rscfg.full_cut = !pFullCutCheck->IsChecked() ? _T("True") : _T("False");
	}
}


void CMainFrame::OnUpdateCheckFullCut(CCmdUI* pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码
	pCmdUI->SetCheck(!_tcsicmp(m_rscfg.full_cut.c_str(), _T("True")));
}


void CMainFrame::OnHadSound()
{
	// TODO: 在此添加命令处理程序代码
}


void CMainFrame::OnUpdateHadSound(CCmdUI* pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码
}


void CMainFrame::OnDefSound()
{
	// TODO: 在此添加命令处理程序代码
}


void CMainFrame::OnUpdateDefSound(CCmdUI* pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码
}


void CMainFrame::OnComboxOcr()
{
	// TODO: 在此添加命令处理程序代码
	CMFCRibbonBar* pRibbon = g_pMainFrame->GetRibbonBar();
	ASSERT_VALID(pRibbon);
	CMFCRibbonComboBox* pOcrCombo = DYNAMIC_DOWNCAST(CMFCRibbonComboBox, pRibbon->FindByID(ID_COMBOX_OCR));
	if (pOcrCombo)
	{
		// 获取当前文本项;
		CString strOcr = pOcrCombo->GetEditText();
		WritePrivateProfileString(_T("CurStatus"), _T("Cur_Count"), strOcr, m_szBaiduCfgPath);
	}
}



void CMainFrame::OnUpdateComboxOcr(CCmdUI* pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码
}


void CMainFrame::OnComboProduct()
{
	// TODO: 在此添加命令处理程序代码
	CMFCRibbonBar* pRibbon = g_pMainFrame->GetRibbonBar();
	ASSERT_VALID(pRibbon);
	CMFCRibbonComboBox* pChipCombo = DYNAMIC_DOWNCAST(CMFCRibbonComboBox, pRibbon->FindByID(ID_COMBO_PRODUCT));
	if (pChipCombo)
	{
		// UI下拉框;
		CString curText = pChipCombo->GetEditText();
		CMFCRibbonComboBox* pStyleCombo = DYNAMIC_DOWNCAST(CMFCRibbonComboBox, pRibbon->FindByID(ID_COMBO_UI));
		pStyleCombo->RemoveAllItems();
		for (auto it : m_map_tree.find(curText.GetString())->second)
		{
			pStyleCombo->AddItem(it.ui.c_str());
		}
		pStyleCombo->SelectItem(0);

		// SUBUI下拉框;
		CString strUIName = pStyleCombo->GetEditText();
		CMFCRibbonComboBox* pSubUICombo = DYNAMIC_DOWNCAST(CMFCRibbonComboBox, pRibbon->FindByID(ID_COMBO_SUBUI));
		pSubUICombo->RemoveAllItems();
		std::vector<UITree>& ui = m_map_tree.find(curText.GetString())->second;
		for (std::vector<UITree>::iterator it = ui.begin(); it != ui.end(); it++)
		{
			if (_tcsicmp(strUIName.GetString(), it->ui.c_str()) == 0)
			{
				if (it->vtSubUI.size() == 0)
					break;

				for (auto SubUI : it->vtSubUI)
				{
					pSubUICombo->AddItem(SubUI.c_str());
				}

				pSubUICombo->SelectItem(0);
			}
		}

		// 保存结果;
		CString strCurChip = pChipCombo->GetEditText();
		WritePrivateProfileString(_T("MenuTree"), _T("menutreeselectedchip"), strCurChip, m_szRsCfgPath);

		CString strCurStyle = pStyleCombo->GetEditText();
		WritePrivateProfileString(_T("MenuTree"), _T("menutreeselectedstyle"), strCurStyle, m_szRsCfgPath);

		CString strSubStyle = pSubUICombo->GetEditText();
		WritePrivateProfileString(_T("MenuTree"), _T("menutreeselectedchannel"), strSubStyle, m_szRsCfgPath);

		_stprintf_s(m_szCurMenuTreeDir, _T("%s%s%s\\%s\\"), m_rscfg.sat_home.c_str(), _T("resource\\MenuTree\\"), strCurChip.GetString(), strCurStyle.GetString());
	}
}


void CMainFrame::OnUpdateComboProduct(CCmdUI* pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码

}

void CMainFrame::OnComboUi()
{
	// TODO: 在此添加命令处理程序代码
	CMFCRibbonBar* pRibbon = g_pMainFrame->GetRibbonBar();
	ASSERT_VALID(pRibbon);
	CMFCRibbonComboBox* pStyleCombo = DYNAMIC_DOWNCAST(CMFCRibbonComboBox, pRibbon->FindByID(ID_COMBO_UI));
	if (pStyleCombo)
	{
		CMFCRibbonComboBox* pChipCombo = DYNAMIC_DOWNCAST(CMFCRibbonComboBox, pRibbon->FindByID(ID_COMBO_PRODUCT));
		// UI下拉框;
		CString strCurChip = pChipCombo->GetEditText();
		// SUBUI下拉框;
		CString strUIName = pStyleCombo->GetEditText();
		CMFCRibbonComboBox* pSubUICombo = DYNAMIC_DOWNCAST(CMFCRibbonComboBox, pRibbon->FindByID(ID_COMBO_SUBUI));
		pSubUICombo->RemoveAllItems();
		std::vector<UITree>& ui = m_map_tree.find(strCurChip.GetString())->second;
		for (std::vector<UITree>::iterator it = ui.begin(); it != ui.end(); it++)
		{
			if (_tcsicmp(strUIName.GetString(), it->ui.c_str()) == 0)
			{
				if (it->vtSubUI.size() == 0)
					break;

				for (auto SubUI : it->vtSubUI)
				{
					pSubUICombo->AddItem(SubUI.c_str());
				}

				pSubUICombo->SelectItem(0);
			}
		}

		// UI下拉框;
		CString strCurStyle = pStyleCombo->GetEditText();
		WritePrivateProfileString(_T("MenuTree"), _T("menutreeselectedstyle"), strCurStyle, m_szRsCfgPath);

		CString strSubStyle = pSubUICombo->GetEditText();
		WritePrivateProfileString(_T("MenuTree"), _T("menutreeselectedchannel"), strSubStyle, m_szRsCfgPath);

		_stprintf_s(m_szCurMenuTreeDir, _T("%s%s%s\\%s\\"), m_rscfg.sat_home.c_str(), _T("resource\\MenuTree\\"), strCurChip.GetString(), strCurStyle.GetString());
	}
}

void CMainFrame::OnUpdateComboUi(CCmdUI* pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码

}

void CMainFrame::OnComboSubUi()
{
	// TODO: 在此添加命令处理程序代码
	CMFCRibbonBar* pRibbon = g_pMainFrame->GetRibbonBar();
	ASSERT_VALID(pRibbon);
	CMFCRibbonComboBox* pSubUICombo = DYNAMIC_DOWNCAST(CMFCRibbonComboBox, pRibbon->FindByID(ID_COMBO_SUBUI));
	if (pSubUICombo)
	{
		// UI下拉框;
		CString strSubStyle = pSubUICombo->GetEditText();
		WritePrivateProfileString(_T("MenuTree"), _T("menutreeselectedchannel"), strSubStyle, m_szRsCfgPath);
	}
}

void CMainFrame::OnUpdateComboSubUi(CCmdUI* pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码

}


void CMainFrame::OnCombo1_22293()
{
	CMFCRibbonBar* pRibbon = g_pMainFrame->GetRibbonBar();
	ASSERT_VALID(pRibbon);
	CMFCRibbonComboBox* pCombobx1 = DYNAMIC_DOWNCAST(CMFCRibbonComboBox, pRibbon->FindByID(ID_COMBO1_22293));

	// 保存到配置文件中;
	int nCurSel = pCombobx1->GetCurSel();
	if (nCurSel != CB_ERR)
	{
		CString strChroma1 = pCombobx1->GetEditText();
		WritePrivateProfileString(_T("SATHelper"), _T("chroma1"), strChroma1, m_szRsCfgPath);
		m_rscfg.chroma1 = strChroma1.GetString();

		if (g_dmap.find(1) == g_dmap.end())
		{
			CDevice* pdev = new CDevice();
			pdev->m_iBaudrate = 115200;
			pdev->m_iDatabit = 8;
			pdev->m_iPort = atoi(m_rscfg.chroma1.substr(3).c_str());
			pdev->m_iParitybit = 0;
			pdev->m_iStopbit = 1;
			pdev->m_iID = 1;
			pdev->m_strDeviceName = "Chroma22293";
			pdev->m_iDeviceType = 1;
			if (pdev->Open())
			{
				g_dmap.insert(std::pair<int, CDevice*>(pdev->m_iID, pdev));
			}
			else
			{
				delete pdev;
			}
		}
		else
		{
			CDevice* pDev = g_dmap.find(1)->second;
			int port = atoi(strChroma1.Right(1).GetString());
			if (pDev->m_iPort != port)
			{
				pDev->m_iPort = port;
				pDev->Close();
				pDev->Open();
			}
		}
	}
}

void CMainFrame::OnUpdateCombo1_22293(CCmdUI* pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码
	
}

void CMainFrame::OnCombo2_22293()
{
	CMFCRibbonBar* pRibbon = g_pMainFrame->GetRibbonBar();
	ASSERT_VALID(pRibbon);
	CMFCRibbonComboBox* pCombobx2 = DYNAMIC_DOWNCAST(CMFCRibbonComboBox, pRibbon->FindByID(ID_COMBO2_22293));

	// 保存到配置文件中;
	int nCurSel = pCombobx2->GetCurSel();
	if (nCurSel != CB_ERR)
	{
		CString strChroma2 = pCombobx2->GetEditText();
		WritePrivateProfileString(_T("SATHelper"), _T("chroma2"), strChroma2, m_szRsCfgPath);
		m_rscfg.chroma2 = strChroma2.GetString();

		if (g_dmap.find(2) == g_dmap.end())
		{
			CDevice* pdev = new CDevice();
			pdev->m_iBaudrate = 115200;
			pdev->m_iDatabit = 8;
			pdev->m_iPort = atoi(m_rscfg.chroma2.substr(3).c_str());
			pdev->m_iParitybit = 0;
			pdev->m_iStopbit = 1;
			pdev->m_iID = 2;
			pdev->m_strDeviceName = "Chroma22293";
			pdev->m_iDeviceType = 1;
			if (pdev->Open())
			{
				g_dmap.insert(std::pair<int, CDevice*>(pdev->m_iID, pdev));
			}
			else
			{
				delete pdev;
			}
		}
		else 
		{
			CDevice* pDev = g_dmap.find(2)->second;
			int port = atoi(strChroma2.Right(1).GetString());
			if (pDev->m_iPort != port)
			{
				pDev->m_iPort = port;
				pDev->Close();
				pDev->Open();
			}
		}
	}
}

void CMainFrame::OnUpdateCombo2_22293(CCmdUI* pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码
	
}


void CMainFrame::OnCheck1_22293()
{
	CMFCRibbonBar* pRibbon = g_pMainFrame->GetRibbonBar();
	ASSERT_VALID(pRibbon);
	CMFCRibbonButton* pCheck1 = DYNAMIC_DOWNCAST(CMFCRibbonButton, pRibbon->FindByID(ID_CHECK1_22293));
		
	// TODO: 在此添加命令处理程序代码
	if (g_dmap.find(1) != g_dmap.end())
	{
		if (pCheck1->IsChecked())
		{
			g_dmap.find(1)->second->Close();
		}
		else
		{
			g_dmap.find(1)->second->Open();
		}
	}
}

void CMainFrame::OnUpdateCheck1_22293(CCmdUI* pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码
	if (g_dmap.find(1) != g_dmap.end())
		pCmdUI->SetCheck(g_dmap.find(1)->second->IsOpen());
	else
		pCmdUI->SetCheck(FALSE);
}

void CMainFrame::OnCheck2_22293()
{
	// TODO: 在此添加命令处理程序代码
	CMFCRibbonBar* pRibbon = g_pMainFrame->GetRibbonBar();
	ASSERT_VALID(pRibbon);
	CMFCRibbonButton* pCheck2 = DYNAMIC_DOWNCAST(CMFCRibbonButton, pRibbon->FindByID(ID_CHECK2_22293));

	// TODO: 在此添加命令处理程序代码
	if (g_dmap.find(2) != g_dmap.end())
	{
		if (pCheck2->IsChecked())
		{
			g_dmap.find(2)->second->Close();
		}
		else
		{
			g_dmap.find(2)->second->Open();
		}
	}
}

void CMainFrame::OnUpdateCheck2_22293(CCmdUI* pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码
	if (g_dmap.find(2) != g_dmap.end())
		pCmdUI->SetCheck(g_dmap.find(2)->second->IsOpen());
	else
		pCmdUI->SetCheck(FALSE);
}


void CMainFrame::OnCombo2Tv()
{
	// TODO: 在此添加命令处理程序代码
	CMFCRibbonBar* pRibbon = g_pMainFrame->GetRibbonBar();
	ASSERT_VALID(pRibbon);
	CMFCRibbonComboBox* pCombobxTV = DYNAMIC_DOWNCAST(CMFCRibbonComboBox, pRibbon->FindByID(ID_COMBO_TVPORT));

	// 保存到配置文件中;
	int nCurSel = pCombobxTV->GetCurSel();
	if (nCurSel != CB_ERR)
	{
		CString strTVPort = pCombobxTV->GetEditText();
		WritePrivateProfileString(_T("COMM"), _T("serial_communicator_port"), strTVPort, m_szRsCfgPath);
		m_rscfg.tv_port = strTVPort.GetString();
	}
}


void CMainFrame::OnUpdateCombo2Tv(CCmdUI* pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码
}


void CMainFrame::OnCheckWatchtv()
{
	// TODO: 在此添加命令处理程序代码
	CMFCRibbonBar* pRibbon = g_pMainFrame->GetRibbonBar();
	ASSERT_VALID(pRibbon);
	CMFCRibbonButton* pCheckWatchTV = DYNAMIC_DOWNCAST(CMFCRibbonButton, pRibbon->FindByID(ID_CHECK_WATCHTV));
	m_rscfg.bWatchTV = !pCheckWatchTV->IsChecked();
	WritePrivateProfileString(_T("Abnormal"), _T("Watch"), m_rscfg.bWatchTV ? _T("1") : _T("0"), m_szRsCfgPath);
}


void CMainFrame::OnUpdateCheckWatchtv(CCmdUI* pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码
	pCmdUI->SetCheck(m_rscfg.bWatchTV);
}


void CMainFrame::OnTraymenuLogin()
{
	// TODO: 在此添加命令处理程序代码
	CDlgLogin dlg;
	if ( dlg.DoModal() )
	{
	}
}
