C++ создать приложение, которое меняет яркость экрана в зависимости от времени на компьютере (чтобы ночью не было ярко)
Возникла идея такого приложения, однако не знаю как такое сделать. Нашел в интернете код , но при его запуске вылазит
(у меня ноутбук)
Сам код:
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <windows.h>
#include <cmath>
#include <ctime>
#include <highlevelmonitorconfigurationapi.h>
#pragma comment(lib, "Dxva2.lib")
using namespace std;
int main()
{
HWND hWnd = GetConsoleWindow();
HRESULT hError = 0;
DWORD error = 0;
HMONITOR hMonitor = MonitorFromWindow(hWnd, MONITOR_DEFAULTTOPRIMARY);
if (hMonitor)
{
DWORD MaxBrightness, MinBrightness, CurrentBrightness;
PHYSICAL_MONITOR phm[1];
if (GetPhysicalMonitorsFromHMONITOR(hMonitor, 1, phm))
{
if (GetMonitorBrightness(phm[0].hPhysicalMonitor, &MinBrightness, &CurrentBrightness, &MaxBrightness))
{
time_t currentSeconds = time(NULL);
tm* timeInfo = localtime(¤tSeconds);
UINT currentHours = timeInfo->tm_hour;
DWORD NewBrightness = 0;
if (currentHours <= 6 || currentHours >= 22)
NewBrightness = round(MaxBrightness / 2 / 2);
else if (currentHours > 6 && currentHours <= 12)
NewBrightness = round(MaxBrightness / 2);
else
NewBrightness = 80;
SetMonitorBrightness(phm[0].hPhysicalMonitor, NewBrightness);
cout << "[INFO] Old brightness: " << CurrentBrightness << endl;
cout << "[INFO] New brightness: " << NewBrightness << endl;
}
else
error = GetLastError();
hError = HRESULT_FROM_WIN32(error);
cout << "[ERROR] Cannot get monitor brightness!!! - Error code: " << hex << hError << "\n";
}
else
{
error = GetLastError();
hError = HRESULT_FROM_WIN32(error);
cout << "[ERROR] Cannot get physical monitor!!! - Error code: " << hError << "\n";
}
}
else
cout << "[ERROR] Cannot get monitor handle!!!\n";
return 0;
}