垂直平铺窗口c#

问题描述 投票:0回答:2

我有以下代码来垂直平铺窗口,但此代码的问题是它垂直平铺所有窗口,但我只想平铺此代码调用的要垂直平铺的资源管理器窗口和记事本窗口。 这是我们的一位内部客户的要求,他们希望同时打开文件资源管理器和记事本,并且在打开时它们应该并排垂直平铺。

有人可以帮忙吗

先谢谢了!!!

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

    [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
    internal static extern IntPtr GetForegroundWindow();

    [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", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, ExactSpelling = true, SetLastError = true)]
    internal static extern void MoveWindow(IntPtr hwnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
    [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, ExactSpelling = true, SetLastError = true)]
    static extern unsafe bool CascadeWindows(IntPtr hWnd, int wHow, RECT[] lpRect, uint cKids, IntPtr[] lpKids);
    [DllImport("user32.dll", EntryPoint = "SetWindowPos")]
    public static extern IntPtr SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);
    //fdfdfdf
    [DllImport("user32.dll", EntryPoint = "TileWindows")]
    static extern unsafe IntPtr TileWindows(IntPtr hWnd, int wHow, RECT[] lpRect, uint cKids, IntPtr[] lpKids);
    [DllImport("User32.dll")]

    static extern Int32 FindWindow(String lpClassName, String lpWindowName);    

    static void Main(string[] args)
    {
        const short SWP_NOMOVE = 0X2;
        const short SWP_NOSIZE = 1;
        const short SWP_NOZORDER = 0X4;
        const int SWP_SHOWWINDOW = 0x0040;
        const int MDITILE_SKIPDISABLED = 0x0002;
        const int MDITILE_ZORDER = 0x0004;
        const long WS_EX_TOPMOST = 0x00000008L;
        const int MDITILE_HORIZONTAL = 0x0001;
        const int MDITILE_VERTICAL = 0x0000;
        const short SWP_ASYNCWINDOWPOS = 0x4000;
        try
        {
            int hWnd;
            RECT Rect = new RECT();
            Process outlook = new Process();
            outlook.StartInfo.FileName = "notepad.exe";
            outlook.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            outlook.Start();
            IntPtr id = outlook.MainWindowHandle;
            if (id != IntPtr.Zero)
            {
                id = GetForegroundWindow();
                SetWindowPos(id, 0, 0, 0, 800, 900, SWP_NOZORDER | SWP_SHOWWINDOW | SWP_ASYNCWINDOWPOS);
            }
            Thread.Sleep(2000);
            Process explorer = new Process();
            explorer.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            explorer.StartInfo.FileName = "explorer.exe";
           explorer.Start();

            IntPtr id2 = explorer.MainWindowHandle;

            if (id2 != IntPtr.Zero)
            {
                id2 = GetForegroundWindow();
                SetWindowPos(id2, 0, -800, 0, 800, 900, SWP_NOZORDER | SWP_SHOWWINDOW);
            }

            TileWindows(id2, MDITILE_VERTICAL, null, 0, null);


            Console.Read();

        }
        catch (Exception ex)
        {

        }
}
c#
2个回答
1
投票

虽然我自己也在 C# 中做同样的事情,即将代码从 C++ 转换为 C#,所以我没有可用的代码,但我肯定可以提供一些有关如何使其工作的信息。

要平铺多个窗口,您需要在最后一个参数中提供它们的 HWND 值,即

IntPtr[]
lpKids,该函数将平铺 HWND 存在于 lpKids 数组中的所有窗口。 并且 uint cKids 必须包含 lpKids 数组中 HWND 的数量。

一旦这两个参数设置正确,它应该可以工作。

一旦我的代码顺利运行,我可能会发布工作代码。

 TileWindows((IntPtr)null, style, rect, (uint)hwndList.Count, lpKids);

哪里 (IntPtr)null 表示您指的是桌面窗口

样式为 MDITILE_VERTICAL 或 MDITILE_HORIZONTAL 以垂直或水平平铺

矩形是包含桌面窗口大小的矩形(x,y,宽度,高度)

(uint)hwndList.Count 是您要操作的 HWND 的数量。我使用函数 EnumDesktopWindows() 使用我自己的选择标准得到了这些

lpKids 是一个包含我想要操作的 HWND 的数组。

为了获得桌面矩形,我使用了:

    private RECT getRect()
    {
        RECT rect = new RECT();
        rect.y = 0;
        rect.x = 0;
        rect.width = SystemInformation.VirtualScreen.Width;
        rect.height = SystemInformation.VirtualScreen.Height;

        return rect;
    }

RECT 在哪里

        internal struct RECT
        {
            public int x;
            public int y;
            public int width;
            public int height;
        }

希望有帮助。


0
投票

我有一些代码,它确实有效,但它有副作用:

  • 所有其他窗口仍然受到一些影响,窗口状态从 最大化到正常,最小化到正常,依此类推。
  • Aero Snap 效果不起作用
 static class Program
    {
        [DllImport("user32.dll", SetLastError = true)] // https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-tilewindows
        private static extern int TileWindows(IntPtr hwndParent, uint wHow, Rect[] lpRect, uint cKids, IntPtr[] lpKids);

        internal struct Rect
        {
            public int x;
            public int y;
            public int width;
            public int height;
        }

        public static bool TileWindowsSideBySide1(Form form1, Form form2)
        {
            var windows = new[] { form1.Handle, form2.Handle };
            if (windows.Contains((IntPtr)null) || windows.Any(x => x == (IntPtr)0)) return false;

            var res = TileWindows(
                (IntPtr) null, // desktop is parent zone
                0, // MDITILE_HORIZONTAL 0x0001 Tiles windows horizontally;  MDITILE_VERTICAL 0x0000 Tiles windows vertically
                null, // desktop rect
                (uint) windows.Length, // number of windows
                windows //windows to be tiled
            );

            return res == windows.Length;
        }
}
© www.soinside.com 2019 - 2024. All rights reserved.