Skip to content

Commit 134ea87

Browse files
committed
Add getMonitorFromWindow and getScaleFactor methods
1 parent 4c14ce0 commit 134ea87

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

src/bindings/windows.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ export const user32 = new ffi.Library("User32.dll", {
4747
// https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-bringwindowtotop
4848
SetForegroundWindow: ["bool", ["int64"]],
4949
// https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-setlayeredwindowattributes
50-
SetLayeredWindowAttributes: ["bool", ["int64", "int", "int", "int64"]]
50+
SetLayeredWindowAttributes: ["bool", ["int64", "int", "int", "int64"]],
51+
// https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-monitorfromwindow
52+
MonitorFromWindow: ["int64", ["int64", "int64"]]
5153
});
5254

5355
export const kernel32 = new ffi.Library("kernel32", {
@@ -62,6 +64,10 @@ export const kernel32 = new ffi.Library("kernel32", {
6264
]
6365
});
6466

67+
export const shellScaling = new ffi.Library("SHCore.dll", {
68+
GetScaleFactorForMonitor: ["int64", ["int64", "int*"]]
69+
});
70+
6571
export const getProcessId = (handle: number) => {
6672
const processIdBuffer = ref.alloc("uint32");
6773
user32.GetWindowThreadProcessId(handle, processIdBuffer);
@@ -120,3 +126,5 @@ export const getWindowBounds = (handle: number) => {
120126
height: bounds.bottom - bounds.top
121127
};
122128
};
129+
130+
export const getScaleFactor = (handle: number) => {};

src/classes/window.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ import {
44
getProcessPath,
55
getWindowBounds,
66
getWindowTitle,
7-
user32
7+
user32,
8+
shellScaling,
9+
getScaleFactor
810
} from "../bindings/windows";
911
import { basename } from "path";
12+
import { windowManager } from "..";
1013

1114
const ffi = require("ffi");
1215

@@ -58,6 +61,7 @@ export class Window {
5861

5962
setBounds(bounds: Rectangle) {
6063
const { x, y, height, width } = { ...this.getBounds(), ...bounds };
64+
6165
user32.MoveWindow(this.handle, x, y, width, height, true);
6266
}
6367

src/index.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import { Window } from "./classes/window";
22
import { EventEmitter } from "events";
3-
import { getActiveWindowHandle, user32 } from "./bindings/windows";
3+
import {
4+
getActiveWindowHandle,
5+
user32,
6+
shellScaling
7+
} from "./bindings/windows";
48

59
const ffi = require("ffi");
10+
const ref = require("ref");
611

712
let interval: any = null;
813

@@ -61,6 +66,17 @@ class WindowManager extends EventEmitter {
6166
user32.EnumWindows(callback, 0);
6267
return windows;
6368
};
69+
70+
getMonitorFromWindow = (window: Window) => {
71+
return user32.MonitorFromWindow(window.handle, 0);
72+
};
73+
74+
getScaleFactor = (monitor: number) => {
75+
const sfRef = ref.alloc("int");
76+
shellScaling.GetScaleFactorForMonitor(monitor, sfRef);
77+
78+
return sfRef.deref() / 100;
79+
};
6480
}
6581

6682
const windowManager = new WindowManager();

0 commit comments

Comments
 (0)