Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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