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

#include "stdafx.h"
#include "SmsManager.h"
#include "DLG_ShowWarning.h"
#include "afxdialogex.h"
#include "MemdcR.h"
#include "MainFrm.h"

// DLG_ShowWarning 对话框

IMPLEMENT_DYNAMIC(DLG_ShowWarning, CDialogEx)

DLG_ShowWarning::DLG_ShowWarning(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_SHOWWARNING, pParent)
{
	m_lCount = 0;
	m_pMainFrame = NULL;
	m_ppDlgWarning = NULL;
}

DLG_ShowWarning::~DLG_ShowWarning()
{
}

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


BEGIN_MESSAGE_MAP(DLG_ShowWarning, CDialogEx)
	ON_WM_CLOSE()
	ON_WM_PAINT()
	ON_WM_LBUTTONDOWN()
	ON_WM_TIMER()
END_MESSAGE_MAP()


// DLG_ShowWarning 消息处理程序


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

	CBitmap   bmpMain;  //用于载入教室图形 
	BITMAP    BitMap;    //用于取位图的大小
	long      lTemp;
	lTemp = bmpMain.LoadBitmap(IDB_S_RECVSMS);  //装入位图
	lTemp = bmpMain.GetBitmap(&BitMap);     //取得位图的大小

	xBmp = BitMap.bmWidth + 2;
	yBmp = BitMap.bmHeight + 2;


	HDC       hScrDC;         // 屏幕和内存设备描述表
							  //为屏幕创建设备描述表
	hScrDC = CreateDC(_T("DISPLAY"), NULL, NULL, NULL);
	//获得屏幕分辨率
	xScrn = GetDeviceCaps(hScrDC, HORZRES);
	yScrn = GetDeviceCaps(hScrDC, VERTRES);

	MoveWindow(xScrn, yScrn, xBmp, yBmp);
	
	CRect rect;
	GetClientRect(&rect);
	::SetWindowPos(m_hWnd, HWND_TOPMOST, 0, 0, rect.Width(), rect.Height() + 20, SWP_NOMOVE);

	SetTimer(1, 100, NULL);

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


void DLG_ShowWarning::OnClose()
{
	KillTimer(1);
	KillTimer(2);
	KillTimer(3);


	if (m_ppDlgWarning)
	{
		*m_ppDlgWarning = NULL;
	}

	CDialogEx::OnClose();
	this->DestroyWindow();
	delete this;
}

void DLG_ShowWarning::SetParam(CString strText, CWnd * pMainFrame, DLG_ShowWarning* * pdlgWarning)
{
	m_strShowText = strText;
	m_pMainFrame = pMainFrame;
	m_ppDlgWarning = pdlgWarning;
}

void DLG_ShowWarning::OnPaint()
{
	CPaintDC dc(this); // device context for painting

	CMemDC_R memDC(&dc);

	CDC  pDispDC;
	pDispDC.CreateCompatibleDC(&memDC);
	CBitmap   bmpMain;  //用于载入教室图形 
	CBitmap * OldBmp;    //记录旧的Bitmap
	BITMAP    BitMap;    //用于取位图的大小
	long      lTemp;
	lTemp = bmpMain.LoadBitmap(IDB_S_RECVSMS);  //装入位图
	lTemp = bmpMain.GetBitmap(&BitMap);     //取得位图的大小
											//HBITMAP bmp;
											//bmp = (HBITMAP)LoadImage( NULL,"Main.bmp",IMAGE_BITMAP,0,0,LR_DEFAULTCOLOR|LR_LOADFROMFILE);
											//GetObject(bmp, sizeof(BitMap), &BitMap);
											//CBitmap * pbmp;
											//pbmp = CBitmap::FromHandle( bmp );

											//OldBmp = pDispDC.SelectObject( &bmpMain );//选入位图
	OldBmp = pDispDC.SelectObject(&bmpMain);//选入位图
	memDC.BitBlt(0, 0, BitMap.bmWidth, BitMap.bmHeight, &pDispDC, 0, 0, SRCCOPY);
	memDC.SetBkMode(TRANSPARENT);

	CFont* pOldfont = NULL;
	CFont font1;
		font1.CreateFont(-MulDiv(10, dc.GetDeviceCaps(LOGPIXELSY), 24),
			0, 0, 0, FW_NORMAL, 0, 0, 0, GB2312_CHARSET,
			OUT_STROKE_PRECIS, CLIP_STROKE_PRECIS, DRAFT_QUALITY,
			VARIABLE_PITCH | FF_SWISS, _T("黑体"));

	pOldfont = dc.SelectObject(&font1);

	//memDC.TextOut(10, 50, m_strShowText);
	RECT rect;
	rect.left = 10; rect.top = 50;
	rect.right = 200; rect.bottom = 200;
	memDC.SetTextColor(RGB(255, 0, 0));
	memDC.DrawText(m_strShowText, &rect, DT_LEFT | DT_WORDBREAK | DT_EDITCONTROL);

	if (pOldfont)
		dc.SelectObject(pOldfont);
	font1.DeleteObject();



	//恢复原来的BMP
	pDispDC.SelectObject(OldBmp);
	//CDialog::OnPaint();
}


void DLG_ShowWarning::OnLButtonDown(UINT nFlags, CPoint point)
{
	//KillTimer(1);
	//KillTimer(2);
	//SetTimer(3, 30, NULL);  //启动降时钟

	if (m_pMainFrame)
	{
		((CMainFrame *)m_pMainFrame)->ShowCheckUpDlg();
	}
	CDialogEx::OnLButtonDown(nFlags, point);
}


void DLG_ShowWarning::OnTimer(UINT_PTR nIDEvent)
{
	switch (nIDEvent)
	{
	case 1:
		m_lCount += 5;
		if (m_lCount >= yBmp + 50)  //升至上限
		{
			KillTimer(1);
			//SetTimer(2, 5000, NULL);   //5秒钟后退出,改成点击后下降
			return;
		}
		MoveWindow(xScrn - xBmp - 20, yScrn - m_lCount, xBmp, yBmp);
		break;
	case 2:
		KillTimer(2);
		SetTimer(3, 70, NULL);  //启动降时钟
		break;
	case 3:
		m_lCount -= 5;
		if (m_lCount <= 0)  //降至上限
		{
			KillTimer(3);
			this->OnClose();
			return;
		}
		MoveWindow(xScrn - xBmp - 20, yScrn - m_lCount, xBmp, yBmp);
		break;
	}

	CDialogEx::OnTimer(nIDEvent);
}