Skip to content

Instantly share code, notes, and snippets.

@mmozeiko mmozeiko/test.c
Last active Jun 25, 2019

Embed
What would you like to do?
SetForegroundWindow
#include <windows.h>
#include <stdio.h>
LRESULT CALLBACK WindowProc(HWND wnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
if (msg == WM_TIMER)
{
printf("press SPACE now\n");
DWORD tid = GetWindowThreadProcessId(wnd, NULL);
AttachThreadInput(tid, GetCurrentThreadId(), TRUE);
SetWindowPos(wnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
SetWindowPos(wnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
SetForegroundWindow(wnd);
SetActiveWindow(wnd);
SetFocus(wnd);
AttachThreadInput(tid, GetCurrentThreadId(), FALSE);
return 0;
}
else if (msg == WM_KEYDOWN && wparam == VK_SPACE)
{
MessageBoxW(wnd, L"SPACE pressed", L"title", MB_OK);
return 0;
}
else if (msg == WM_DESTROY)
{
PostQuitMessage(0);
return 0;
}
return DefWindowProcW(wnd, msg, wparam, lparam);
}
int main()
{
WNDCLASSEXW wc =
{
.cbSize = sizeof(wc),
.lpfnWndProc = WindowProc,
.lpszClassName = L"MY_CLASS_NAME",
};
RegisterClassExW(&wc);
HWND w = CreateWindowExW(WS_EX_APPWINDOW, wc.lpszClassName, L"title", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100, 100, 200, 200, NULL, NULL, NULL, NULL);
SetTimer(w, 1, 5 * 1000, NULL);
BOOL r;
MSG msg;
while ((r = GetMessageW(&msg, NULL, 0, 0)) > 0)
{
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.