Last active
January 26, 2022 01:05
-
-
Save fronterior/528a658d25e4357c07c8ea4f1b2cb46c to your computer and use it in GitHub Desktop.
WM_WINDOWPOSCHANGING Message Handler for Electron Application, which is always fixed on the floor.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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'); | |
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, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 hereEDIT:
So, I played with your code a bit, and found out that the example you provided works pretty well if you call
preventZOrderChange
afterSetWindowPos
. I am using Electron v16.