-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
429 additions
and
224 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#include "pch.h" | ||
#include "MonitorList.h" | ||
|
||
std::vector<MonitorInfo> EnumerateAllMonitors() | ||
{ | ||
std::vector<MonitorInfo> monitors; | ||
EnumDisplayMonitors(nullptr, nullptr, [](HMONITOR hmon, HDC, LPRECT, LPARAM lparam) | ||
{ | ||
auto& monitors = *reinterpret_cast<std::vector<MonitorInfo>*>(lparam); | ||
monitors.push_back(MonitorInfo(hmon)); | ||
|
||
return TRUE; | ||
}, reinterpret_cast<LPARAM>(&monitors)); | ||
return monitors; | ||
} | ||
|
||
MonitorList::MonitorList() | ||
{ | ||
m_monitors = EnumerateAllMonitors(); | ||
} | ||
|
||
void MonitorList::Update() | ||
{ | ||
auto monitors = EnumerateAllMonitors(); | ||
std::map<HMONITOR, MonitorInfo> newMonitors; | ||
for (auto& monitor : monitors) | ||
{ | ||
newMonitors.insert({ monitor.MonitorHandle, monitor }); | ||
} | ||
|
||
std::vector<int> monitorIndexesToRemove; | ||
auto index = 0; | ||
for (auto& monitor : m_monitors) | ||
{ | ||
auto search = newMonitors.find(monitor.MonitorHandle); | ||
if (search == newMonitors.end()) | ||
{ | ||
monitorIndexesToRemove.push_back(index); | ||
} | ||
else | ||
{ | ||
newMonitors.erase(search); | ||
} | ||
index++; | ||
} | ||
|
||
// Remove old monitors | ||
for (auto& removalIndex : monitorIndexesToRemove) | ||
{ | ||
m_monitors.erase(m_monitors.begin() + removalIndex); | ||
for (auto& comboBox : m_comboBoxes) | ||
{ | ||
winrt::check_hresult(SendMessageW(comboBox, CB_DELETESTRING, removalIndex, 0)); | ||
} | ||
} | ||
|
||
// Add new monitors | ||
for (auto& pair : newMonitors) | ||
{ | ||
auto monitor = pair.second; | ||
m_monitors.push_back(monitor); | ||
for (auto& comboBox : m_comboBoxes) | ||
{ | ||
winrt::check_hresult(SendMessageW(comboBox, CB_ADDSTRING, 0, (LPARAM)monitor.DisplayName.c_str())); | ||
} | ||
} | ||
} | ||
|
||
void MonitorList::ForceUpdateComboBox(HWND comboBoxHandle) | ||
{ | ||
winrt::check_hresult(SendMessageW(comboBoxHandle, CB_RESETCONTENT, 0, 0)); | ||
for (auto& monitor : m_monitors) | ||
{ | ||
winrt::check_hresult(SendMessageW(comboBoxHandle, CB_ADDSTRING, 0, (LPARAM)monitor.DisplayName.c_str())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#pragma once | ||
|
||
struct MonitorInfo | ||
{ | ||
MonitorInfo(HMONITOR monitorHandle) | ||
{ | ||
MonitorHandle = monitorHandle; | ||
MONITORINFOEX monitorInfo = { sizeof(monitorInfo) }; | ||
winrt::check_bool(GetMonitorInfo(MonitorHandle, &monitorInfo)); | ||
std::wstring displayName(monitorInfo.szDevice); | ||
DisplayName = displayName; | ||
} | ||
|
||
HMONITOR MonitorHandle; | ||
std::wstring DisplayName; | ||
|
||
bool operator==(const MonitorInfo& monitor) { return MonitorHandle == monitor.MonitorHandle; } | ||
bool operator!=(const MonitorInfo& monitor) { return !(*this == monitor); } | ||
}; | ||
|
||
class MonitorList | ||
{ | ||
public: | ||
MonitorList(); | ||
|
||
void Update(); | ||
void RegisterComboBoxForUpdates(HWND comboBoxHandle) { m_comboBoxes.push_back(comboBoxHandle); ForceUpdateComboBox(comboBoxHandle); } | ||
void UnregisterComboBox(HWND comboBoxHandle) { m_comboBoxes.erase(std::remove(m_comboBoxes.begin(), m_comboBoxes.end(), comboBoxHandle), m_comboBoxes.end()); } | ||
const std::vector<MonitorInfo> GetCurrentMonitors() { return m_monitors; } | ||
|
||
private: | ||
void ForceUpdateComboBox(HWND comboBoxHandle); | ||
|
||
private: | ||
std::vector<HWND> m_comboBoxes; | ||
std::vector<MonitorInfo> m_monitors; | ||
}; |
Oops, something went wrong.