I have a project exclusively for Windows that needs the following:
1 - Get the active window
2 - Get the dimension of the active window
3 - From the dimension I get, update the (x, y, w, h)
4 - Set the active window to 'restore' mode
5 - Set the new position of the active window
I am looking for any modules that can do this and found winctl but I can't make it work.
QUESTION
Are there any modules that can access the Windows active window?
If there is, can you please provide a code sample?
Thank you in advance!
This is how I solve my issue by using ffi-napi .
I added comments on the code to make it easy to understand.
const ffi = require('ffi-napi');
// create foreign function
const user32 = new ffi.Library('user32', {
'GetForegroundWindow': ['long', []],
'ShowWindow': ['bool', ['long', 'int']],
'GetWindowRect': ['bool', ['long', 'pointer']],
'SetWindowPos': ['bool', ['long', 'long', 'int', 'int', 'int', 'int', 'uint']]
});
// create rectangle from pointer
const pointerToRect = function (rectPointer) {
const rect = {};
rect.left = rectPointer.readInt16LE(0);
rect.top = rectPointer.readInt16LE(4);
rect.right = rectPointer.readInt16LE(8);
rect.bottom = rectPointer.readInt16LE(12);
return rect;
}
// obtain window dimension
const getWindowDimensions = function (handle) {
const rectPointer = Buffer.alloc(16);
const getWindowRect = user32.GetWindowRect(handle, rectPointer);
return !getWindowRect
? null
: pointerToRect(rectPointer);
}
// get active window
const activeWindow = user32.GetForegroundWindow();
// get window dimension
const activeWindowDimensions = getWindowDimensions(activeWindow);
// get active window width and height
const activeWindowWidth = activeWindowDimensions.right - activeWindowDimensions.left;
const activeWindowHeight = activeWindowDimensions.bottom - activeWindowDimensions.top;
// center the active window on the screen
const activeWindowX = (1920 / 2) - (activeWindowWidth / 2);
const activeWindowY = (1080 / 2) - activeWindowHeight / 2);
// force active window to restore mode
user32.ShowWindow(activeWindow, 9);
// set window position based on bounds values
user32.SetWindowPos(
activeWindow,
0,
activeWindowX,
activeWindowY,
activeWindowWidth,
activeWindowHeight,
0x4000 | 0x0020 | 0x0020 | 0x0040
);
I use this code on my small project called Sōkan .
Perhaps you can get or set to window position by using remote
and getFocusedWindow()
.
const remote = require('electron').remote;
let win = remote.BrowserWindow.getFocusedWindow();
BrowserWindow.getFocusedWindow()
https://www.electronjs.org/docs/api/browser-window#browserwindowgetfocusedwindow
console.log(win.getPosition());
win.setPosition(0, 0);
win.getPosition()
https://www.electronjs.org/docs/api/browser-window#wingetposition
win.setPosition()
https://www.electronjs.org/docs/api/browser-window#winsetpositionx-y-animate
But be careful when using remote
. Currently, Electron makes it a security recommendation with "Disable the remote module".
Checklist: Security Recommendations
https://www.electronjs.org/docs/tutorial/security#checklist-security-recommendations
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.