3

我正在尝试使用以下代码移动 winamps 主窗口:

[DllImport("user32.dll")]
    static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
[DllImport("user32.dll")]
    static extern bool GetWindowRect(IntPtr hWnd, out RECT rect);
[DllImport("user32.dll")]
    static extern IntPtr GetForegroundWindow();

static void resize()
{
   Process w = new Process();
   w.StartInfo = new ProcessStartInfo("winamp.exe");
   w.Start();
   Thread.Sleep(5000);
   IntPtr hWnd = GetForegroundWindow();
   RECT rect;
   GetWindowRect(hWnd, out rect);
   int width = rect.right - rect.left;
   int height = rect.bottom - rect.top;
   MoveWindow(hWnd, 0, 0, width, height, true);
}

此代码片段适用于我测试的所有进程,Winamp 除外。当我使用mainWindowHandle进程时,它会移动另一个窗口。有人知道如何让它与 Winamp 一起工作吗?

4

4 回答 4

1

我试过你的代码,它并不总是工作,它没有重新调整窗口大小,它只是移动它,实际上我在 2000 毫秒睡眠中发现了一个问题,你可以将其更改为 while 循环检查句柄是否为零,然后继续执行代码,

while (p.MainWindowHandle == 0)
{
}
IntPtr hWnd = p.MainWindowHandle;

如果 winamp 花费太多时间来构建自己的窗口,这可能会有所帮助

于 2012-11-08T08:11:50.380 回答
1

使用以下代码,我可以确认通过 Win32 API 更改 WinAmp 的主窗口大小不起作用。

通过 Win32 API 更改其他 winamp 窗口大小确实有效,但不是解决方案:

public partial class Form1 : Form
{    
[DllImport("user32")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr i);
[DllImport("user32.dll")]
static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
[DllImport("user32.dll")]
static extern bool GetWindowRect(IntPtr hWnd, out RECT rect);    
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
    public int Left;        // x position of upper-left corner
    public int Top;         // y position of upper-left corner
    public int Right;       // x position of lower-right corner
    public int Bottom;      // y position of lower-right corner
}    

private System.IntPtr hWnd;
private void button1_Click(object sender, EventArgs e)
{
    Process p = Process.Start(@"C:\Program Files\Winamp\winamp.exe");       
    try
    {
        do
        {
            p.Refresh();
        }
        while (p.MainWindowHandle.ToInt32() == 0);

        hWnd = new IntPtr(p.MainWindowHandle.ToInt32());
    }
    catch (Exception ex)
    {
        //Do some stuff...
        throw;
    }
}

private void button2_Click(object sender, EventArgs e)
{
    //Make sure we have a handle to the shelled exe
    if (hWnd == new IntPtr(0)) return;
    ResizeExternalExeChildWindows(hWnd);
}

private void ResizeExternalExeChildWindows(IntPtr parent)
{
    List<IntPtr> childWindows = GetChildWindows(hWnd);
    foreach (IntPtr childWindow in childWindows)
    {
        RECT rect;
        GetWindowRect(childWindow, out rect);
        int width = rect.Right - rect.Left;
        int height = rect.Bottom - rect.Top;
        MoveWindow(hWnd, 0, 0, width, height, true);
    }
}

// <summary>
/// Returns a list of child windows
/// </summary>
/// <param name="parent">Parent of the windows to return</param>
/// <returns>List of child windows</returns>
public static List<IntPtr> GetChildWindows(IntPtr parent)
{
    List<IntPtr> result = new List<IntPtr>();
    GCHandle listHandle = GCHandle.Alloc(result);
    try
    {
        EnumWindowProc childProc = new EnumWindowProc(EnumWindow);
        EnumChildWindows(parent, childProc, GCHandle.ToIntPtr(listHandle));
    }
    finally
    {
        if (listHandle.IsAllocated)
            listHandle.Free();
    }
    return result;
}

/// <summary>
/// Callback method to be used when enumerating windows.
/// </summary>
/// <param name="handle">Handle of the next window</param>
/// <param name="pointer">Pointer to a GCHandle that holds a reference to the list to fill</param>
/// <returns>True to continue the enumeration, false to bail</returns>
private static bool EnumWindow(IntPtr handle, IntPtr pointer)
{
    GCHandle gch = GCHandle.FromIntPtr(pointer);
    List<IntPtr> list = gch.Target as List<IntPtr>;
if (list == null)
{
    throw new InvalidCastException("GCHandle Target could not be cast as List<IntPtr>");
}
list.Add(handle);
//  You can modify this to check to see if you want to cancel the operation, then return a null here
return true;

}

/// <summary>
/// Delegate for the EnumChildWindows method
/// </summary>
/// <param name="hWnd">Window handle</param>
/// <param name="parameter">Caller-defined variable; we use it for a pointer to our list</param>
/// <returns>True to continue enumerating, false to bail.</returns>
public delegate bool EnumWindowProc(IntPtr hWnd, IntPtr parameter);

}
}

原因

因为 WinAmp 是可换肤的,它支持通过配置文件调整大小(而不是通过使用 win32 API 的外部应用程序)。

解决方案

打开文件 C:\Users[username]\AppData\Roaming\Winamp\studio.xnf 并编辑以下值:

  <entry name="Bento_nomax_h" value="492" />
  <entry name="Bento_nomax_w" value="633" />
  <entry name="Bento_nomax_x" value="27" />
  <entry name="Bento_nomax_y" value="16" />

我正在使用便当皮肤。如果您打开 WinAmp.ini 文件,您可以检测到正在使用的皮肤:

皮肤=便当

为 WinAmp 的主窗口设置初始大小的解决方案是在使用Process.Start.

于 2012-12-27T03:18:38.290 回答
1

这对我使用最新版本有用,请试一试:

    internal struct RECT
    {
        public int left;
        public int top;
        public int right;
        public int bottom;
    }

    [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, ExactSpelling = true, SetLastError = true)]
    internal static extern bool GetWindowRect(IntPtr hWnd, ref RECT rect);

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, SetWindowPosFlags uFlags);

    public static class HWND
    {
        public static IntPtr
        NoTopMost = new IntPtr(-2),
        TopMost = new IntPtr(-1),
        Top = new IntPtr(0),
        Bottom = new IntPtr(1);
    }

    [Flags]
    public enum SetWindowPosFlags : uint
    {
        SWP_ASYNCWINDOWPOS = 0x4000,
        SWP_DEFERERASE = 0x2000,
        SWP_DRAWFRAME = 0x0020,
        SWP_FRAMECHANGED = 0x0020,
        SWP_HIDEWINDOW = 0x0080,
        SWP_NOACTIVATE = 0x0010,
        SWP_NOCOPYBITS = 0x0100,
        SWP_NOMOVE = 0x0002,
        SWP_NOOWNERZORDER = 0x0200,
        SWP_NOREDRAW = 0x0008,
        SWP_NOREPOSITION = 0x0200,
        SWP_NOSENDCHANGING = 0x0400,
        SWP_NOSIZE = 0x0001,
        SWP_NOZORDER = 0x0004,
        SWP_SHOWWINDOW = 0x0040,
    }

    [DllImport("user32.dll", SetLastError = true)]
    private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

    static void Resize()
    {
        IntPtr winampMainWindow = IntPtr.Zero;

        while (true)
        {
            winampMainWindow = FindWindow("BaseWindow_RootWnd", "Main Window");
            if (winampMainWindow != IntPtr.Zero)
            {
                RECT rect = new RECT();
                GetWindowRect(winampMainWindow, ref rect);
                int width = rect.right - rect.left;
                int height = rect.bottom - rect.top;
                SetWindowPos(winampMainWindow,
                    HWND.Top,
                    0,
                    0,
                    width,
                    height,
                    SetWindowPosFlags.SWP_SHOWWINDOW | SetWindowPosFlags.SWP_NOZORDER | SetWindowPosFlags.SWP_NOSIZE | SetWindowPosFlags.SWP_NOSENDCHANGING | SetWindowPosFlags.SWP_NOOWNERZORDER | SetWindowPosFlags.SWP_NOCOPYBITS);
                break;
            }
            Thread.Sleep(1000);
        }
    }

    [STAThread]
    static void Main()
    {
        Process.Start("winamp.exe");
        Resize();
    }
于 2012-12-28T14:53:54.197 回答
0

绝对的噩梦!但是,右键单击“歌曲窗口”会打开停靠菜单-我认为取消停靠-(某些东西)有效。这些选项之一让我可以移动该死的东西。这是侥幸,但那里出现了一些有效的东西。

于 2021-02-19T19:34:41.177 回答