Skip to content

Instantly share code, notes, and snippets.

@strictlymike
Created November 21, 2019 18:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save strictlymike/753e5f29681ba2cb4df31a4f2412ef6f to your computer and use it in GitHub Desktop.
Save strictlymike/753e5f29681ba2cb4df31a4f2412ef6f to your computer and use it in GitHub Desktop.
Prevent Windows 10 from stealing the focus from the zip file password dialog
/* Prevent Windows 10 file transfer dialog ("0% complete") from stealing focus
* from "Password needed" dialog when copying files out of a password-protected
* zip file. */
#include <windows.h>
#include <stdio.h>
#pragma comment(lib, "user32")
#define MY_EVENT_MIN EVENT_OBJECT_CREATE
#define MY_EVENT_MAX EVENT_OBJECT_CREATE
#define MY_EVENT_KEY EVENT_OBJECT_CREATE
void winEventProc(
HWINEVENTHOOK hWinEventHook,
DWORD event,
HWND hwnd,
LONG idObject,
LONG idChild,
DWORD idEventThread,
DWORD dwmsEventTime
);
void keepOnTop(HWND hwnd);
int
main(void)
{
MSG Msg;
HWINEVENTHOOK hHook = NULL;
hHook = SetWinEventHook(MY_EVENT_MIN, MY_EVENT_MAX, NULL, (WINEVENTPROC)
winEventProc, 0 /*idProcess */, 0 /*idThread*/,
WINEVENT_OUTOFCONTEXT);
while (GetMessage(&Msg, NULL, 0, 0) > 0) {
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
UnhookWinEvent(hHook);
return 0;
}
void
keepOnTop(HWND hwnd)
{
BOOL Ok = FALSE;
Ok = SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0,
SWP_NOMOVE|SWP_NOSIZE);
while (Ok && IsWindow(hwnd)) {
Ok = SetForegroundWindow(hwnd);
Sleep(50);
}
}
void
winEventProc(
HWINEVENTHOOK hWinEventHook,
DWORD event,
HWND hwnd,
LONG idObject,
LONG idChild,
DWORD idEventThread,
DWORD dwmsEventTime
)
{
char title[128];
int ret = 0;
UNREFERENCED_PARAMETER(hWinEventHook);
UNREFERENCED_PARAMETER(idObject);
UNREFERENCED_PARAMETER(idChild);
UNREFERENCED_PARAMETER(idEventThread);
UNREFERENCED_PARAMETER(dwmsEventTime);
if (MY_EVENT_KEY == event) {
ret = GetWindowTextA(hwnd, title, sizeof(title));
if ((ret > 0) && (!strcmp(title, "Password needed"))) {
keepOnTop(hwnd);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment