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

#include "stdafx.h"
#include "OGCAssistTool.h"
#include "PageDebug.h"
#include "Injection.h"

// 注入;
extern CInjection g_Injection;
// OGC Tool无序句柄;
extern HWND g_hWnd_SN;
extern HWND g_hWnd_SN_Combobox;
extern HWND g_hWnd_Tester;
extern HWND g_hWnd_FWVersion;
extern HWND g_hWnd_Channel;
extern BOOL StartOGCTool();
// CDlgDebug 对话框

IMPLEMENT_DYNAMIC(CPageDebug, CDialogEx)

CPageDebug::CPageDebug(CWnd* pParent /*=NULL*/)
	: CDialogEx(CPageDebug::IDD, pParent)
{

}

CPageDebug::~CPageDebug()
{
}

void CPageDebug::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, LIST_DEBUG_DATA, m_ctrlList);
}


BEGIN_MESSAGE_MAP(CPageDebug, CDialogEx)
	ON_WM_CTLCOLOR()
	ON_BN_CLICKED(BTN_SHOW_WND, &CPageDebug::OnBnClickedShowWnd)
	ON_BN_CLICKED(BTN_HIDE_WND, &CPageDebug::OnBnClickedHideWnd)
	ON_BN_CLICKED(CHECK_HIJACK, &CPageDebug::OnBnClickedHijack)
	ON_BN_CLICKED(CHECK_RESUME_HIJACKING, &CPageDebug::OnBnClickedResumeHijacking)
	ON_BN_CLICKED(BTN_TEST, &CPageDebug::OnBnClickedTest)
END_MESSAGE_MAP()


// CDlgDebug 消息处理程序

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

	// TODO:  在此添加额外的初始化
	//SetBackgroundColor(RGB(255,255,255));
	SetBackgroundColor(RGB(200,200,200));
	InitListCtrl();
	return TRUE;  // return TRUE unless you set the focus to a control
	// 异常: OCX 属性页应返回 FALSE
}

HBRUSH CPageDebug::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
	HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);

	// TODO:  在此更改 DC 的任何属性
	switch ( pWnd->GetDlgCtrlID() ) 
	{
	case CHECK_LOCK:
	case CHECK_HIJACK:
	case CHECK_RESUME_HIJACKING:
		{
#if 1 // 透明控件处理;
			pDC->SetBkMode(TRANSPARENT);

			CRect rc;
			pWnd->GetWindowRect(&rc);
			ScreenToClient(&rc);

			CDC* dc = GetDC();
			pDC->BitBlt(0,0,rc.Width(),rc.Height(),dc,rc.left,rc.top,SRCCOPY);  //把父窗口背景图片先画到按钮上

			ReleaseDC(dc);
			hbr = (HBRUSH) ::GetStockObject(NULL_BRUSH);
#endif
		}
		break;
	default:
		break; 
	}

	// TODO:  如果默认的不是所需画笔，则返回另一个画笔
	return hbr;
}

void CPageDebug::InitListCtrl()
{
	m_ctrlList.InsertColumn( 0, _T("序号"));
	m_ctrlList.SetColumnWidth( 0, 50 );
	m_ctrlList.InsertColumn( 1, _T("R"));
	m_ctrlList.SetColumnWidth( 1, 100 );
	m_ctrlList.InsertColumn( 2, _T("G"));
	m_ctrlList.SetColumnWidth( 2, 100);
	m_ctrlList.InsertColumn( 3, _T("B"));
	m_ctrlList.SetColumnWidth( 3, 100);
	m_ctrlList.InsertColumn( 4, _T("▲E实际值"));
	m_ctrlList.SetColumnWidth(4, 100);
}

HWND 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 CPageDebug::OnBnClickedShowWnd()
{
	// TODO: 在此添加控件通知处理程序代码
	HWND hWnd = NULL;
	if ( hWnd = MyFindWindow(_T("TfrmDemo")) )
		::ShowWindow(hWnd, SW_SHOW);

	if ( hWnd = MyFindWindow(_T("TApplication")) )
		::ShowWindow(hWnd, SW_SHOW);
}

void CPageDebug::OnBnClickedHideWnd()
{
	// TODO: 在此添加控件通知处理程序代码
	HWND hWnd = NULL;
	if ( hWnd = MyFindWindow(_T("TfrmDemo")) )
		::ShowWindow(hWnd, SW_HIDE);

	if ( hWnd = MyFindWindow(_T("TApplication")) )
		::ShowWindow(hWnd, SW_HIDE);
}

void CPageDebug::OnBnClickedHijack()
{
	// TODO: 在此添加控件通知处理程序代码
}

void CPageDebug::OnBnClickedResumeHijacking()
{
	// TODO: 在此添加控件通知处理程序代码
}

void CPageDebug::OnBnClickedTest()
{
	// 1、执行一次Connect按钮事件;
	//// 1.1、Connect成功后, 目标进程会返回消息;
	//// 1.2、若没有消息返回，需要判断是否在程序启动前已注入过并且Connect过。
	//// 1.3、要避免2的情况，主进程应该每次启动时，都向目标进程发送消息获取按钮状态。
	// 2、读取SN长度,设置一次SN长度下拉框;
	// 3、将SN赋值到目标进程SN编辑框;
	// 4、执行Go函数;
	// 5、劫持的Go函数未返回消息时,Test按钮和SN编辑框禁止输入;
	switch ( StartOGCTool() )
	{
	case -1:
		// 退出新开;
		break;
	case 0:
		// 新开失败;
		break;
	case 1:
		// 新开成功;
		{
			AfxGetApp()->GetMainWnd()->SetTimer(0, 500, NULL);
			MessageBox(_T("OGC程序启动中，请稍后再尝试，谢谢！"), _T("提示"), MB_OK|MB_ICONWARNING);
			// 向目标发消息，请求获取状态信息;
		}
		break;
	case 2:
		// 已存在进程;
		{

		}
		break;
	default:
		break;
	}
}
