win32 | 使用SetWindowPos置顶窗口
win32 | 使用SetWindowPos置顶窗口
写了个置顶窗口的命令行小工具。
很舒服,唯一不好的是有的时候通过窗口名找不到窗口,这个就比较麻烦哈哈哈哈。
// this file is gbk encoded
// author: Mz1
#include <windows.h>
#include <stdio.h>
HWND g_hWnd = NULL;
char g_window_name[255] = {0};
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
char szTitle[MAX_PATH] = { 0 };
GetWindowText(hwnd, szTitle, MAX_PATH);
// printf("%s\n", szTitle);
if (strcmp(szTitle, g_window_name) == 0)
{
printf("> find %s. \n", szTitle);
g_hWnd = hwnd;
return FALSE; // 停止枚举
}
return TRUE;
}
void show_usage(){
puts("> usage: mztopper.exe pin [window_name]");
puts("> usage: mztopper.exe unpin [window_name]");
}
int main(int argc, char* argv[]){
if (argc < 3){
show_usage();
exit(-2);
}
strcpy(g_window_name, argv[2]);
EnumWindows(EnumWindowsProc, NULL); // 枚举并查找窗口
if (g_hWnd == NULL){
printf("not find this window. \n");
exit(-1);
}
if (strcmp(argv[1], "pin") == 0){
// 置顶窗口
SetWindowPos(g_hWnd, HWND_TOPMOST,
0, 0,
0, 0,
SWP_NOMOVE | SWP_NOSIZE
);
printf("> %s is on top now. \n", g_window_name);
}else if (strcmp(argv[1], "unpin") == 0){
SetWindowPos(g_hWnd, HWND_NOTOPMOST,
0, 0,
0, 0,
SWP_NOMOVE | SWP_NOSIZE
);
printf("> %s is remove from top now. \n", g_window_name);
}else{
show_usage();
}
return 0;
}
本文来自博客园,作者:Mz1,转载请注明原文链接:https://www.cnblogs.com/Mz1-rc/p/17058860.html
如果有问题可以在下方评论或者email:mzi_mzi@163.com
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异