gpt4 book ai didi

c++ - 如何通过鼠标移动而不是 VK_SPACE 打开显示

转载 作者:行者123 更新时间:2023-12-05 05:47:17 25 4
gpt4 key购买 nike

此代码禁用 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获取实时鼠标位置,当鼠标越过显示器边缘时执行节能操作。这是我的代码,我详细注释了代码。

#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;
}

}
}

关于c++ - 如何通过鼠标移动而不是 VK_SPACE 打开显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71030204/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com