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

#include "stdafx.h"
#include "CombineSubjectDrawTest.h"
#include "MenuZoomSetting_Dlg.h"
#include "afxdialogex.h"


// CMenuZoomSetting_Dlg 对话框

IMPLEMENT_DYNAMIC(CMenuZoomSetting_Dlg, CDialogEx)

CMenuZoomSetting_Dlg::CMenuZoomSetting_Dlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(IDD, pParent)
	, m_flMinZoomValue(0)
	, m_flMaxZoomValue(0)
	, m_flDBZoomValue(0)
{
	m_pControl = NULL;
}

CMenuZoomSetting_Dlg::~CMenuZoomSetting_Dlg()
{
}

void CMenuZoomSetting_Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT_MIN, m_flMinZoomValue);
	DDX_Text(pDX, IDC_EDIT_MAX, m_flMaxZoomValue);
	DDX_Control(pDX, IDC_SLIDER1, m_slider_range);
	DDX_Text(pDX, IDC_EDIT_DBZOOM_VALUE, m_flDBZoomValue);
	DDX_Control(pDX, IDC_COMBO_LINE, m_line_cbx);
	DDX_Control(pDX, IDC_MFCCOLORBUTTON_COLOR, m_btn_color);
	DDX_Control(pDX, IDC_COMBO_VK_KEY, m_ctrlLZoomKey);
}


BEGIN_MESSAGE_MAP(CMenuZoomSetting_Dlg, CDialogEx)

	ON_BN_CLICKED(IDC_BUTTON_SETTING, &CMenuZoomSetting_Dlg::OnBnClickedButtonSetting)
	ON_BN_CLICKED(IDC_BUTTON_AUTO_RESIZE, &CMenuZoomSetting_Dlg::OnBnClickedButtonAutoResize)
	ON_BN_CLICKED(IDOK, &CMenuZoomSetting_Dlg::OnBnClickedOk)
	ON_WM_HSCROLL()
	ON_BN_CLICKED(IDC_CHECK_DBZOOM, &CMenuZoomSetting_Dlg::OnBnClickedCheckDbzoom)
	ON_BN_CLICKED(IDC_BUTTON_SETTING2, &CMenuZoomSetting_Dlg::OnBnClickedButtonSetting2)
	ON_BN_CLICKED(IDC_BUTTON_REAL_SIZE, &CMenuZoomSetting_Dlg::OnBnClickedButtonRealSize)
	ON_BN_CLICKED(IDC_CHECK_ENABLE, &CMenuZoomSetting_Dlg::OnBnClickedCheckEnable)
	ON_CBN_SELCHANGE(IDC_COMBO_LINE, &CMenuZoomSetting_Dlg::OnCbnSelchangeComboLine)
	ON_BN_CLICKED(IDC_MFCCOLORBUTTON_COLOR, &CMenuZoomSetting_Dlg::OnBnClickedMfccolorbuttonColor)
	ON_CBN_SELCHANGE(IDC_COMBO_VK_KEY, &CMenuZoomSetting_Dlg::OnCbnSelchangeComboVkKey)
END_MESSAGE_MAP()


// CMenuZoomSetting_Dlg 消息处理程序


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

	if (m_pControl == NULL)
		return TRUE;

	float flMin = 0;
	float flMax = 0;
	float flCur = 0;
	m_pControl->GetZoomRange(flMin, flMax);
	m_pControl->GetImageZoomValue(flCur);

	UpdateZoomRangeToUI(flMin, flMax, flCur);

	if (m_pControl->GetImageCount() <= 0)
	{
		m_slider_range.EnableWindow(FALSE);
	}



	CButton* pBtn = (CButton*)GetDlgItem(IDC_CHECK_DBZOOM);
	pBtn->SetCheck(m_pControl->IsEnableDoubleClickZoom());
	m_pControl->GetDoubleClickZoomValue(m_flDBZoomValue);

	OnBnClickedCheckDbzoom();


	CString strValue;
	int nItem = 0;
	for (int i = 1; i < 5; i++)
	{
		strValue.Format(_T("%d"), i);
		nItem = m_line_cbx.AddString(strValue);
		m_line_cbx.SetItemData(nItem, i);
	}
	m_line_cbx.SetCurSel(0);

	int nWidth = 1;
	m_pControl->GetMouseRectZoomLineWidth(nWidth);

	for (int i = 1; i < m_line_cbx.GetCount(); i++)
	{
		if (m_line_cbx.GetItemData(i) == nWidth)
		{
			m_line_cbx.SetCurSel(i);
			break;
		}
	}

	//
	CMFCColorButton * pColorBtn = (CMFCColorButton*)GetDlgItem(IDC_MFCCOLORBUTTON_COLOR);
	COLORREF _color;
	m_pControl->GetMouseRectZoomLineColor(_color);
	pColorBtn->SetColor(_color);
	//
	CButton* pCheck = (CButton*)GetDlgItem(IDC_CHECK_ENABLE);
	pCheck->SetCheck(m_pControl->IsEnableMouseRectZoom());



	int nIdx = m_ctrlLZoomKey.AddString(_T("Control"));
	m_ctrlLZoomKey.SetItemData(nIdx, VK_CONTROL);
	nIdx = m_ctrlLZoomKey.AddString(_T("Shift"));
	m_ctrlLZoomKey.SetItemData(nIdx, VK_SHIFT);
	OnBnClickedCheckEnable();

	
	UINT nKey = 0;
	m_pControl->GetMouseZoomRectCorpKeyValue(nKey);
	for (int i = 0; i < m_ctrlLZoomKey.GetCount(); i++)
	{
		if (m_ctrlLZoomKey.GetItemData(i) == nKey)
		{
			m_ctrlLZoomKey.SetCurSel(i);
			break;
		}
	}

	UpdateData(FALSE);
	
	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}

void CMenuZoomSetting_Dlg::UpdateZoomRangeToUI(float flMin,float flMax,float flCurrent)
{
	m_flMinZoomValue = flMin;
	m_flMaxZoomValue = flMax;


	float flTempMin = m_flMinZoomValue * 100.0;
	float flTempMax = m_flMaxZoomValue * 100.0;



	CString strValue;
	strValue.Format(_T("最小值:%.2f"), m_flMinZoomValue);
	GetDlgItem(IDC_STATIC_MIN)->SetWindowText(strValue);

	strValue.Format(_T("最大值:%.2f"), m_flMaxZoomValue);
	GetDlgItem(IDC_STATIC_MAX)->SetWindowText(strValue);

	m_slider_range.SetRange((int)flTempMin, (int)flTempMax);

	if (flCurrent < m_flMinZoomValue || flCurrent> m_flMaxZoomValue)
	{
		GetDlgItem(IDC_STATIC_CUR)->SetWindowText(_T("当前值: 无图像"));
	}
	else
	{
		strValue.Format(_T("当前值:%.2f"), flCurrent);
		GetDlgItem(IDC_STATIC_CUR)->SetWindowText(strValue);
	}


	float tempCur = flCurrent*100.0;
	m_slider_range.SetPos((int)tempCur);

	UpdateData(FALSE);


	
}


void CMenuZoomSetting_Dlg::OnBnClickedButtonSetting()
{
	if (FALSE == UpdateData(TRUE))
	{
		return;
	}
	float flMin = 0;
	float flMax = 0;
	m_pControl->GetZoomRange(flMin, flMax);
	if (m_flMinZoomValue <= 0)
	{
		
		MessageBox(_T("缩放最小值不能<=0"),_T("提示"),MB_ICONINFORMATION);
		m_flMinZoomValue = flMin;
		m_flMaxZoomValue = flMax;
		UpdateData(FALSE);

		return;
	}
	if (m_flMaxZoomValue > 10)
	{
		MessageBox(_T("缩放最大值不能 >0"), _T("提示"), MB_ICONINFORMATION);

		m_flMinZoomValue = flMin;
		m_flMaxZoomValue = flMax;
		UpdateData(FALSE);
		return;
	}

	m_pControl->SetZoomRange(m_flMinZoomValue, m_flMaxZoomValue);

	
	
	float flCur = 0;
	m_pControl->GetZoomRange(flMin, flMax);
	m_pControl->GetImageZoomValue(flCur);

	UpdateZoomRangeToUI(flMin, flMax, flCur);


}


void CMenuZoomSetting_Dlg::OnBnClickedButtonAutoResize()
{
	// TODO: 在此添加控件通知处理程序代码
	m_pControl->ShowImageAdjustByVisibleRectMode();
}


void CMenuZoomSetting_Dlg::OnBnClickedOk()
{
	// TODO: 在此添加控件通知处理程序代码
	CDialogEx::OnOK();
}


void CMenuZoomSetting_Dlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
	if (pScrollBar->GetSafeHwnd() == m_slider_range.GetSafeHwnd())
	{
		int nPos = m_slider_range.GetPos();
		float flZoomValue = (float)nPos / 100.0;
		m_pControl->SetImageZoomValue(flZoomValue);
	}
	CDialogEx::OnHScroll(nSBCode, nPos, pScrollBar);
}


void CMenuZoomSetting_Dlg::OnBnClickedCheckDbzoom()
{
	CButton* pBtn = (CButton*)GetDlgItem(IDC_CHECK_DBZOOM);
	
	GetDlgItem(IDC_EDIT_DBZOOM_VALUE)->EnableWindow(pBtn->GetCheck());

	m_pControl->EnableDoubleClickZoom(pBtn->GetCheck());
}


void CMenuZoomSetting_Dlg::OnBnClickedButtonSetting2()
{
	if (UpdateData(TRUE) == FALSE)
		return;
	float flMin = 0;
	float flMax = 0;
	m_pControl->GetZoomRange(flMin, flMax);
	if (m_flDBZoomValue < flMin)
	{

		MessageBox(_T("缩放最小值必须在缩放范围内"), _T("提示"), MB_ICONINFORMATION);
		m_pControl->GetDoubleClickZoomValue(m_flDBZoomValue);
		UpdateData(FALSE);

		return;
	}
	if (m_flDBZoomValue > flMax)
	{
		MessageBox(_T("缩放最大值必须在缩放范围内"), _T("提示"), MB_ICONINFORMATION);
		m_pControl->GetDoubleClickZoomValue(m_flDBZoomValue);
		UpdateData(FALSE);
		return;
	}

	m_pControl->SetDoubleClickZoomValue(m_flDBZoomValue);
}
void CMenuZoomSetting_Dlg::OnBnClickedButtonRealSize()
{
	m_pControl->ShowImageWidthRealSize();
}

void CMenuZoomSetting_Dlg::OnBnClickedCheckEnable()
{
	CButton* pCheck = (CButton*)GetDlgItem(IDC_CHECK_ENABLE);
	m_line_cbx.EnableWindow(pCheck->GetCheck());
	m_btn_color.EnableWindow(pCheck->GetCheck());

	m_ctrlLZoomKey.EnableWindow(pCheck->GetCheck());

	m_pControl->EnableMouseRectZoom(pCheck->GetCheck());
}


void CMenuZoomSetting_Dlg::OnCbnSelchangeComboLine()
{
	// TODO: 在此添加控件通知处理程序代码
	int nSel = m_line_cbx.GetCurSel();
	int nWidth = m_line_cbx.GetItemData(nSel);
	m_pControl->SetMouseRectZoomLineWidth(nWidth);
}


void CMenuZoomSetting_Dlg::OnBnClickedMfccolorbuttonColor()
{
	m_pControl->SetMouseRectZoomLineColor(m_btn_color.GetColor());


}

void CMenuZoomSetting_Dlg::OnCbnSelchangeComboVkKey()
{
	int nSel = m_ctrlLZoomKey.GetCurSel();
	if (nSel < 0)
		return;

	m_pControl->SetMouseZoomRectCorpKeyValue(m_ctrlLZoomKey.GetItemData(nSel));
}
