// GetBatterySampleDlg.cpp : implementation file
//

#include "stdafx.h"
#include "GetBatterySample.h"
#include "GetBatterySampleDlg.h"
#include "Winbase.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CGetBatterySampleDlg dialog

CGetBatterySampleDlg::CGetBatterySampleDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CGetBatterySampleDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_BatteryLifePercent = _T("");
	m_BatteryFlag = _T("");
	//m_BatterymAHourConsumed = _T("");
	//m_BatteryVoltage = _T("");
}

void CGetBatterySampleDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT_BatteryLifePercent, m_BatteryLifePercent);
	DDX_Text(pDX, IDC_EDIT_BatteryFlag, m_BatteryFlag);
	//DDX_Text(pDX, IDC_EDIT_BatterymAHourConsumed, m_BatterymAHourConsumed);
	//DDX_Text(pDX, IDC_EDIT_BatteryVoltage, m_BatteryVoltage);
}

BEGIN_MESSAGE_MAP(CGetBatterySampleDlg, CDialog)
#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
	ON_WM_SIZE()
#endif
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_BUTTON_Get, &CGetBatterySampleDlg::OnBnClickedButtonGet)
	ON_BN_CLICKED(IDC_BUTTON_EXIT, &CGetBatterySampleDlg::OnBnClickedButtonExit)
END_MESSAGE_MAP()


// CGetBatterySampleDlg message handlers

BOOL CGetBatterySampleDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here
	m_BatteryLifePercent = _T("80%");
	m_BatteryFlag = _T("Battery Online");
//	m_BatterymAHourConsumed = _T("11");
	//m_BatteryVoltage = _T("3V");
	UpdateData(FALSE);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
void CGetBatterySampleDlg::OnSize(UINT /*nType*/, int /*cx*/, int /*cy*/)
{
	if (AfxIsDRAEnabled())
	{
		DRA::RelayoutDialog(
			AfxGetResourceHandle(), 
			this->m_hWnd, 
			DRA::GetDisplayMode() != DRA::Portrait ? 
			MAKEINTRESOURCE(IDD_GETBATTERYSAMPLE_DIALOG_WIDE) : 
			MAKEINTRESOURCE(IDD_GETBATTERYSAMPLE_DIALOG));
	}
}
#endif


void CGetBatterySampleDlg::OnBnClickedButtonGet()
{
	// TODO: Add your control notification handler code here
    SYSTEM_POWER_STATUS_EX2 sp_ex2;
	SYSTEM_POWER_STATUS_EX  sp;
	WCHAR str1[128] = L"";
	GetSystemPowerStatusEx2(&sp_ex2, sizeof(SYSTEM_POWER_STATUS_EX2), TRUE);

	GetSystemPowerStatusEx(&sp,TRUE);
	if (sp_ex2.BatteryLifePercent == 255)
	{
		m_BatteryLifePercent = _T("");
	}
	else
	{
		wsprintf(str1, L"%d%s", sp_ex2.BatteryLifePercent, _T("%"));
		m_BatteryLifePercent = str1;
	}
	//wsprintf(str1, L"%dV", sp_ex2.BatteryVoltage);
	////m_BatteryVoltage = str1;
	//wsprintf(str1, L"%dmah", sp_ex2.BatterymAHourConsumed);
	//m_BatterymAHourConsumed = str1;
	if(sp.ACLineStatus == 0)
	{
		m_BatteryFlag = _T("Battery Online");
	}
	else if(sp_ex2.BatteryFlag == 0x08)
	{
		m_BatteryFlag = _T("Charing");
	}
	else
	{
		m_BatteryFlag = _T("AC Online");
	}
	UpdateData(FALSE);
}

void CGetBatterySampleDlg::OnBnClickedButtonExit()
{
	// TODO: Add your control notification handler code here
	CDialog::OnCancel();
}
