您的位置:首页 > 其它

特定显示器调整主副屏、方向、位置

2016-12-27 10:09 148 查看
inline char* UnicodeToAnsi(const wchar_t* szStr)

{

int nLen = WideCharToMultiByte(CP_ACP, 0, szStr, -1, NULL, 0, NULL, NULL);

if (nLen == 0){

return NULL;

}

char* pResult = new char[nLen];

WideCharToMultiByte(CP_ACP, 0, szStr, -1, pResult, nLen, NULL, NULL);

return pResult;

}

extern “C” __declspec(dllexport) bool ToSetPrimaryDisPlay(){

DEVMODE deviceMode;
deviceMode.dmSize = sizeof(DEVMODE);
deviceMode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL | DM_POSITION | DM_DISPLAYFREQUENCY | DM_DISPLAYFLAGS;
DISPLAY_DEVICE DisplayDevice;
DISPLAY_DEVICE DisPlay_Info;
ZeroMemory(&DisplayDevice, sizeof(DISPLAY_DEVICE));
DisplayDevice.cb = sizeof(DisplayDevice);

ZeroMemory(&DisPlay_Info, sizeof(DISPLAY_DEVICE));
DisPlay_Info.cb = sizeof(DisPlay_Info);

int old_Coord_X = 0;
int old_Coord_Y = 0;

SetDisplayConfig(0, NULL, 0, NULL, (SDC_APPLY | SDC_TOPOLOGY_EXTEND));

for (int i = 0; EnumDisplayDevices(NULL, i, &DisplayDevice, 0); i++)
{
//EnumDisplayDevices函数是用来枚举显示设备的,第一个调用会得到所有显示器,
//将第一次枚举得到的显示器名字作为参数再传入EnumDisplayDevices,将会得到
//显示器的硬件名称
EnumDisplayDevices(DisplayDevice.DeviceName, 0, &DisPlay_Info, 0);


//获取到特定名称的显示器,在这里MONITOR\IMR00是特定的显示器硬件名称

if (strstr(UnicodeToAnsi(DisPlay_Info.DeviceID), “MONITOR\IMR00”) != NULL || strstr(UnicodeToAnsi(DisPlay_Info.DeviceID), “MONITOR\IVR00”) != NULL)

{

//枚举显示器属性,

EnumDisplaySettings(DisplayDevice.DeviceName, ENUM_CURRENT_SETTINGS, &deviceMode);

//以下标识符,代表本次操作更新注册表;

DWORD dwFlags = CDS_UPDATEREGISTRY;

//调整顺序按照调整主副屏、调整横竖屏、调整位置的操作进行,
//判断特定显示器是否为主显示器;
if (DisplayDevice.StateFlags &DISPLAY_DEVICE_PRIMARY_DEVICE){
cout << "需要调整主副屏" << endl;
old_Coord_X = deviceMode.dmPosition.x;
old_Coord_Y = deviceMode.dmPosition.y;
deviceMode.dmPosition.x = -1920;
deviceMode.dmPosition.y = 0;
if (DISP_CHANGE_SUCCESSFUL != ChangeDisplaySettingsEx(DisplayDevice.DeviceName, &deviceMode, NULL, dwFlags, NULL)){
//return false;
cout << "设置主副屏失败" << endl;
}
}
//判断特定显示器是否需要调整方向
if (deviceMode.dmDisplayOrientation != DMDO_90){
cout << "需要调整横竖屏" << endl;
deviceMode.dmDisplayOrientation = DMDO_90;
if (deviceMode.dmPelsHeight != 1080){
int temp = deviceMode.dmPelsHeight;
deviceMode.dmPelsHeight = deviceMode.dmPelsWidth;
deviceMode.dmPelsWidth = temp;
}
if (DISP_CHANGE_SUCCESSFUL != ChangeDisplaySettingsEx(DisplayDevice.DeviceName, &deviceMode, NULL, CDS_UPDATEREGISTRY, NULL)){
cout << "设置横竖屏失败" << endl;
}
}

//判断特定显示器是否需要调整位置
if (deviceMode.dmPosition.x < 0){
cout << "需要调整位置" << endl;
deviceMode.dmPosition.x = GetSystemMetrics(SM_CXSCREEN);
deviceMode.dmPosition.y = 0;
if (DISP_CHANGE_SUCCESSFUL != ChangeDisplaySettingsEx(DisplayDevice.DeviceName, &deviceMode, NULL, CDS_UPDATEREGISTRY, NULL)){
//ChangeDisplaySettingsEx(0, 0, 0, 0, 0);
cout << "移动屏幕失败" << endl;
}
}
}
}
return true;


}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  显示器设置