-
Notifications
You must be signed in to change notification settings - Fork 0
/
MagWindow.h
101 lines (82 loc) · 3.12 KB
/
MagWindow.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#pragma once
#include "stdafx.h"
class MagWindow
{
private:
HWND _hwnd;
float _magFactor;
POINT _windowPosition;
SIZE _windowSize;
// Rectangle of screen that is centered at the mouse coordinates to be magnified.
RECT _sourceRect;
VOID UpdateSourceRect(LPPOINT mousePoint, POINT panOffset, SIZE windowSize)
{
_sourceRect.left = mousePoint->x + panOffset.x - (int)((windowSize.cx / 2) / _magFactor);
_sourceRect.top = mousePoint->y + panOffset.y - (int)((windowSize.cy / 2) / _magFactor);
_sourceRect.right = mousePoint->x + (int)((windowSize.cx / 2) / _magFactor);
_sourceRect.bottom = mousePoint->y + (int)((windowSize.cy / 2) / _magFactor);
}
BOOL SetMagnificationFactorInternal(float magFactor)
{
MAGTRANSFORM matrix;
memset(&matrix, 0, sizeof(matrix));
matrix.v[0][0] = magFactor;
matrix.v[1][1] = magFactor;
matrix.v[2][2] = 1.0f;
// TODO Avoid race condition where calls to UpdateSourceRect + UpdateMagnifier happen with new magFactor, but before MagSetWindowTransform is called
// This is isn't a problem though since this method is always assumed to be called a non-active magWindow
_magFactor = magFactor;
return MagSetWindowTransform(_hwnd, &matrix);
}
public:
MagWindow() {}
MagWindow(float magFactor, POINT windowPosition, SIZE windowSize)
{
_hwnd = nullptr;
_magFactor = magFactor;
_windowSize = windowSize;
_windowPosition = windowPosition;
}
~MagWindow() {}
BOOL Create(HINSTANCE hInst, HWND hwndHost, BOOL visible)
{
DWORD dwStyle =
WS_CHILD | // Required for magnification window
WS_EX_COMPOSITED; // Double-buffered
if (visible) { dwStyle |= WS_VISIBLE; }
_hwnd = CreateWindow(
WC_MAGNIFIER, // Magnifier window class name defined in magnification.h
TEXT("MagnifierWindow2"),
dwStyle,
_windowPosition.x, _windowPosition.y,
_windowSize.cx, _windowSize.cy,
hwndHost, nullptr, hInst, nullptr);
if (_hwnd == nullptr)
{
return FALSE;
}
return SetMagnificationFactorInternal(_magFactor);
}
HWND GetHandle() { return _hwnd; }
BOOL SetMagnificationFactor(float magFactor)
{
if (_magFactor != 0 && _magFactor == magFactor) { return FALSE; }
return SetMagnificationFactorInternal(magFactor);
}
BOOL SetSize(int width, int height)
{
if (_windowSize.cx == width && _windowSize.cy == height) { return FALSE; }
_windowSize.cx = width;
_windowSize.cy = height;
return SetWindowPos(_hwnd, HWND_TOP,
_windowPosition.x, _windowPosition.y,
_windowSize.cx, _windowSize.cy,
SWP_NOACTIVATE | SWP_NOMOVE);
}
BOOL UpdateMagnifier(LPPOINT mousePoint, POINT panOffset, SIZE windowSize)
{
UpdateSourceRect(mousePoint, panOffset, windowSize);
// Set the source rectangle for the magnifier control.
return MagSetWindowSource(_hwnd, _sourceRect);
}
};