get dpi of monitor and calculate actual size of it.

This commit is contained in:
hecomi
2016-11-06 12:17:29 +09:00
parent 15a98cd774
commit 6ac1a5ee60
7 changed files with 70 additions and 9 deletions
@@ -4,7 +4,7 @@ using System.Collections.Generic;
public class MultipleMonitorCreator : MonoBehaviour
{
[SerializeField] GameObject monitorPrefab;
[SerializeField] float width = 0.3f;
[SerializeField] float scale = 1f;
[SerializeField] float margin = 1f;
private List<GameObject> monitors_ = new List<GameObject>();
@@ -49,19 +49,17 @@ public class MultipleMonitorCreator : MonoBehaviour
// Assign monitor
var texture = go.GetComponent<uDesktopDuplication.Texture>();
texture.monitorId = i;
var monitor = texture.monitor;
// Set width / height
var isHorizontal = texture.monitor.isHorizontal;
var w = isHorizontal ? width : (width * texture.monitor.aspect);
var h = isHorizontal ? width / texture.monitor.aspect : width;
go.transform.localScale = new Vector3(w, go.transform.localScale.y, h);
go.transform.localScale = new Vector3(monitor.widthMeter, go.transform.localScale.y, monitor.heightMeter);
// Set parent as this object
go.transform.SetParent(transform);
// Calc actual size considering mesh size
var scaleX = go.GetComponent<MeshFilter>().sharedMesh.bounds.extents.x * 2f;
totalWidth_ += w * scaleX;
totalWidth_ += monitor.widthMeter * scaleX;
}
}
@@ -71,10 +69,10 @@ public class MultipleMonitorCreator : MonoBehaviour
totalWidth_ += margin * (monitors_.Count - 1);
var x = -totalWidth_ / 2;
foreach (var go in monitors_) {
var texture = go.GetComponent<uDesktopDuplication.Texture>();
var monitor = go.GetComponent<uDesktopDuplication.Texture>().monitor;
var halfScaleX = go.GetComponent<MeshFilter>().sharedMesh.bounds.extents.x;
var w = texture.monitor.isHorizontal ? width : (width * texture.monitor.aspect);
var halfWidth = w * halfScaleX;
var width = monitor.widthMeter;
var halfWidth = width * halfScaleX;
x += halfWidth;
go.transform.localPosition = new Vector3(x, 0f, 0f);
x += halfWidth + margin;
@@ -80,6 +80,10 @@ public static class Lib
[DllImport("uDesktopDuplication")]
public static extern int GetHeight(int id);
[DllImport("uDesktopDuplication")]
public static extern int GetDpiX(int id);
[DllImport("uDesktopDuplication")]
public static extern int GetDpiY(int id);
[DllImport("uDesktopDuplication")]
public static extern MonitorRotation GetRotation(int id);
[DllImport("uDesktopDuplication")]
public static extern bool IsPrimary(int id);
@@ -92,6 +92,26 @@ public class Monitor
get { return Lib.GetHeight(id); }
}
public int dpiX
{
get { return Lib.GetDpiX(id); }
}
public int dpiY
{
get { return Lib.GetDpiY(id); }
}
public float widthMeter
{
get { return width / dpiX * 0.0254f; }
}
public float heightMeter
{
get { return height / dpiY * 0.0254f; }
}
public MonitorRotation rotation
{
get { return Lib.GetRotation(id); }
@@ -1,4 +1,5 @@
#include <d3d11.h>
#include <ShellScalingAPI.h>
#include "Common.h"
#include "Cursor.h"
#include "MonitorManager.h"
@@ -18,6 +19,8 @@ HRESULT Monitor::Initialize(IDXGIOutput* output)
monitorInfo_.cbSize = sizeof(MONITORINFOEX);
GetMonitorInfo(outputDesc_.Monitor, &monitorInfo_);
GetDpiForMonitor(outputDesc_.Monitor, MDT_RAW_DPI, &dpiX_, &dpiY_);
auto output1 = reinterpret_cast<IDXGIOutput1*>(output);
const auto hr = output1->DuplicateOutput(GetDevice(), &deskDupl_);
@@ -196,6 +199,18 @@ int Monitor::GetRotation() const
}
int Monitor::GetDpiX() const
{
return static_cast<int>(dpiX_);
}
int Monitor::GetDpiY() const
{
return static_cast<int>(dpiY_);
}
int Monitor::GetWidth() const
{
const auto rect = monitorInfo_.rcMonitor;
@@ -41,11 +41,14 @@ public:
int GetWidth() const;
int GetHeight() const;
int GetRotation() const;
int GetDpiX() const;
int GetDpiY() const;
IDXGIOutputDuplication* GetDeskDupl();
const std::unique_ptr<Cursor>& GetCursor();
private:
int id_ = -1;
UINT dpiX_ = -1, dpiY_ = -1;
State state_ = State::NotSet;
std::unique_ptr<Cursor> cursor_;
IDXGIOutputDuplication* deskDupl_ = nullptr;
@@ -15,6 +15,7 @@
#include "MonitorManager.h"
#pragma comment(lib, "dxgi.lib")
#pragma comment(lib, "Shcore.lib")
namespace
@@ -234,6 +235,26 @@ extern "C"
return DXGI_MODE_ROTATION_UNSPECIFIED;
}
UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetDpiX(int id)
{
if (!g_manager) return -1;
if (auto monitor = g_manager->GetMonitor(id))
{
return monitor->GetDpiX();
}
return -1;
}
UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetDpiY(int id)
{
if (!g_manager) return -1;
if (auto monitor = g_manager->GetMonitor(id))
{
return monitor->GetDpiY();
}
return -1;
}
UNITY_INTERFACE_EXPORT bool UNITY_INTERFACE_API IsCursorVisible(int id)
{
if (!g_manager) return false;