測試程式,
拿來測試工作上會用到的東西。
pch.h
#ifndef PCH_H
#define PCH_H
// TODO: add headers that you want to pre-compile here
enum class DisplayTopology
{
Internal,
Clone,
Extend,
External,
Error
};
DisplayTopology GetDisplayTopology();
bool SetDisplayTopology(DisplayTopology topology);
#endif //PCH_H
pch.cpp
// pch.cpp: source file corresponding to pre-compiled header; necessary for compilation to succeed
#include "pch.h"
// In general, ignore this file, but keep it around if you are using pre-compiled headers.
#include <ciso646>
#include <windows.h>
#pragma comment(lib,"User32.lib")
//-------------------------------------------------------------------------------------------------
DisplayTopology GetDisplayTopology()
{
UINT32 numPathArrayElements;
UINT32 numModeInfoArrayElements;
GetDisplayConfigBufferSizes(QDC_DATABASE_CURRENT, &numPathArrayElements, &numModeInfoArrayElements);
auto pathArray = new DISPLAYCONFIG_PATH_INFO[numPathArrayElements];
auto modeInfoArray = new DISPLAYCONFIG_MODE_INFO[numModeInfoArrayElements];
DISPLAYCONFIG_TOPOLOGY_ID currentTopologyId;
auto ok = QueryDisplayConfig(QDC_DATABASE_CURRENT,
&numPathArrayElements, pathArray,
&numModeInfoArrayElements, modeInfoArray,
¤tTopologyId);
delete[] pathArray;
delete[] modeInfoArray;
if (ok == ERROR_SUCCESS) switch (currentTopologyId)
{
case DISPLAYCONFIG_TOPOLOGY_INTERNAL: return DisplayTopology::Internal;
case DISPLAYCONFIG_TOPOLOGY_CLONE: return DisplayTopology::Clone;
case DISPLAYCONFIG_TOPOLOGY_EXTEND: return DisplayTopology::Extend;
case DISPLAYCONFIG_TOPOLOGY_EXTERNAL: return DisplayTopology::External;
default: break;
}
return DisplayTopology::Error;
}
//-------------------------------------------------------------------------------------------------
bool SetDisplayTopology(DisplayTopology topology)
{
UINT32 topologies[] =
{
SDC_TOPOLOGY_INTERNAL,
SDC_TOPOLOGY_CLONE,
SDC_TOPOLOGY_EXTEND,
SDC_TOPOLOGY_EXTERNAL
};
if (topology == DisplayTopology::Error) return false;
return SetDisplayConfig(0, NULL, 0, NULL, topologies[(int)topology] | SDC_APPLY) == ERROR_SUCCESS;
}
Main
#include "pch.h"
#include <cctype>
#include <iostream>
#include <string>
using namespace std;
//-------------------------------------------------------------------------------------------------
void set_topology(std::string&& mode)
{
for (auto& c : mode) c = std::tolower(c & 0x7F);
DisplayTopology topology =
(mode == "internal") ? DisplayTopology::Internal :
(mode == "clone") ? DisplayTopology::Clone :
(mode == "duplicate") ? DisplayTopology::Clone :
(mode == "extend") ? DisplayTopology::Extend :
(mode == "external") ? DisplayTopology::External :
DisplayTopology::Error;
if (topology == DisplayTopology::Error) throw "Argument must be one of 'internal', 'clone', 'duplicate', 'extend', or 'external'";
if (!SetDisplayTopology(topology)) throw "Could not change display topology";
}
//-------------------------------------------------------------------------------------------------
void get_topology()
{
static const char* topologies[] =
{
"internal",
"clone",
"extend",
"external",
nullptr
};
auto topology = topologies[(int)GetDisplayTopology()];
if (!topology) throw "Failure to identify the current display topology";
std::cout << topology << "\n";
}
//-------------------------------------------------------------------------------------------------
int main()
{
try
{
int nSel = -1 ;
while (1)
{
cout << "Select a Mode : " << endl
<< "1 = internal" << endl
<< "2 = clone" << endl
<< "3 = extend" << endl
<< "4 = external" << endl;
cin >> nSel;
if (nSel < 0 || nSel>4)
{
cout << endl;
continue;
}
else
{
switch (nSel)
{
case 1:set_topology("internal"); break;
case 2:set_topology("clone"); break;
case 3:set_topology("extend"); break;
case 4:set_topology("external"); break;
default:
break;
}
}
}
}
catch (const char* error_message)
{
std::cerr << error_message << "\n";
return 1;
}
}