如何通过鼠标移动而不是 VK_SPACE 打开显示
How to turn on display with mouse move but not with VK_SPACE
此代码禁用 2 个显示中的 1 个。任务不是通过 VK_SPACE
而是通过鼠标移动来打开它。
我尝试通过 WM_MOUSEMOVE
完成此操作,但到目前为止没有任何效果。我不太明白这个问题是怎么实现的。
如果有机会实现这个话题,我将不胜感激。
#include <iostream>
#include <windows.h>
#include <vector>
#include <lowlevelmonitorconfigurationapi.h>
#include <windowsx.h>
#pragma comment(lib, "Dxva2.lib")
#define KEY_DOWN(key) ((::GetAsyncKeyState(key) & 0x80000) ? 1 : 0)
#define KEY_UP(key) ((::GetAsyncKeyState(key) & 0x80000) ? 0 : 1)
const BYTE PowerMode = 0xD6; // VCP Code defined in VESA Monitor Control Command Set (MCCS) standard
const DWORD PowerOn = 0x01;
const DWORD PowerOff = 0x04;
// Monitor description struct
struct MonitorDesc
{
HANDLE hdl;
DWORD power;
};
// Monitor enumeration callback
BOOL CALLBACK MonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
{
std::vector<MonitorDesc>* pMonitors = reinterpret_cast<std::vector<MonitorDesc>*>(dwData);
DWORD nMonitorCount;
if (GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor, &nMonitorCount))
{
PHYSICAL_MONITOR* pMons = new PHYSICAL_MONITOR[nMonitorCount];
if (GetPhysicalMonitorsFromHMONITOR(hMonitor, nMonitorCount, pMons))
{
for (DWORD i = 0; i < nMonitorCount; i++)
{
MonitorDesc desc;
desc.hdl = pMons[i].hPhysicalMonitor;
pMonitors->push_back(desc);
}
}
delete[] pMons;
}
return TRUE;
}
// Switch monitor power
void MonitorSwitch(MonitorDesc& monitor, DWORD mode)
{
if (monitor.power == mode)
return;
SetVCPFeature(monitor.hdl, PowerMode, mode);
monitor.power = mode;
}
int main()
{
// Скрытие консоли
HWND hWnd;
AllocConsole();
hWnd = FindWindowA("ConsoleWindowClass", NULL);
ShowWindow(hWnd, 0);
std::vector<MonitorDesc> monitors;
EnumDisplayMonitors(NULL, NULL, &MonitorEnumProc, reinterpret_cast<LPARAM>(&monitors));
// Init
for (auto& monitor : monitors)
{
monitor.power = PowerOn;
}
// Here select the first one monitor as example
MonitorDesc targetMonitor = monitors[0];
while (1)
{
if (::GetAsyncKeyState('L') == -32767)
{
if (KEY_DOWN(VK_CONTROL) && KEY_DOWN(VK_MENU))
// turn off
if (targetMonitor.power == PowerOn)
MonitorSwitch(targetMonitor, PowerOff);
}
if (::GetAsyncKeyState(VK_SPACE) == -32767)
{
if (KEY_DOWN(VK_SPACE))
// turn on
MonitorSwitch(targetMonitor, PowerOn);
}
if (::GetAsyncKeyState('E') == -32767)
{
if (KEY_DOWN(VK_CONTROL))
return 0;
}
}
}
我用GetCursorPos获取real-time鼠标位置,当鼠标越过monitor.Here的边缘时执行energy-saving操作是我的代码,我详细注释了代码。
#include <iostream>
#include <windows.h>
#include <vector>
#include <lowlevelmonitorconfigurationapi.h>
#include <windowsx.h>
#pragma comment(lib, "Dxva2.lib")
#define KEY_DOWN(key) ((::GetAsyncKeyState(key) & 0x80000) ? 1 : 0)
#define KEY_UP(key) ((::GetAsyncKeyState(key) & 0x80000) ? 0 : 1)
const BYTE PowerMode = 0xD6; // VCP Code defined in VESA Monitor Control Command Set (MCCS) standard
const DWORD PowerOn = 0x01;
const DWORD PowerOff = 0x04;
// Monitor description struct
struct MonitorDesc
{
HANDLE hdl;
DWORD power;
};
// Monitor enumeration callback
BOOL CALLBACK MonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
{
std::vector<MonitorDesc>* pMonitors = reinterpret_cast<std::vector<MonitorDesc>*>(dwData);
DWORD nMonitorCount;
if (GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor, &nMonitorCount))
{
PHYSICAL_MONITOR* pMons = new PHYSICAL_MONITOR[nMonitorCount];
if (GetPhysicalMonitorsFromHMONITOR(hMonitor, nMonitorCount, pMons))
{
for (DWORD i = 0; i < nMonitorCount; i++)
{
MonitorDesc desc;
desc.hdl = pMons[i].hPhysicalMonitor;
pMonitors->push_back(desc);
}
}
delete[] pMons;
}
return TRUE;
}
// Switch monitor power
void MonitorSwitch(MonitorDesc& monitor, DWORD mode)
{
if (monitor.power == mode)
return;
SetVCPFeature(monitor.hdl, PowerMode, mode);
monitor.power = mode;
}
int main()
{
// Скрытие консоли
HWND hWnd;
AllocConsole();
hWnd = FindWindowA("ConsoleWindowClass", NULL);
ShowWindow(hWnd, 1);
std::vector<MonitorDesc> monitors;
EnumDisplayMonitors(NULL, NULL, &MonitorEnumProc, reinterpret_cast<LPARAM>(&monitors));
// Init
for (auto& monitor : monitors)
{
monitor.power = PowerOn;
}
// Here select the first one monitor as example
MonitorDesc targetMonitor = monitors[0];
int currentMonitorID = 0; //need start this exe in monitors[0]
//Mouse position
LONG zx = -1;
LONG zy = -1;
POINT ptB = { 0, 0 };
while (1)
{
/*---------------------------------------------------------------*/
/*- - -*/
/*- - -*/
/*- - -*/
/*- monitors[0] - monitors[1] -*/
/*- - -*/
/*- - -*/
/*- - -*/
/*---------------------------------------------------------------*/
/* {1919,1079} */
LPPOINT xy = &ptB; //Location variables
GetCursorPos(xy); //Gets the current mouse position
//If the mouse moves, (i.e. the current coordinates change to print out the coordinates) print out the coordinates.
if ((zx != xy->x) || (zy != xy->y))
{
//Here you need to test the edge of your monitor[0]
//After Test, delete this and Hide the console by ShowWindow(hWnd, 0)
printf("x=%d,y=%d\n", xy->x, xy->y);
}
//The coordinate in the lower right corner of my monitor is {1919,1079}
if (xy->x > 1919 && currentMonitorID == 0)
{
currentMonitorID = 1;
MonitorSwitch(monitors[1], PowerOn);
MonitorSwitch(monitors[0], PowerOff);
}
else if ( xy->x <= 1919 && currentMonitorID == 1)
{
currentMonitorID = 0;
MonitorSwitch(monitors[0], PowerOn);
MonitorSwitch(monitors[1], PowerOff);
}
/*if (::GetAsyncKeyState('L') == -32767)
{
if (KEY_DOWN(VK_CONTROL) && KEY_DOWN(VK_MENU))
// turn off
if (targetMonitor.power == PowerOn)
MonitorSwitch(targetMonitor, PowerOff);
}
if (::GetAsyncKeyState(VK_SPACE) == -32767)
{
if (KEY_DOWN(VK_SPACE))
// turn on
MonitorSwitch(targetMonitor, PowerOn);
}*/
if (::GetAsyncKeyState('E') == -32767)
{
if (KEY_DOWN(VK_CONTROL))
return 0;
}
}
}
此代码禁用 2 个显示中的 1 个。任务不是通过 VK_SPACE
而是通过鼠标移动来打开它。
我尝试通过 WM_MOUSEMOVE
完成此操作,但到目前为止没有任何效果。我不太明白这个问题是怎么实现的。
如果有机会实现这个话题,我将不胜感激。
#include <iostream>
#include <windows.h>
#include <vector>
#include <lowlevelmonitorconfigurationapi.h>
#include <windowsx.h>
#pragma comment(lib, "Dxva2.lib")
#define KEY_DOWN(key) ((::GetAsyncKeyState(key) & 0x80000) ? 1 : 0)
#define KEY_UP(key) ((::GetAsyncKeyState(key) & 0x80000) ? 0 : 1)
const BYTE PowerMode = 0xD6; // VCP Code defined in VESA Monitor Control Command Set (MCCS) standard
const DWORD PowerOn = 0x01;
const DWORD PowerOff = 0x04;
// Monitor description struct
struct MonitorDesc
{
HANDLE hdl;
DWORD power;
};
// Monitor enumeration callback
BOOL CALLBACK MonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
{
std::vector<MonitorDesc>* pMonitors = reinterpret_cast<std::vector<MonitorDesc>*>(dwData);
DWORD nMonitorCount;
if (GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor, &nMonitorCount))
{
PHYSICAL_MONITOR* pMons = new PHYSICAL_MONITOR[nMonitorCount];
if (GetPhysicalMonitorsFromHMONITOR(hMonitor, nMonitorCount, pMons))
{
for (DWORD i = 0; i < nMonitorCount; i++)
{
MonitorDesc desc;
desc.hdl = pMons[i].hPhysicalMonitor;
pMonitors->push_back(desc);
}
}
delete[] pMons;
}
return TRUE;
}
// Switch monitor power
void MonitorSwitch(MonitorDesc& monitor, DWORD mode)
{
if (monitor.power == mode)
return;
SetVCPFeature(monitor.hdl, PowerMode, mode);
monitor.power = mode;
}
int main()
{
// Скрытие консоли
HWND hWnd;
AllocConsole();
hWnd = FindWindowA("ConsoleWindowClass", NULL);
ShowWindow(hWnd, 0);
std::vector<MonitorDesc> monitors;
EnumDisplayMonitors(NULL, NULL, &MonitorEnumProc, reinterpret_cast<LPARAM>(&monitors));
// Init
for (auto& monitor : monitors)
{
monitor.power = PowerOn;
}
// Here select the first one monitor as example
MonitorDesc targetMonitor = monitors[0];
while (1)
{
if (::GetAsyncKeyState('L') == -32767)
{
if (KEY_DOWN(VK_CONTROL) && KEY_DOWN(VK_MENU))
// turn off
if (targetMonitor.power == PowerOn)
MonitorSwitch(targetMonitor, PowerOff);
}
if (::GetAsyncKeyState(VK_SPACE) == -32767)
{
if (KEY_DOWN(VK_SPACE))
// turn on
MonitorSwitch(targetMonitor, PowerOn);
}
if (::GetAsyncKeyState('E') == -32767)
{
if (KEY_DOWN(VK_CONTROL))
return 0;
}
}
}
我用GetCursorPos获取real-time鼠标位置,当鼠标越过monitor.Here的边缘时执行energy-saving操作是我的代码,我详细注释了代码。
#include <iostream>
#include <windows.h>
#include <vector>
#include <lowlevelmonitorconfigurationapi.h>
#include <windowsx.h>
#pragma comment(lib, "Dxva2.lib")
#define KEY_DOWN(key) ((::GetAsyncKeyState(key) & 0x80000) ? 1 : 0)
#define KEY_UP(key) ((::GetAsyncKeyState(key) & 0x80000) ? 0 : 1)
const BYTE PowerMode = 0xD6; // VCP Code defined in VESA Monitor Control Command Set (MCCS) standard
const DWORD PowerOn = 0x01;
const DWORD PowerOff = 0x04;
// Monitor description struct
struct MonitorDesc
{
HANDLE hdl;
DWORD power;
};
// Monitor enumeration callback
BOOL CALLBACK MonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
{
std::vector<MonitorDesc>* pMonitors = reinterpret_cast<std::vector<MonitorDesc>*>(dwData);
DWORD nMonitorCount;
if (GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor, &nMonitorCount))
{
PHYSICAL_MONITOR* pMons = new PHYSICAL_MONITOR[nMonitorCount];
if (GetPhysicalMonitorsFromHMONITOR(hMonitor, nMonitorCount, pMons))
{
for (DWORD i = 0; i < nMonitorCount; i++)
{
MonitorDesc desc;
desc.hdl = pMons[i].hPhysicalMonitor;
pMonitors->push_back(desc);
}
}
delete[] pMons;
}
return TRUE;
}
// Switch monitor power
void MonitorSwitch(MonitorDesc& monitor, DWORD mode)
{
if (monitor.power == mode)
return;
SetVCPFeature(monitor.hdl, PowerMode, mode);
monitor.power = mode;
}
int main()
{
// Скрытие консоли
HWND hWnd;
AllocConsole();
hWnd = FindWindowA("ConsoleWindowClass", NULL);
ShowWindow(hWnd, 1);
std::vector<MonitorDesc> monitors;
EnumDisplayMonitors(NULL, NULL, &MonitorEnumProc, reinterpret_cast<LPARAM>(&monitors));
// Init
for (auto& monitor : monitors)
{
monitor.power = PowerOn;
}
// Here select the first one monitor as example
MonitorDesc targetMonitor = monitors[0];
int currentMonitorID = 0; //need start this exe in monitors[0]
//Mouse position
LONG zx = -1;
LONG zy = -1;
POINT ptB = { 0, 0 };
while (1)
{
/*---------------------------------------------------------------*/
/*- - -*/
/*- - -*/
/*- - -*/
/*- monitors[0] - monitors[1] -*/
/*- - -*/
/*- - -*/
/*- - -*/
/*---------------------------------------------------------------*/
/* {1919,1079} */
LPPOINT xy = &ptB; //Location variables
GetCursorPos(xy); //Gets the current mouse position
//If the mouse moves, (i.e. the current coordinates change to print out the coordinates) print out the coordinates.
if ((zx != xy->x) || (zy != xy->y))
{
//Here you need to test the edge of your monitor[0]
//After Test, delete this and Hide the console by ShowWindow(hWnd, 0)
printf("x=%d,y=%d\n", xy->x, xy->y);
}
//The coordinate in the lower right corner of my monitor is {1919,1079}
if (xy->x > 1919 && currentMonitorID == 0)
{
currentMonitorID = 1;
MonitorSwitch(monitors[1], PowerOn);
MonitorSwitch(monitors[0], PowerOff);
}
else if ( xy->x <= 1919 && currentMonitorID == 1)
{
currentMonitorID = 0;
MonitorSwitch(monitors[0], PowerOn);
MonitorSwitch(monitors[1], PowerOff);
}
/*if (::GetAsyncKeyState('L') == -32767)
{
if (KEY_DOWN(VK_CONTROL) && KEY_DOWN(VK_MENU))
// turn off
if (targetMonitor.power == PowerOn)
MonitorSwitch(targetMonitor, PowerOff);
}
if (::GetAsyncKeyState(VK_SPACE) == -32767)
{
if (KEY_DOWN(VK_SPACE))
// turn on
MonitorSwitch(targetMonitor, PowerOn);
}*/
if (::GetAsyncKeyState('E') == -32767)
{
if (KEY_DOWN(VK_CONTROL))
return 0;
}
}
}