⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 poweruseppcdlg.cpp

📁 Calculates power drain for wlan, BT, backlight etc and shows the results. Good to know, what setting
💻 CPP
字号:
// PowerUsePPCDlg.cpp : implementation file
//

#include "stdafx.h"
#include "PowerUsePPC.h"
#include "PowerUsePPCDlg.h"
#include "wirelessdevices.h"
#include "itc50.h"
#pragma comment (lib, "itc50.lib")

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CPowerUsePPCDlg dialog

CPowerUsePPCDlg::CPowerUsePPCDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CPowerUsePPCDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CPowerUsePPCDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32

	m_brush.CreateSolidBrush(RGB(127,127,127));

	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CPowerUsePPCDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPowerUsePPCDlg)
	DDX_Control(pDX, IDC_EDIT1, m_Edit);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CPowerUsePPCDlg, CDialog)
	//{{AFX_MSG_MAP(CPowerUsePPCDlg)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_WM_CTLCOLOR()
	ON_COMMAND(IDR_MENU1, OnMenu1)
	ON_BN_CLICKED(IDC_BTNEXIT, OnBtnexit)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// myThread
BOOL bStopThread=false;
HANDLE hMyThread;
DWORD dwThreadID = 0;

DWORD WINAPI myThread(LPVOID lpParameter){
	//keep device idle thread
	DWORD dwVal=0, BattTimeOut=0, ExtTimeOut=0, WakeTimeOut=0, dwMin=0;
	if (SystemParametersInfo (SPI_GETBATTERYIDLETIMEOUT, 0, &dwVal, 0 /* SPIF_SENDCHANGE */)!=0)
		BattTimeOut=dwVal;
	if (SystemParametersInfo (SPI_GETEXTERNALIDLETIMEOUT, 0, &dwVal, 0 /* SPIF_SENDCHANGE */)!=0)
		ExtTimeOut=dwVal;
	if (SystemParametersInfo (SPI_GETWAKEUPIDLETIMEOUT, 0, &dwVal, 0 /* SPIF_SENDCHANGE */)!=0)
		WakeTimeOut=dwVal;
	if (BattTimeOut>ExtTimeOut)
		dwMin=ExtTimeOut;
	else
		dwMin=BattTimeOut;
	if (WakeTimeOut<dwMin)
		dwMin=WakeTimeOut;
	if (dwMin>600)
		dwMin-=500;
	DEBUGMSG(1, (L"Thread started with dwMin=%u", dwMin));

	//[HKEY_CURRENT_USER\ControlPanel\BackLight]
	//BattAutodimEnabled
	//ACAutodimEnabled
	DWORD dwData=0, cbData=sizeof(DWORD);
	HKEY hKey=NULL;
	if (RegOpenKeyEx(HKEY_CURRENT_USER, L"ControlPanel\\BackLight", 0, 0, &hKey)==ERROR_SUCCESS)
	{
		if (RegSetValueEx(hKey, L"BattAutodimEnabled", NULL, REG_DWORD, (LPBYTE) &dwData, cbData) != ERROR_SUCCESS)
			DEBUGMSG(1, (L"BattAutodimEnabled setting failed\r\n"));
		if (RegSetValueEx(hKey, L"ACAutodimEnabled", NULL, REG_DWORD, (LPBYTE) &dwData, cbData) != ERROR_SUCCESS)
			DEBUGMSG(1, (L"BattAutodimEnabled setting failed\r\n"));
		CloseHandle(hKey);
	}
	DWORD dwBrightness=0;
	ITCGetMaxScreenBrightness(&dwBrightness);
	ITCSetScreenBrightness(dwBrightness);
	ITCSetScreenLight(true, true);

	//SPI_SETBATTERYIDLETIMEOUT
	//SPI_SETEXTERNALIDLETIMEOUT
	if (dwMin==0)
		return -1;

	do{
		SystemIdleTimerReset();
		Sleep(dwMin);
	}while (!bStopThread);
	return 0;
}

/////////////////////////////////////////////////////////////////////////////
// CPowerUsePPCDlg message handlers

BOOL CPowerUsePPCDlg::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
	if (m_menu.LoadMenu(IDR_MENU1)!=0)
		SetMenu(&m_menu);

	CenterWindow(GetDesktopWindow());	// center to the hpc screen
	ShowWindow(SW_SHOWMAXIMIZED);
	ModifyStyleEx(WS_EX_CAPTIONOKBTN,0);	//remove OK

	hMyThread = CreateThread (NULL, 0, myThread, (PVOID)NULL, 0, &dwThreadID);
	CloseHandle (hMyThread);

	// TODO: Add extra initialization here
	if (InitDLL())
	{
		m_bWifiDLL=true;
		GetWDevState(&m_dwWifi, &m_dwPhone, &m_dwBT);
		CString s;
		s.Format(L"Wifi=%u, Phone=%u, BT=%u", m_dwWifi, m_dwPhone, m_dwBT);
		AddText(s);
	}
	else{
		m_bWifiDLL=false;
		m_dwWifi=-1;
		m_dwBT=-1;
		m_dwPhone=-1;
	}

	/*
	DWORD BattCurrent=GetBattCurrent();
	CString txt;
	txt.Format(L"Battery Current: %u", BattCurrent);
	
	AddText(txt);
	*/
	return TRUE;  // return TRUE  unless you set the focus to a control
}

DWORD CPowerUsePPCDlg::GetBattCurrent()
{
	SYSTEM_POWER_STATUS_EX2 pwrstat;
	if (!GetSystemPowerStatusEx2(&pwrstat, sizeof(pwrstat), TRUE))
	{
		DEBUGMSG(1, (L"Couldn't get power state: %u\n",GetLastError()));
		return 0;
	} 
	else
	{
		if (pwrstat.ACLineStatus==AC_LINE_OFFLINE){
			DWORD sampleTime=pwrstat.BatteryAverageInterval;
			Sleep(sampleTime*2);
			GetSystemPowerStatusEx2(&pwrstat, sizeof(pwrstat), TRUE);
			// extract the power status information that you need 
			DEBUGMSG(1, (L"power state: %u\n",pwrstat.BatteryCurrent));
			return pwrstat.BatteryCurrent;
		}
		else
			return 0xBADCAB1E;
	}

}


void CPowerUsePPCDlg::OnButton1() 
{
	DWORD testVal = GetSystemMetrics(SM_CXVSCROLL);
	testVal = GetSystemMetrics(SM_CYMENU);
	HWND hWndTB = ::FindWindow(L"HHTASKBAR", NULL);
	RECT TBrect;
	if (hWndTB!=NULL)
		testVal = ::GetWindowRect(hWndTB, &TBrect);
	CString s;
	// TODO: Add your control notification handler code here
	//Start, switch Radios off
	if (m_bWifiDLL){
		/*
		RADIODEVICES_MANAGED = 1,
		RADIODEVICES_PHONE,
		RADIODEVICES_BLUETOOTH,

		*/
		for (int j=1; j<=3; j++){
			if (SetWDevState( j, 0)==S_OK)
				AddText(L"Radios powered off");
			else
				AddText(L"Radio powering failed");
		}
	}
	else
		AddText(L"Cannot use WifiDLL");

	//Switch backlight off
	if (ITC_ISSUCCESS(ITCSetScreenLight(FALSE, TRUE)))
		AddText(L"Backlight off");
	else
		AddText(L"Backlight off failed");

	DWORD BattCurrent=GetBattCurrent();
	m_BattBase=BattCurrent;
	if (BattCurrent==0xBADCAB1E)
		s=L"Error in GetBattCurrent";
	else
		s.Format(L"Base current: %u", m_BattBase);
	AddText(s);

	//Switch backlight on
	DWORD maxLevel;
	ITCGetMaxScreenBrightness(&maxLevel);
	s.Format(L"Max backlight level is %u", maxLevel);
	AddText(s);
	ITCSetScreenLight(TRUE, TRUE);
	for (int i=0; i<=maxLevel; i++){
		ITCSetScreenBrightness(i);
		BattCurrent=GetBattCurrent();
		if (BattCurrent==0xBADCAB1E)
			s=L"Error in GetBattCurrent";
		else
			s.Format(L"Current for backlight level %u is %u", i, BattCurrent);
		AddText(s);
	}

	s.Format(L"Finished");
	
	AddText(s);
}

void CPowerUsePPCDlg::OnMenu1()
{
	DEBUGMSG(1, (L"Menu1\n"));
}

void CPowerUsePPCDlg::AddText(CString txt)
{
	CString s;
	m_Edit.GetWindowText(s);
	if (s.GetLength()==0){
		CString s2(txt);
		m_Edit.SetWindowText(s2);
	}
	else{
		CString s2 = s + L"\r\n" + txt;
		m_Edit.SetWindowText(s2);
	}
	m_Edit.LineScroll(m_Edit.GetLineCount());
	DEBUGMSG(1, (txt));
	DEBUGMSG(1, (L"\n"));
}

HBRUSH CPowerUsePPCDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
	
	// TODO: Change any attributes of the DC here
	/*
	CString s;
	pWnd->GetWindowText(s);
	DEBUGMSG(1, (L"Window '%s', nCtlColor=%u\n", s, nCtlColor));
	*/
	if (nCtlColor==4)//(s.Compare(L"PowerUsePPC")==0) 
	{
		return m_brush;
	}
	
	// TODO: Return a different brush if the default is not desired
	return hbr;
}

void CPowerUsePPCDlg::OnBtnexit() 
{
	// TODO: Add your control notification handler code here
	bStopThread=true;
	DeinitDLL();
	PostQuitMessage(0);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -