移动窗口 - MoveWindow
函数简介
移动指定窗口到指定位置。该函数会将窗口的左上角移动到指定的屏幕坐标位置,保持窗口的当前大小不变。坐标是相对于屏幕左上角的绝对坐标。
接口名称
MoveWindow
DLL调用
int MoveWindow(long ola, long hwnd, int x, int y)
参数定义:
ola
(长整型数): OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。hwnd
(长整型数): 目标窗口的句柄。x
(整型数): 窗口左上角的目标X坐标(屏幕坐标)。y
(整型数): 窗口左上角的目标Y坐标(屏幕坐标)。
示例:
// 移动窗口到屏幕坐标(100, 200)
int ret = MoveWindow(ola, hwnd, 100, 200);
if (ret == 1) {
printf("窗口移动成功\n");
} else {
printf("窗口移动失败\n");
}
// 获取当前窗口位置并向右移动100像素
int x, y;
GetWindowRect(hwnd, &x, &y, NULL, NULL);
ret = MoveWindow(ola, hwnd, x + 100, y);
if (ret == 1) {
printf("窗口向右移动成功\n");
}
// 将窗口移动到屏幕中心
int screen_width = GetSystemMetrics(SM_CXSCREEN);
int screen_height = GetSystemMetrics(SM_CYSCREEN);
int window_width, window_height;
GetWindowRect(hwnd, NULL, NULL, &window_width, &window_height);
ret = MoveWindow(ola, hwnd,
(screen_width - window_width) / 2,
(screen_height - window_height) / 2);
if (ret == 1) {
printf("窗口居中成功\n");
}
COM调用
int MoveWindow(long hwnd, int x, int y)
参数定义:
hwnd
(长整型数): 目标窗口的句柄。x
(整型数): 窗口左上角的目标X坐标(屏幕坐标)。y
(整型数): 窗口左上角的目标Y坐标(屏幕坐标)。
示例:
# 移动窗口到屏幕坐标(100, 200)
ret = ola.MoveWindow(hwnd, 100, 200)
if ret == 1:
messagebox("窗口移动成功")
else:
messagebox("窗口移动失败")
# 获取当前窗口位置并向右移动100像素
import win32gui
x, y, _, _ = win32gui.GetWindowRect(hwnd)
ret = ola.MoveWindow(hwnd, x + 100, y)
if ret == 1:
messagebox("窗口向右移动成功")
# 将窗口移动到屏幕中心
import win32api
screen_width = win32api.GetSystemMetrics(0)
screen_height = win32api.GetSystemMetrics(1)
_, _, window_width, window_height = win32gui.GetWindowRect(hwnd)
ret = ola.MoveWindow(hwnd,
(screen_width - window_width) // 2,
(screen_height - window_height) // 2)
if ret == 1:
messagebox("窗口居中成功")
返回值
整型数:
- 0: 移动失败
- 1: 移动成功
注意事项
- 窗口必须处于可见状态,否则移动可能失败
- 坐标是相对于屏幕左上角的绝对坐标
- 移动窗口不会改变窗口的大小
- 建议在使用此函数前,先使用 GetWindowRect 函数获取窗口当前位置
- 如果需要同时改变窗口的大小和位置,可以使用 SetWindowSize 函数