Skip to content

Instantly share code, notes, and snippets.

@turanegaku
Created July 18, 2016 04:52
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 turanegaku/fdb71894c9b2d35bbeee760e96d349ba to your computer and use it in GitHub Desktop.
Save turanegaku/fdb71894c9b2d35bbeee760e96d349ba to your computer and use it in GitHub Desktop.
foreground window script.
#include<queue>
#include<cstdio>
#include<windows.h>
using namespace std;
HWND findWindow(const char* name) {
queue<HWND> que;
que.push(GetDesktopWindow());
while(!que.empty()) {
HWND p = que.front(); que.pop();
int l = GetWindowTextLength(p);
char title[l];
GetWindowTextA(p, title, l);
if (strstr(title, name) ) {
return p;
}
for(HWND child = FindWindowEx(p, NULL, NULL, NULL); child != NULL;
child = FindWindowEx(p, child, NULL, NULL)){
que.push(child);
}
}
return NULL;
}
int main(int argv, char **args) {
if (argv < 2) {
printf("Usage: %s [name]\n", args[0]);
return 2;
}
HWND id = findWindow(args[1]);
if (id == NULL)
return 3;
return !SetForegroundWindow(id);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment