笔记本屏幕 亮度 背光调节 工具 c++ 用来解决亮度调节功能键失效问题

news/2025/1/31 12:55:16/

某次windows10 大更新,坑啊,调节亮度的快捷键失效了,多次卸载集显驱动和重新安装没有解决,无奈使用电源管理里面的亮度滑竿几个月后,更新了win10 1803, 本想能解决这个问题,然而。。。无奈只能自力更生,通过其他办法解决问题

一开始看到的是 Monitor Monitor Configuration Functions ,
https://blog.csdn.net/tody_guo/article/details/16898749
https://msdn.microsoft.com/en-us/library/ms775232(v=vs.85).aspx
估计微软停止维护这套api很久了,获取的 physical monitor handle 总是为0,此法不行
然后找到一个瞎眼的调gammer。。。。
最后找到这个 https://blog.csdn.net/tody_guo/article/details/30255233
获得最终解决办法

本文工具 核心代码以及思路 均来自
https://blog.csdn.net/tody_guo/article/details/30255233

核心原理是 使用 c++连接 wmi 服务,进而修改 笔记本屏幕 lcd 背光亮度
wmi 接口路径
调节亮度类接口
root\wmi\WmiMonitorBrightnessMethods
获取当前亮度类接口
root\wmi\WmiMonitorBrightness

使用和细节:
因为无法使用fn当作热键,所以我使用 shift + alt 来代替
此程序目前硬编码为
shift + alt + f8 降低 5% 亮度
shift + alt + f9 提升 5% 亮度
可以在 第252行 和 第253行 修改硬编码的热键
在第184行修改硬编码 一阶亮度值

结束程序请使用 任务管理器直接结束
程序使用单例模式
程序没有窗口,请在任务管理器里面检查程序是否启动成功

vs2017工程
微云:https://share.weiyun.com/5cluRgV

完整源代码如下

#include <iostream>
#include <string>
#include <windows.h>
#include <objbase.h>
#include <wbemidl.h>
#include <comdef.h>
#include <mutex>#pragma comment(lib, "wbemuuid.lib")
#pragma comment(lib, "comsuppw.lib")using namespace std;// wmi namespace
IWbemLocator  *wmiLocator = NULL;
IWbemServices *wmiNamespace = 0;
BSTR wmiPath = SysAllocString(L"root\\wmi");
HRESULT hr = S_OK;// brightnessAdjustmentClass
IWbemClassObject *brightnessAdjustmentClass = NULL;
IWbemClassObject *brightnessAdjustmentClassInstant = NULL;
IWbemClassObject *brightnessAdjustmentClassObject = NULL;
IWbemClassObject *brightnessAdjustmentMethodInstant = NULL;
IEnumWbemClassObject *brightnessAdjustmentClassEnum = NULL;BSTR brightnessAdjustmentClassPath = SysAllocString(L"WmiMonitorBrightnessMethods");
BSTR brightnessAdjustmentMethodName = SysAllocString(L"WmiSetBrightness");
BSTR brightnessAdjustmentMethodArgName1 = SysAllocString(L"Timeout");
BSTR brightnessAdjustmentMethodArgName2 = SysAllocString(L"Brightness");
BSTR brightnessAdjustmentClassQuery = SysAllocString(L"Select * from WmiMonitorBrightnessMethods");// brightnessClass
IEnumWbemClassObject *brightnessClassEnum = NULL;
//IWbemClassObject *brightnessClass = NULL;
IWbemClassObject *brightnessClassObject = NULL;BSTR brightnessClassPath = SysAllocString(L"WmiMonitorBrightness");
BSTR brightnessVariableName = SysAllocString(L"CurrentBrightness");
BSTR brightnessQuery = SysAllocString(L"Select * from WmiMonitorBrightness");int Init()
{// Initialize COM and connect up to CIMOM  hr = CoInitialize(0);if (FAILED(hr))return -1;hr = CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL);if (FAILED(hr))return -1;hr = CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID *)&wmiLocator);if (FAILED(hr))return -1;hr = wmiLocator->ConnectServer(wmiPath, NULL, NULL, NULL, 0, NULL, NULL, &wmiNamespace);if (hr != WBEM_S_NO_ERROR)return -1;hr = CoSetProxyBlanket(wmiNamespace, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, NULL, RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE);if (hr != WBEM_S_NO_ERROR)return -1;// query variable enumhr = wmiNamespace->ExecQuery(_bstr_t(L"WQL"), brightnessAdjustmentClassQuery, WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &brightnessAdjustmentClassEnum);if (hr != WBEM_S_NO_ERROR)return -1;/*hr = wmiNamespace->ExecQuery(_bstr_t(L"WQL"), brightnessQuery, WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &brightnessClassEnum);if (hr != WBEM_S_NO_ERROR)return -1;*/// get class objULONG ret;hr = brightnessAdjustmentClassEnum->Next(WBEM_INFINITE, 1, &brightnessAdjustmentClassObject, &ret);if (hr != WBEM_S_NO_ERROR)return -1;hr = wmiNamespace->GetObject(brightnessAdjustmentClassPath, 0, NULL, &brightnessAdjustmentClass, NULL);if (hr != WBEM_S_NO_ERROR)return -1;// get brightness adjust method instanthr = brightnessAdjustmentClass->GetMethod(brightnessAdjustmentMethodName, 0, &brightnessAdjustmentClassInstant, NULL);if (hr != WBEM_S_NO_ERROR)return -1;hr = brightnessAdjustmentClassInstant->SpawnInstance(0, &brightnessAdjustmentMethodInstant);if (hr != WBEM_S_NO_ERROR)return -1;return 0;
}int GetBrightness()
{// 每次都需要查询if (brightnessClassEnum) brightnessClassEnum->Release();if (brightnessClassObject) brightnessClassObject->Release();HRESULT hr = wmiNamespace->ExecQuery(_bstr_t(L"WQL"), brightnessQuery, WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &brightnessClassEnum);if (hr != WBEM_S_NO_ERROR)return -1;DWORD ret;hr = brightnessClassEnum->Next(WBEM_INFINITE, 1, &brightnessClassObject, &ret);if (hr != WBEM_S_NO_ERROR)return -1;// 获得亮度值VARIANT brightnessValue;VariantInit(&brightnessValue);hr = brightnessClassObject->Get(brightnessVariableName, 0, &brightnessValue, 0, 0);if (hr != WBEM_S_NO_ERROR)return -1;return brightnessValue.uintVal;
}int SetBrightness(int brightness)
{// 亮度范围是 [0, 100]brightness = min(max(brightness, 0), 100);// write brightness adjust paramsVARIANT var;VariantInit(&var);V_VT(&var) = VT_BSTR;// write timeout paramV_BSTR(&var) = SysAllocString(L"0");hr = brightnessAdjustmentMethodInstant->Put(brightnessAdjustmentMethodArgName1, 0, &var, CIM_UINT32);if (hr != WBEM_S_NO_ERROR)return -1;// write brightness paramV_BSTR(&var) = SysAllocString(to_wstring(brightness).c_str());hr = brightnessAdjustmentMethodInstant->Put(brightnessAdjustmentMethodArgName2, 0, &var, CIM_UINT8);VariantClear(&var);if (hr != WBEM_S_NO_ERROR)return -1;// 获取 method instant path 和 executeVARIANT pathVariable;VariantInit(&pathVariable);hr = brightnessAdjustmentClassObject->Get(_bstr_t(L"__PATH"), 0, &pathVariable, NULL, NULL);if (hr != WBEM_S_NO_ERROR)return -1;hr =wmiNamespace->ExecMethod(pathVariable.bstrVal, brightnessAdjustmentMethodName, 0, NULL, brightnessAdjustmentMethodInstant, NULL, NULL);VariantClear(&pathVariable);if (hr != WBEM_S_NO_ERROR)return -1;return 0;
}void Cleanup()
{// 清理SysFreeString(wmiPath);SysFreeString(brightnessAdjustmentClassPath);SysFreeString(brightnessAdjustmentMethodName);SysFreeString(brightnessAdjustmentMethodArgName1);SysFreeString(brightnessAdjustmentMethodArgName2);SysFreeString(brightnessAdjustmentClassQuery);SysFreeString(brightnessClassPath);SysFreeString(brightnessVariableName);SysFreeString(brightnessQuery);brightnessAdjustmentClass->Release();brightnessAdjustmentMethodInstant->Release();brightnessAdjustmentClassInstant->Release();brightnessAdjustmentClassObject->Release();brightnessAdjustmentClassEnum->Release();//if (brightnessClass) brightnessClass->Release();brightnessClassObject->Release();brightnessClassEnum->Release();wmiNamespace->Release();wmiLocator->Release();CoUninitialize();
}// 亮度进阶值
const int incrementValue = 5;void IncreaseBrightness()
{// 提升一阶亮度int i = GetBrightness();SetBrightness(i + incrementValue);
}
void ReduceBrightness()
{// 降低一阶亮度int i = GetBrightness();SetBrightness(i - incrementValue);
}ATOM hotKeyId1, hotKeyId2;LRESULT __stdcall WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{switch (Msg){case WM_DESTROY:PostQuitMessage(0);break;case WM_HOTKEY:if (wParam == hotKeyId1)PostMessageA(0, WM_USER + 1, 0, 0);else if (wParam == hotKeyId2)PostMessageA(0, WM_USER + 2, 0, 0);break;}return DefWindowProcA(hWnd, Msg, wParam, lParam);
}int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{// 全局唯一auto m = CreateMutexA(NULL, TRUE, "{CAF16FA1-1E0C-419B-9D45-99B5719ED318}");if (ERROR_ALREADY_EXISTS == GetLastError())return -1;int r = Init();if (r != 0)return -1;//创建隐藏窗口,仅用于捕获热键WNDCLASSEX wcex;memset(&wcex, 0, sizeof(wcex));wcex.cbSize     = sizeof(wcex);wcex.style      = 0;wcex.lpfnWndProc= WndProc;wcex.cbClsExtra = 0;wcex.cbWndExtra = 0;wcex.hInstance  = GetModuleHandleA(0);wcex.hIcon      = 0;wcex.hCursor    = 0;wcex.hbrBackground  = 0;wcex.lpszMenuName   = 0;wcex.lpszClassName  = "test";if (RegisterClassExA(&wcex) == FALSE)return -1;HWND hwnd = CreateWindowA("test", "", WS_EX_NOACTIVATE, 0, 0, 0, 0, 0, 0, GetModuleHandleA(0), 0);ShowWindow(hwnd, SW_HIDE);//注册热键hotKeyId1 = GlobalAddAtom("MonitorBrightnessAdjuster1") - 0xc000;hotKeyId2 = GlobalAddAtom("MonitorBrightnessAdjuster2") - 0xc000;int b = RegisterHotKey(hwnd, hotKeyId1, MOD_SHIFT | MOD_ALT, VK_F8);b = RegisterHotKey(hwnd, hotKeyId2, MOD_SHIFT | MOD_ALT, VK_F9);MSG msg;while (GetMessageA(&msg, 0, 0, 0)){TranslateMessage(&msg);DispatchMessageW(&msg);if (msg.message == WM_USER + 1)ReduceBrightness();else if (msg.message == WM_USER + 2)IncreaseBrightness();}// 一般不会到达这里,一般是直接任务管理器结束此程序// 清理UnregisterHotKey(hwnd, hotKeyId1);UnregisterHotKey(hwnd, hotKeyId2);GlobalDeleteAtom(hotKeyId1);GlobalDeleteAtom(hotKeyId2);Cleanup();return 0;
}

http://www.ppmy.cn/news/343355.html

相关文章

Qt调节电脑屏幕亮度

文章目录 前言1.CGammaRamp.h2.CGammaRamp.cpp3.MainWindow.h4.MainWindow.cpp4.效果展示(**尝试了GIF、录屏、截图都没法记录屏幕亮度变化的画面,这里只演示界面**)5.gdi32.dll电脑API下载 前言 电脑没找到调节亮度在哪里,网上查找了一些资料模仿写了一个简单的,记录一下方便…

方法及问题整理--调整屏幕亮度

由于最近碰到了无法调整屏幕亮度的问题&#xff0c;试验了各个方法&#xff0c;最后顺便整理一下~ 通过“电源选项”设置 一、打开控制面板&#xff0c;将右上角的查看方式切换为“大图标”&#xff0c;点击“电源选项”。 二、窗口底部有“屏幕亮度”条&#xff0c;通过拉动…

机器视觉中的光源选型及打光方案分析

2.2.1 光源选择标准 光源的选择标准如下&#xff1a; &#xff08;1&#xff09;光源均匀性要好&#xff0c;在有效的照射范围内&#xff0c;灰度值标准差要小&#xff1b; &#xff08;2&#xff09;具有较宽的光谱范围&#xff0c;可以对不同材料的物体进行检测&#xff1…

计算机xp怎么调整亮度,[如何调整计算机屏幕的亮度] xp系统如何调整屏幕的亮度?屏幕亮度调整方法的详细说明...

76836读数如何调整计算机屏幕的亮度&#xff1f;许多朋友认为计算机显示器的亮度太亮或太暗. 如果要调节亮度&#xff0c;则找不到调节入口. 电脑屏幕的亮度也与电脑的节能有关&#xff0c;因此目前有许多具有亮度调节功能的电脑优化软件. 以下编辑总结了为每个人调整计算机屏幕…

channel的底层原理

golang之channel底层实现原理 在前面的文章中&#xff0c;我们已经了解了golang中channel的使用和优势。现在&#xff0c;让我们深入了解一下channel的底层实现原理&#xff0c;以更好地理解其工作方式。 通道的数据结构 在golang中&#xff0c;每个channel都有一个与之关联的数…

台式机XP系统调节屏幕亮度

XP系统更改方法&#xff1a; 【桌面】→ 【单击右键】→ 【属性】→ 【外观】→ 【高级】→ 项目选择【窗口】→ 【颜色1&#xff08;L&#xff09;】→ 【选择&#xff08;其它&#xff09;】将色调改为&#xff1a;85&#xff1b;饱和度&#xff1a;123&#xff1b;亮度&…

java实现屏幕亮度调节

第一步&#xff1a;添加以下工具类。 package home.equipmentControl.brightnessAdjust;import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader;public class BrightnessManager {public static void setBrightness(int brightness)t…

屏幕亮度自动调节的实现

以下文章转载自&#xff1a;http://www.miui.com/thread-27347-1-1.html 当在论坛上看到很多机友反映G7的自动亮度太暗&#xff0c;石头的自动亮度太亮的时候&#xff0c;MIUI小组准备做自动亮度的优化工作&#xff0c;着眼点就是增加自动亮度可调节功能。回首过往&#xff0c…