
// OGCAssistToolDlg.cpp : 实现文件
//

#include "stdafx.h"
#include "OGCAssistTool.h"
#include "OGCAssistToolDlg.h"
#include "PageDebug.h"
#include "PageLog.h"
#include "PageConfig.h"
#include "PageStatistics.h"
#include "Injection.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// 注入;
CInjection g_Injection;
// OGC Tool无序句柄;
HWND g_hWnd_SN = NULL;
HWND g_hWnd_SN_Combobox = NULL;
HWND g_hWnd_Tester = NULL;
HWND g_hWnd_FWVersion = NULL;
HWND g_hWnd_Channel = NULL;
// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

class CAboutDlg : public CDialogEx
{
public:
	CAboutDlg();

// 对话框数据
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()


// COGCAssistToolDlg 对话框




COGCAssistToolDlg::COGCAssistToolDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(COGCAssistToolDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void COGCAssistToolDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_TAB, m_tabCtrl);
}

BEGIN_MESSAGE_MAP(COGCAssistToolDlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_NOTIFY(TCN_SELCHANGE, IDC_TAB, &COGCAssistToolDlg::OnTcnSelchangeTab)
	ON_WM_TIMER()
END_MESSAGE_MAP()


// COGCAssistToolDlg 消息处理程序

BOOL COGCAssistToolDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 将“关于...”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 设置此对话框的图标。当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码
	//SetBackgroundImage(IDB_LOGO);
	//SetBackgroundColor(RGB(255,255,255));
	if ( GLOBAL::g_config.nOffline )
	{

	}
	InitTabCtrl();

	SetTimer(0, 300, NULL);

	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

void COGCAssistToolDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialogEx::OnSysCommand(nID, lParam);
	}
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void COGCAssistToolDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR COGCAssistToolDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

void COGCAssistToolDlg::InitTabCtrl()
{
	static CFont font;// 必须全局或成员或静态,局部变量无效异常;
	VERIFY(font.CreateFont(
		39,                        // nHeight
		0,                         // nWidth
		0,                         // nEscapement
		0,                         // nOrientation
		FW_NORMAL,                 // nWeight
		FALSE,                     // bItalic
		FALSE,                     // bUnderline
		0,                         // cStrikeOut
		ANSI_CHARSET,              // nCharSet
		OUT_DEFAULT_PRECIS,        // nOutPrecision
		CLIP_DEFAULT_PRECIS,       // nClipPrecision
		DEFAULT_QUALITY,           // nQuality
		DEFAULT_PITCH | FF_SWISS,  // nPitchAndFamily
		_T("Arial")));             
	m_tabCtrl.SetFont(&font); 

	m_ImageList.Create(IDB_TAB, 32, 1, RGB(255,255,255) );
	m_tabCtrl.SetImageList(&m_ImageList);

	m_tabCtrl.InsertItem(0, _T("调试"));
	m_tabCtrl.InsertItem(1, _T("日志"));
	m_tabCtrl.InsertItem(2, _T("统计"));
	m_tabCtrl.InsertItem(3, _T("设置"));

	TC_ITEM tcItem;
	tcItem.mask = TCIF_IMAGE;
	for( int i = 0; i < 4; i++ )
	{
		tcItem.iImage = i;
		m_tabCtrl.SetItem( i, &tcItem );                            //显示ImageList的序号
	}


	// 获取当前窗口大小;
	CRect rcWnd;
	m_tabCtrl.GetWindowRect(&rcWnd);

	int nCount = m_tabCtrl.GetItemCount();
	// 设置标签的大小(必须在属性Style里设置Fixed Width)
	CSize size(rcWnd.Width()/nCount-1, 80), sizeReturn;
	sizeReturn = m_tabCtrl.SetItemSize(size);

	// 设置好标签后,创建Page;
	CPageDebug *pDlgDebug = new CPageDebug();
	pDlgDebug->Create(PAGE_DEBUG, &m_tabCtrl);
	m_pDialog[0] = pDlgDebug;

	CPageLog* pDlgLog = new CPageLog();
	pDlgLog->Create(PAGE_LOG, &m_tabCtrl);
	m_pDialog[1] = pDlgLog;

	CPageStatistics *pDlgStatistics = new CPageStatistics();
	pDlgStatistics->Create(PAGE_STATISTICS, &m_tabCtrl);
	m_pDialog[2] = pDlgStatistics;

	CPageConfig *pDlgConfig = new CPageConfig();
	pDlgConfig->Create(PAGE_CONFIG, &m_tabCtrl);
	m_pDialog[3] = pDlgConfig;

	//设定在Tab内显示的范围
	CRect rcClient;
	m_tabCtrl.GetClientRect(rcClient);
	rcClient.top += 82;

	pDlgDebug->MoveWindow(&rcClient);
	pDlgDebug->ShowWindow(SW_SHOW);

	pDlgLog->MoveWindow(&rcClient);
	pDlgLog->ShowWindow(SW_HIDE);

	pDlgConfig->MoveWindow(&rcClient);
	pDlgConfig->ShowWindow(SW_HIDE);

	pDlgStatistics->MoveWindow(&rcClient);
	pDlgStatistics->ShowWindow(SW_HIDE);

	// 设置默认page;
	m_tabCtrl.SetCurSel(m_CurSelTab=0);
}

HWND COGCAssistToolDlg::MyFindWindow(LPCTSTR lpClassName)
{
	HWND hWnd = NULL;
	if ( lpClassName == NULL || lpClassName[0] == '\0' )
		return hWnd;

	for (std::vector<GLOBAL::WND_INFO>::iterator it = GLOBAL::g_procWndInfo.vtWndInfo.begin(); it != GLOBAL::g_procWndInfo.vtWndInfo.end(); it++ )
	{
		// 返回第一个找到的类名;
		if ( _tcscmp(lpClassName, it->szClassName) == 0 )
		{
			hWnd = it->hWnd;
			break;
		}
	}

	return hWnd;
}

void COGCAssistToolDlg::FetchOGCWnd()
{
	// SN Combobox;
	for(std::vector<GLOBAL::WND_INFO>::iterator it = GLOBAL::g_procWndInfo.vtWndInfo.begin(); it != GLOBAL::g_procWndInfo.vtWndInfo.end(); it++ )
	{
		if ( _tcscmp(_T("TfrmDemo"), it->szClassName) == 0 )
		{
			g_hWnd_SN_Combobox = (++it)->hWnd;
			break;
		}
	}

	// SN Edit && Tester;
	for(std::vector<GLOBAL::WND_INFO>::iterator it = GLOBAL::g_procWndInfo.vtWndInfo.begin(); it != GLOBAL::g_procWndInfo.vtWndInfo.end(); it++ )
	{
		if ( !_tcscmp(_T("OCC"), it->szWndTitle) &&  !_tcscmp(_T("TCheckBox"), it->szClassName) )
		{
			g_hWnd_SN = (--it)->hWnd;
			g_hWnd_Tester = (--it)->hWnd;
			break;
		}
	}

	// Channel Edit;
	for(std::vector<GLOBAL::WND_INFO>::iterator it = GLOBAL::g_procWndInfo.vtWndInfo.begin(); it != GLOBAL::g_procWndInfo.vtWndInfo.end(); it++ )
	{
		if ( !_tcscmp(_T("Connect CA310"), it->szWndTitle) &&  !_tcscmp(_T("TButton"), it->szClassName) )
		{
			g_hWnd_Channel = (--it)->hWnd;
			break;
		}
	}

	// FW Version;
	for(std::vector<GLOBAL::WND_INFO>::iterator it = GLOBAL::g_procWndInfo.vtWndInfo.begin(); it != GLOBAL::g_procWndInfo.vtWndInfo.end(); it++ )
	{
		if ( !_tcscmp(_T("FS2"), it->szWndTitle) &&  !_tcscmp(_T("TCheckBox"), it->szClassName) )
		{
			g_hWnd_FWVersion = (--it)->hWnd;
			break;
		}
	}

#ifdef _DEBUG
	TCHAR szMsg[MAX_PATH] = {0};
	//_stprintf(szMsg, _T("Channel=%08X, SN=%08X, Combobox=%08X, Tester=%08X, FWVersion=%08X\n"), g_hWnd_Channel, g_hWnd_SN, g_hWnd_SN_Combobox, g_hWnd_Tester, g_hWnd_FWVersion);
	_stprintf(szMsg, _T("Channel=%ld, SN=%ld, Combobox=%ld, Tester=%ld, FWVersion=%ld\n"), g_hWnd_Channel, g_hWnd_SN, g_hWnd_SN_Combobox, g_hWnd_Tester, g_hWnd_FWVersion);
	//TRACE(szMsg);
	GLOBAL::WriteTextLog(szMsg);
#endif
}

void COGCAssistToolDlg::OnTcnSelchangeTab(NMHDR *pNMHDR, LRESULT *pResult)
{
	// TODO: 在此添加控件通知处理程序代码
	m_pDialog[m_CurSelTab]->ShowWindow(SW_HIDE);
	//得到新的页面
	m_CurSelTab = m_tabCtrl.GetCurSel();
	//把新的页面显示出来
	m_pDialog[m_CurSelTab]->ShowWindow(SW_SHOW);
	m_pDialog[m_CurSelTab]->UpdateData(TRUE);

	*pResult = 0;
}

BOOL COGCAssistToolDlg::DestroyWindow()
{
	// TODO: 在此添加专用代码和/或调用基类
	for ( int i = 0; i < 4; i++ )
	{
		delete m_pDialog[i];
		m_pDialog[i] = NULL;
	}
	return CDialogEx::DestroyWindow();
}

void COGCAssistToolDlg::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	switch (nIDEvent)
	{
	case 0:
		{
			// OGC启动时间较长(5~7秒,根据电脑性能决定),需要判断窗口是否完全显示出来;
			if ( FindWindow(_T("TApplication"), _T("Demo")) && FindWindow(_T("TfrmDemo"), _T("(TPV 2021/09/29) UHD Series OGC Tool V2.21.1.3")) )
			{
				Sleep(1200); // 再等多会;
				GLOBAL::EnumProcessAllWnd(&GLOBAL::g_procWndInfo);
				if ( !GLOBAL::g_bHijacted )
				{
					TCHAR szDll[MAX_PATH] = {0};
					_stprintf(szDll, _T("%s%s"), GLOBAL::g_szModulePath, _T("OGCAssist.dll"));
					g_Injection.setInjectionObj(GLOBAL::g_procWndInfo.dwProcId, szDll);

					// 需要查找OGCAssist.dll是否在目录进程内;
					if ( GLOBAL::GetModulePath(_T("OGCAssist.dll"), GLOBAL::g_procWndInfo.dwProcId) )
					{
						GLOBAL::g_bHijacted = TRUE;						
					}
					else 
					{						
						GLOBAL::g_bHijacted = g_Injection.InjectDynamicLibrary();
					}
				}
				// 附加窗口句柄;
				FetchOGCWnd();

#pragma region 隐藏窗口;
				HWND hWnd = NULL;
				if ( hWnd = MyFindWindow(_T("TfrmDemo")) )
					::ShowWindow(hWnd, SW_HIDE);

				if ( hWnd = MyFindWindow(_T("TApplication")) )
					::ShowWindow(hWnd, SW_HIDE);
#pragma endregion

#pragma region 进程通信：请求状态;

#pragma endregion
				KillTimer(nIDEvent);
			}			
		}
		break;
	default:
		break;
	}
	CDialogEx::OnTimer(nIDEvent);
}
