Skip to content

Commit

Permalink
merge in master
Browse files Browse the repository at this point in the history
  • Loading branch information
robmikh committed Sep 28, 2019
2 parents 964881e + c796256 commit c459667
Show file tree
Hide file tree
Showing 16 changed files with 429 additions and 224 deletions.
1 change: 0 additions & 1 deletion Win32CaptureSample/App.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "pch.h"
#include "EnumerationWindow.h"
#include "App.h"
#include "SimpleCapture.h"
#include "CaptureSnapshot.h"
Expand Down
23 changes: 0 additions & 23 deletions Win32CaptureSample/EnumerationMonitor.cpp

This file was deleted.

18 changes: 0 additions & 18 deletions Win32CaptureSample/EnumerationMonitor.h

This file was deleted.

90 changes: 0 additions & 90 deletions Win32CaptureSample/EnumerationWindow.cpp

This file was deleted.

20 changes: 0 additions & 20 deletions Win32CaptureSample/EnumerationWindow.h

This file was deleted.

76 changes: 76 additions & 0 deletions Win32CaptureSample/MonitorList.cpp
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()));
}
}
37 changes: 37 additions & 0 deletions Win32CaptureSample/MonitorList.h
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;
};
Loading

0 comments on commit c459667

Please sign in to comment.