垂直平铺视窗c#

本文关键字:垂直 | 更新日期: 2023-09-27 18:09:48

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

有人能帮帮忙吗?

谢谢提前! !

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#

虽然我自己在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垂直或水平平铺

rect是包含桌面窗口大小的矩形(x,y,width,height)

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;
        }