windows切换窗口最常用的方法是alt+tab或者win+tab但是这么做需要一个个的查找,我希望可以通过键盘过滤窗口标题,然后跳转到指定窗口,为此写了如下解决方案 要求依赖 编译套件 mingw ,依赖的package ivy
/*
gcc -o dianswitch.exe dianswitch.c
*/
#include <windows.h>
#include <stdio.h>
BOOL CALLBACK enum_windows_callback(HWND hwnd, LPARAM lParam)
{
if (!IsWindowVisible(hwnd) || !IsWindowEnabled(hwnd))
{
return TRUE;
}
char title[1024];
if (GetWindowText(hwnd, title, sizeof(title)) > 0)
{
printf("%s\n", title);
}
return TRUE;
}
void switch_to_window(const char *title)
{
HWND hwnd = FindWindow(NULL, title);
if (hwnd == NULL)
{
printf("Window not found: %s\n", title);
return;
}
SetForegroundWindow(hwnd);
}
int main(int argc, char *argv[])
{
if (argc == 1)
{
EnumWindows(enum_windows_callback, 0);
}
else
{
switch_to_window(argv[1]);
}
return 0;
}
编译后的可执行文件放到环境变量下,然后创建一个elisp函数
(defun dian-switch ()
"Call dianswitch.exe and get user input from ivy-read"
(interactive)
(let ((default-process-coding-system '(gbk . gbk)))
(let* ((output-lines (process-lines "dianswitch.exe"))
(selected-line (ivy-read "Select a line: " output-lines)))
(when selected-line
(shell-command (concat "dianswitch.exe \"" selected-line "\""))
))))