Windows Manipulator - A Node.js module for manipulating windows in Windows OS.
wmnp is a Node.js addon that provides functionalities to interact with Windows OS windows. It allows you to get information about windows, manipulate their positions, sizes, visibility, and more.
-
Node.js 18.x or later
-
Python 2.7 or later (or Python 3.x)
-
Visual Studio Build Tools 2015 or later installed
You can download and install the Visual Studio Build Tools from here. During installation, select the "Desktop development with C++" workload.
-
Node-gyp installed globally
npm install -g node-gyp
npm install wmnpconst wmnp = require('wmnp');
// Get the active window
const activeWindow = wmnp.getActiveWindow();
// Get window dimensions
console.log(activeWindow.dimensions());
// Enumerate all windows
wmnp.enumerateWindows(window => {
console.log(`Title: "${window.getTitle()}"`);
console.log(window.dimensions());
});
// Get a window by class name
let notepadWin = wmnp.getWindowByClassName('Notepad');
console.log(notepadWin.getTitle());
console.log(notepadWin.dimensions());
// Get a window by title
notepadWin = wmnp.getWindowByTitle('Untitled - Notepad');
console.log(notepadWin.getTitle());
console.log(notepadWin.dimensions());
// Check if a window exists
let wnd = wmnp.getActiveWindow();
console.log(wnd.exists());
// Use Window constructor
wnd = new wmnp.Window(0x00000000); // Invalid hwnd
console.log(wnd.exists()); // false
wnd = wmnp.getActiveWindow();
const hwnd = wnd.getHwnd();
wnd = new wmnp.Window(hwnd); // Valid hwnd
console.log(wnd.exists()); // true
// Get window class name
console.log(wnd.getClassName());
// Get process ID of the window
console.log(wnd.getPid());
// Get parent window handle
console.log(wnd.getParentHwnd());
// Get ancestor window
const GA_PARENT = 1, GA_ROOT = 2, GA_ROOTOWNER = 3;
console.log(wnd.getAncestor(GA_PARENT));
console.log(wnd.getAncestor(GA_ROOT));
console.log(wnd.getAncestor(GA_ROOTOWNER));
// Get monitor information
console.log(wnd.getMonitor());
// Set a window to the foreground
wnd.setForegroundWindow();
// Set window position
const hwndInsertAfter = 0; // HWND_TOP
const uFlags = 0x0000 | 0x0040; // SWP_SHOWWINDOW
let x = 100, y = 100, width = 400, height = 400;
wnd.setWindowPos(hwndInsertAfter, x, y, width, height, uFlags);
// Show the window
const nCmdShow = 1; // SW_SHOWNORMAL
wnd.showWindow(nCmdShow);
// Move the window
wnd.move(x, y, width, height);
// Move the window relative to its current position
let dx = 100, dy = 100, dw = 40, dh = 40;
wnd.moveRelative(dx, dy, dw, dh);Returns the active window as a Window object.
Enumerates all top-level windows and calls the provided callback function with each window.
Returns a Window object for the first window that matches the given class name.
Returns a Window object for the first window that matches the given title.
Creates a new Window object with the specified window handle (hwnd).
getHwnd(): Returns the window handle.getTitle(): Returns the window title.getClassName(): Returns the window class name.getPid(): Returns the process ID of the window.exists(): Checks if the window exists.dimensions(): Returns an object containing the window's dimensions.getParentHwnd(): Returns the parent window handle.getAncestor(gaFlags): Returns the ancestor window handle based on thegaFlags.getMonitor(): Returns information about the monitor that the window is on.setForegroundWindow(): Sets the window to the foreground.setWindowPos(hwndInsertAfter, x, y, cx, cy, uFlags): Sets the window's position.showWindow(nCmdShow): Shows or hides the window based onnCmdShow.move(x, y, width, height): Moves and resizes the window.moveRelative(dx, dy, dw, dh): Moves and resizes the window relative to its current position and size.
GA_PARENT = 1GA_ROOT = 2GA_ROOTOWNER = 3
HWND_TOP = 0HWND_BOTTOM = 1HWND_TOPMOST = -1HWND_NOTOPMOST = -2
SWP_NOSIZE = 0x0001SWP_NOMOVE = 0x0002SWP_NOZORDER = 0x0004SWP_NOREDRAW = 0x0008SWP_NOACTIVATE = 0x0010SWP_FRAMECHANGED = 0x0020SWP_SHOWWINDOW = 0x0040SWP_HIDEWINDOW = 0x0080SWP_NOCOPYBITS = 0x0100SWP_NOOWNERZORDER = 0x0200SWP_NOSENDCHANGING = 0x0400SWP_DRAWFRAME = SWP_FRAMECHANGEDSWP_NOREPOSITION = SWP_NOOWNERZORDERSWP_DEFERERASE = 0x2000SWP_ASYNCWINDOWPOS = 0x4000
SW_HIDE = 0SW_SHOWNORMAL = 1SW_SHOWMINIMIZED = 2SW_SHOWMAXIMIZED = 3SW_SHOWNOACTIVATE = 4SW_SHOW = 5SW_MINIMIZE = 6SW_SHOWMINNOACTIVE = 7SW_SHOWNA = 8SW_RESTORE = 9SW_SHOWDEFAULT = 10SW_FORCEMINIMIZE = 11
To build the project, run:
npm run buildnode-addon-apinode-gyp
Zoran Banković - zoranbankovic@gmail.com
MIT