获取窗口区域 - GetWindowRect
函数简介
获取指定窗口的屏幕坐标区域。该函数返回窗口的左上角和右下角坐标,这些坐标是相对于屏幕左上角的绝对坐标。返回的区域包括窗口的标题栏、边框和客户区。
接口名称
GetWindowRect
DLL调用
int GetWindowRect(long ola, long hwnd, int* x1, int* y1, int* x2, int* y2)
参数定义:
ola
(长整型数): OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。hwnd
(长整型数): 目标窗口的句柄。x1
(整型数指针): 返回窗口左上角的X坐标。y1
(整型数指针): 返回窗口左上角的Y坐标。x2
(整型数指针): 返回窗口右下角的X坐标。y2
(整型数指针): 返回窗口右下角的Y坐标。
示例:
// 获取窗口区域
int x1, y1, x2, y2;
int ret = GetWindowRect(ola, hwnd, &x1, &y1, &x2, &y2);
if (ret == 1) {
printf("窗口位置:左上角(%d, %d), 右下角(%d, %d)\n", x1, y1, x2, y2);
printf("窗口大小:宽度=%d, 高度=%d\n", x2 - x1, y2 - y1);
} else {
printf("获取窗口区域失败\n");
}
// 判断鼠标是否在窗口区域内
int mouse_x, mouse_y;
GetCursorPos(&mouse_x, &mouse_y);
if (mouse_x >= x1 && mouse_x <= x2 && mouse_y >= y1 && mouse_y <= y2) {
printf("鼠标在窗口区域内\n");
}
// 将窗口移动到屏幕中心
int screen_width = GetSystemMetrics(SM_CXSCREEN);
int screen_height = GetSystemMetrics(SM_CYSCREEN);
int window_width = x2 - x1;
int window_height = y2 - y1;
MoveWindow(ola, hwnd,
(screen_width - window_width) / 2,
(screen_height - window_height) / 2);
COM调用
int GetWindowRect(long hwnd, int* x1, int* y1, int* x2, int* y2)
参数定义:
hwnd
(长整型数): 目标窗口的句柄。x1
(整型数指针): 返回窗口左上角的X坐标。y1
(整型数指针): 返回窗口左上角的Y坐标。x2
(整型数指针): 返回窗口右下角的X坐标。y2
(整型数指针): 返回窗口右下角的Y坐标。
示例:
# 获取窗口区域
x1, y1, x2, y2 = 0, 0, 0, 0
ret = ola.GetWindowRect(hwnd, x1, y1, x2, y2)
if ret == 1:
messagebox(f"窗口位置:左上角({x1}, {y1}), 右下角({x2}, {y2})")
messagebox(f"窗口大小:宽度={x2 - x1}, 高度={y2 - y1}")
else:
messagebox("获取窗口区域失败")
# 判断鼠标是否在窗口区域内
import win32api
mouse_x, mouse_y = win32api.GetCursorPos()
if mouse_x >= x1 and mouse_x <= x2 and mouse_y >= y1 and mouse_y <= y2:
messagebox("鼠标在窗口区域内")
# 将窗口移动到屏幕中心
screen_width = win32api.GetSystemMetrics(0)
screen_height = win32api.GetSystemMetrics(1)
window_width = x2 - x1
window_height = y2 - y1
ola.MoveWindow(hwnd,
(screen_width - window_width) // 2,
(screen_height - window_height) // 2)
返回值
整型数:
- 0: 获取失败
- 1: 获取成功
注意事项
- 窗口必须处于可见状态,否则获取可能失败
- 返回的坐标是相对于屏幕左上角的绝对坐标
- 返回的区域包括窗口的非客户区(标题栏、边框等)
- 如果只需要获取客户区域,请使用 GetClientRect 函数
- 对于多显示器系统,坐标值可能为负数,这表示窗口位于主显示器左侧或上方的显示器上