Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tuj84257/ee92a1e000a3922d9d526e3ac603919b to your computer and use it in GitHub Desktop.
Save tuj84257/ee92a1e000a3922d9d526e3ac603919b 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:
// - [email protected]
// - [email protected]
// - [email protected]
// - [email protected]
/* // example in Electron main process.
const {
SetWindowPos,
HWND_BOTTOM,
SWP_SHOWWINDOW,
} = require('win-setwindowpos');
SetWindowPos(
win.getNativeWindowHandle(),
HWND_BOTTOM,
0,
700,
800,
600,
SWP_SHOWWINDOW
);
preventZOrderChange(win);
*/
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,
};
@Thesiva7
Copy link

Hi @tuj84257, while running your code i am getting TypeError: actualStructDataBuffer.writeUInt32LE is not a function
Could you help me on this?

@tuj84257
Copy link
Author

Hi @Thesiva7, I hope I am not responding too late. I think you might need to upgrade the version of node you're running.

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