Skip to content

Instantly share code, notes, and snippets.

@lowfront
Last active January 26, 2022 01:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lowfront/528a658d25e4357c07c8ea4f1b2cb46c to your computer and use it in GitHub Desktop.
Save lowfront/528a658d25e4357c07c8ea4f1b2cb46c to your computer and use it in GitHub Desktop.
WM_WINDOWPOSCHANGING Message Handler for Electron Application, which is always fixed on the floor.
// Node.js & Electron reference : https://stackoverflow.com/a/58473299
// C++ reference : https://stackoverflow.com/a/65052538
// Environment:
// - Windows 10
// - Nodejs v14.17.6
// Dependencies:
// - electron@14.0.1
// - ref-napi@3.0.3
// - ref-struct-di@1.1.1
// - win-setwindowpos@2.1.0
/* // example in Electron main process.
const {
SetWindowPos,
HWND_BOTTOM,
SWP_SHOWWINDOW,
} = require('win-setwindowpos');
preventZOrderChange(win);
SetWindowPos(
win.getNativeWindowHandle(),
HWND_BOTTOM,
0,
700,
800,
600,
SWP_SHOWWINDOW
);
*/
const ref = require('ref-napi');
const StructType = require('ref-struct-di')(ref);
const {
SetWindowPos,
HWND_BOTTOM,
SWP_NOACTIVATE,
SWP_NOSIZE,
SWP_NOMOVE,
SWP_NOZORDER,
SWP_NOSENDCHANGING,
} = require('win-setwindowpos');
const WINDOWPOS = StructType({
hwnd: ref.types.int32,
hwndInsertAfter: ref.types.int32,
x: ref.types.int32,
y: ref.types.int32,
cx: ref.types.int32,
cy: ref.types.int32,
flags: ref.types.uint32,
});
const WM_WINDOWPOSCHANGING = 0x0046;
const preventZOrderChange = win => {
win.hookWindowMessage(WM_WINDOWPOSCHANGING, (wParam, lParam)=> {
const buf = Buffer.alloc(8);
buf.type = ref.refType(WINDOWPOS);
lParam.copy(buf);
const actualStructDataBuffer = buf.deref();
const windowPos = actualStructDataBuffer.deref();
const newFlags = windowPos.flags | SWP_NOZORDER | SWP_NOMOVE | SWP_NOSIZE;
actualStructDataBuffer.writeUInt32LE(newFlags, 6);
actualStructDataBuffer.writeUInt32LE(HWND_BOTTOM, 1);
});
};
module.exports = {
WINDOWPOS,
WM_WINDOWPOSCHANGING,
preventZOrderChange,
};
@tuj84257
Copy link

tuj84257 commented Dec 19, 2021

Hey @lowfront, thank you for creating this gist. Could you please provide a more thorough sample code? I was not able to send the window to bottom using the example you provided here

EDIT:

So, I played with your code a bit, and found out that the example you provided works pretty well if you call preventZOrderChange after SetWindowPos. I am using Electron v16.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment