移动窗口时触发事件

本文关键字:事件 窗口 移动 | 更新日期: 2023-09-27 17:55:17

我目前正在开发一个iTunes插件,我需要让我的WPF插件在拖动,调整大小等时粘在iTunes窗口旁边。目标是让iTunes并排粘在我的wpf应用程序上。

我正在寻找一种方法来跟踪另一个窗口(在我的例子中是iTunes)的移动(调整大小,移动)。适用于Windows SDK的iTunes COM提供了最大化和最小化的事件,不幸的是,没有用于调整大小和移动窗口的事件。

我已经尝试了win32 setParent函数,但没有成功。我认为这不是我问题的合适解决方案。我已经在网上彻底搜索了一下,但没有找到任何东西。

移动窗口时触发事件

我认为WINDOWPOS结构就是你要找的。 其他窗口结构可能会派上用场。

一些谷歌搜索发现了另一个不使用WINDOWPOS的例子:

1、调用 API FindWindow() 检索窗口句柄(Uuse SPY++ 获取两个参数 ClassName 和 WindowName);

2、调用接口 GetWindowRect() 检索指定的大小和位置 窗。

代码片段

    [DllImport("user32.dll")]
    private static extern IntPtr FindWindow(string className, string windowName);
    [DllImport("user32.dll")]
    private static extern int GetWindowRect(IntPtr hwnd, out Rectangle rect);
    private void button2_Click(object sender, EventArgs e)
    {
        string className = "yourClassName";
        string windowName = "yourWindowName";
        Rectangle rect;
        IntPtr hwnd = FindWindow(className, windowName);
        GetWindowRect(hwnd, out rect);
    }