获取并设置windows上的显示器亮度级别
我们想获取/设置Windows上显示器的亮度级别。我们找不到任何有效的示例代码。我们不断从GetPhysicalMonitorsFromHMONITOR()获取空句柄。有人能提供适用于Windows 7及更高版本的示例代码吗?我们已经搜索了一个又一个工作代码示例,但找不到任何内容。下面是我们尝试过的代码(在许多代码中)获取并设置windows上的显示器亮度级别,windows,screen-brightness,Windows,Screen Brightness,我们想获取/设置Windows上显示器的亮度级别。我们找不到任何有效的示例代码。我们不断从GetPhysicalMonitorsFromHMONITOR()获取空句柄。有人能提供适用于Windows 7及更高版本的示例代码吗?我们已经搜索了一个又一个工作代码示例,但找不到任何内容。下面是我们尝试过的代码(在许多代码中) typedef结构{ 浮动*获取; 浮点常量*集; }枚举信息; 静止的 BOOL回调监视器( HMONITOR HMONITOR, HDC HDC监视器, LPRECT lpr
typedef结构{
浮动*获取;
浮点常量*集;
}枚举信息;
静止的
BOOL回调监视器(
HMONITOR HMONITOR,
HDC HDC监视器,
LPRECT lprcMonitor,
LPRAM dwData
) {
布尔ok;
枚举信息*常量信息=(枚举信息*数据);
eprintf(“窗口:亮度:hMonitor=%p info=%p\n”,hMonitor,info);
德沃德nmonitor;
LPPHYSICAL\u监视器PPPhysicalMonitors=NULL;
/*获取物理监视器的数量。
*/
ok=从HMonitor获取物理监视器的编号(
hMonitor,
&监视器);
eprintf(“WINDOWS:brightness:num\u监视器正常=%d计数=%ld\n”,
好的,nmonitor);
如果(!好的||
!n监视器){
去做;
}
/*分配物理监视结构的数组。
*/
物理监测器=
(LPPHYSICAL_MONITOR)malloc(nmonitor*sizeof(PHYSICAL_MONITOR));
if(!PPPhysicalMonitors){
去做;
}
ok=GetPhysicalMonitors fromMonitor(
H监测器、N监测器、P物理监测器);
/*使用监视器手柄(未显示)。
*/
如果(确定){
for(无符号ofs=0;OFSSZ物理监视器说明);
seaiq_string_t tmp_s;
eprintf(“WINDOWS:亮度:监视器[%d]句柄=%p说明=%s\n”,
ofs,P监视器->HPPhysicalMonitor,
tmp_s=QString_to_string(tmp));
seaiq_无字符串(tmp_s);
DWORD caps=0;
DWORD temps=0;
ok=GetMonitorCapabilities(pmonitor->HPPhysicalMonitor,&caps,&temps);
eprintf(“WINDOWS:亮度:监视器[%d]正常=%d亮度=%d无=%d\n”,ofs,正常,
(大写字母和MC_大写字母和亮度)!=0,
(大写和小写大写)!=0);
DWORD最小值=0;
DWORD cur=0;
DWORD max=0;
ok=GetMonitorBrightness(
p监视器->物理监视器、&min、&cur、&max);
eprintf(“WINDOWS:亮度:监视器[%d]正常=%d最小=%lu当前=%lu最大=%lu\n”,
ofs、ok、min、cur、max);
}
LPPHYSICAL_MONITOR pmonitor=&ppphysicalmonitors[0];
DWORD最小值=0;
DWORD cur=0;
DWORD max=0;
ok=GetMonitorBrightness(
p监视器->物理监视器、&min、&cur、&max);
eprintf(“WINDOWS:brightness:GetMonitorBrightness:ok=%d min=%lu cur=%lu max=%lu\n”,
正常,最小值,当前值,最大值);
如果(确定){
如果(信息->获取和最小值<最大和最小值获取);
}
如果(信息->设置){
DWORD dwv=*信息->设置*(最大-最小)+最小;
ok=设置监视器亮度(
P物理监视器[0]。HP物理监视器,
dwv);
eprintf(“窗口:亮度:设置:dwv=%ld ok=%d\n”,dwv,ok);
}
}
}
/*关闭显示器手柄。
*/
ok=物理监测器(
nmonitor,
物理监测器);
/*释放数组。
*/
免费(PPP物理监控器);
完成:
返回1;
}
在编写问题时,您没有调用名为GetPhysicalMonitors()
的函数。请花点时间仔细解释您遇到的问题。如果您需要示例代码,那么stackoverflow可能是错误的位置。感谢您指出这一点。修复了描述中的错误,即GetPhysicalMonitorsFromHMONITOR(MSDN建议使用)。我们不太关心使用什么特定的函数来实现这一点;我们只是想要一种方法。我们花了一整天的时间在这上面,不明白我们可能做错了什么,或者为什么没有可用的代码片段。这应该很容易。
typedef struct {
float * get ;
float const * set ;
} enum_info_t ;
static
BOOL CALLBACK monitor_callback(
HMONITOR hMonitor,
HDC hdcMonitor,
LPRECT lprcMonitor,
LPARAM dwData
) {
BOOL ok ;
enum_info_t * const info = (enum_info_t *) dwData ;
eprintf("WINDOWS:brightness:hMonitor=%p info=%p\n", hMonitor, info) ;
DWORD nmonitor ;
LPPHYSICAL_MONITOR pPhysicalMonitors = NULL;
/* Get the number of physical monitors.
*/
ok = GetNumberOfPhysicalMonitorsFromHMONITOR(
hMonitor,
&nmonitor) ;
eprintf("WINDOWS:brightness:num_monitors ok=%d count=%ld\n",
ok, nmonitor) ;
if (!ok ||
!nmonitor) {
goto done ;
}
/* Allocate the array of PHYSICAL_MONITOR structures.
*/
pPhysicalMonitors =
(LPPHYSICAL_MONITOR) malloc(nmonitor * sizeof(PHYSICAL_MONITOR)) ;
if (!pPhysicalMonitors) {
goto done ;
}
ok = GetPhysicalMonitorsFromHMONITOR(
hMonitor, nmonitor, pPhysicalMonitors);
/* Use the monitor handles (not shown).
*/
if (ok) {
for (unsigned ofs=0;ofs<nmonitor;ofs++) {
LPPHYSICAL_MONITOR pmonitor = &pPhysicalMonitors[ofs] ;
QString tmp =
QString::fromWCharArray(pmonitor->szPhysicalMonitorDescription) ;
seaiq_string_t tmp_s ;
eprintf("WINDOWS:brightness:monitor[%d] handle=%p description=%s\n",
ofs, pmonitor->hPhysicalMonitor,
tmp_s = QString_to_string(tmp)) ;
seaiq_string_free(tmp_s) ;
DWORD caps = 0 ;
DWORD temps = 0 ;
ok = GetMonitorCapabilities(pmonitor->hPhysicalMonitor, &caps, &temps) ;
eprintf("WINDOWS:brightness:monitor[%d] ok=%d brightness=%d none=%d\n", ofs, ok,
(caps & MC_CAPS_BRIGHTNESS) != 0,
(caps & MC_CAPS_NONE) != 0) ;
DWORD min = 0 ;
DWORD cur = 0 ;
DWORD max = 0 ;
ok = GetMonitorBrightness(
pmonitor->hPhysicalMonitor, &min, &cur, &max) ;
eprintf("WINDOWS:brightness:monitor[%d] ok=%d min=%lu cur=%lu max=%lu\n",
ofs, ok, min, cur, max) ;
}
LPPHYSICAL_MONITOR pmonitor = &pPhysicalMonitors[0] ;
DWORD min = 0 ;
DWORD cur = 0 ;
DWORD max = 0 ;
ok = GetMonitorBrightness(
pmonitor->hPhysicalMonitor, &min, &cur, &max) ;
eprintf("WINDOWS:brightness:GetMonitorBrightness:ok=%d min=%lu cur=%lu max=%lu\n",
ok, min, cur, max) ;
if (ok) {
if (info->get && min < max && min <= cur && cur <= max) {
*info->get =
((float)cur - (float)min) /
((float)max - (float)min) ;
eprintf("WINDOWS:brightness:get:%.3f\n", *info->get) ;
}
if (info->set) {
DWORD dwv = *info->set * (max - min) + min ;
ok = SetMonitorBrightness(
pPhysicalMonitors[0].hPhysicalMonitor,
dwv) ;
eprintf("WINDOWS:brightness:set:dwv=%ld ok=%d\n", dwv, ok) ;
}
}
}
/* Close the monitor handles.
*/
ok = DestroyPhysicalMonitors(
nmonitor,
pPhysicalMonitors);
/* Free the array.
*/
free(pPhysicalMonitors);
done:
return 1 ;
}