用c语言写获取当前窗口句柄的代码
时间: 2023-05-10 22:55:54 浏览: 257
以下是获取当前窗口句柄的 C 语言代码:
```c
#include <Windows.h>
int main() {
HWND hwnd = GetForegroundWindow();
printf("当前窗口句柄为:%p\n", hwnd);
return 0;
}
```
该代码使用了 Windows API 中的 `GetForegroundWindow()` 函数,可以获取当前活动窗口的句柄。
相关问题
用C++获取当前gmsh窗口
目前,C语言没有直接获取当前GM窗口的API。但是,可以通过以下方法获取当前活动窗口的句柄:
1. 使用GetForegroundWindow函数获取当前活动窗口的句柄。
2. 使用GetClassName函数获取当前活动窗口的类名。
3. 判断类名是否为"YYGMWindowClass",如果是,则当前窗口就是GM窗口。
下面是一个示例代码:
```c
#include <windows.h>
#include <stdio.h>
int main() {
HWND hwnd = GetForegroundWindow();
char class_name[256];
GetClassName(hwnd, class_name, sizeof(class_name));
if (strcmp(class_name, "YYGMWindowClass") == 0) {
printf("当前窗口是GM窗口\n");
} else {
printf("当前窗口不是GM窗口\n");
}
return 0;
}
```
获取当前窗口的鼠标位置并输出
以下是一个使用C语言获取当前窗口的鼠标位置并输出的示例程序:
```c
#include <stdio.h>
#include <windows.h>
int main() {
// 获取当前窗口句柄
HWND windowHandle = GetForegroundWindow();
// 获取鼠标位置
POINT cursorPos;
GetCursorPos(&cursorPos);
// 将鼠标位置转换为窗口内坐标
ScreenToClient(windowHandle, &cursorPos);
// 输出鼠标位置
printf("当前窗口内鼠标位置:(%d, %d)\n", cursorPos.x, cursorPos.y);
return 0;
}
```
上述代码使用Windows API函数`GetForegroundWindow`获取当前窗口句柄,然后使用`GetCursorPos`函数获取鼠标的屏幕坐标。接下来,使用`ScreenToClient`函数将鼠标位置转换为相对于窗口内的坐标。最后,通过`printf`函数输出鼠标位置。
请注意,此代码仅为示例,实际使用时可能需要进行错误处理和适当的修改以满足具体需求。