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

#include "pch.h"
#include "CDlgDownCnt.h"

#define IDC_STATIC_DOWNCNT 1001
#define IDC_STATIC_INFO 1002
// CDlgDownCnt 对话框

IMPLEMENT_DYNAMIC(CDlgDownCnt, CBCGPDialog)

CDlgDownCnt::CDlgDownCnt(const std::wstring& str, int downcnt)
	: CBCGPDialog(nullptr, nullptr)
	, infoStr(str)
	, downCnt(downcnt)
{
	EnableVisualManagerStyle();
}

CDlgDownCnt::~CDlgDownCnt()
{
}

void CDlgDownCnt::DoDataExchange(CDataExchange* pDX)
{
	CBCGPDialog::DoDataExchange(pDX);

}


BEGIN_MESSAGE_MAP(CDlgDownCnt, CBCGPDialog)
	ON_WM_TIMER()
END_MESSAGE_MAP()


// CDlgDownCnt 消息处理程序


void CDlgDownCnt::OnTimer(UINT_PTR nIDEvent)
{
	
	downCnt--;

	if (downCnt <= 0) {
		KillTimer(1);
		EndDialog(0);
		
	}

	SetDlgItemInt(IDC_STATIC_DOWNCNT, downCnt);

	CBCGPDialog::OnTimer(nIDEvent);
}


BOOL CDlgDownCnt::OnInitDialog()
{
	CBCGPDialog::OnInitDialog();
	// 获取窗口大小
	CRect rc;
	GetWindowRect(rc);
	// 窗口四周保留32像素边界，然后labelCount、labelInfo纵向排列各占一半高度。
	int margin = globalUtils.ScaleByDPI(32) ;
	int halfHeight = (rc.Height() - 2 * margin) / 2;
	// 设置labelCount的位置
	CRect rcCount = CRect(margin, margin, rc.Width() - margin, margin + halfHeight);
	// 设置labelInfo的位置
	CRect rcInfo = CRect(margin, margin + halfHeight, rc.Width() - margin, rc.Height() - margin);

	if (!labelCount.Create(_T(""), WS_CHILD | WS_VISIBLE | SS_CENTER, rcCount, this, IDC_STATIC_DOWNCNT))
	{
		return FALSE;
	}

	if (!labelInfo.Create(_T(""), WS_CHILD | WS_VISIBLE | SS_CENTER, rcInfo, this, IDC_STATIC_INFO))
	{
		return FALSE;
	}
	m_InfoFont.CreatePointFont(180, L"宋体");
	m_CountFont.CreatePointFont(240, L"宋体");
	labelInfo.SetFont(&m_InfoFont);
	labelCount.SetFont(&m_CountFont);
	labelInfo.SetWindowText(infoStr.c_str());
	SetDlgItemInt(IDC_STATIC_DOWNCNT, downCnt);
	SetTimer(1, 1000, NULL);
	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}

BOOL CDlgDownCnt::CreateDialogTemplate()
{
	
	CRect rect(0, 0, 400, 200);
	CString strCaption = _T("倒计时");

	DLGTEMPLATE* pTemplate = dlgTemplate.CreateTemplate(WS_POPUP | WS_CAPTION | WS_SYSMENU | DS_SETFONT, rect, strCaption);

	if (!InitModalIndirect(pTemplate, NULL))
	{
		return FALSE;
	}

	return TRUE;
}

void runDownCntDlg(const std::wstring& str, int downcnt)
{
	CDlgDownCnt dlg(str, downcnt);
	dlg.CreateDialogTemplate();
	dlg.DoModal();
}