0

我有两个独立的进程——一个是控制台应用程序,一个是 winform 应用程序。现在,如果 winform 被最小化,那么控制台应用程序应该对其进行规范化,反之亦然。我怎样才能做到这一点?此外,控制台应用程序启动 winform 并且在启动时它应该处于其规范化位置。我该如何在以下几行中解决这个问题

     var processes = Process.GetProcessesByName("MyWinformApp");
                if (processes.Length == 0)
                {
                    Process.Start("MyWinformApp.exe");
    How to be sure that the winform will open up in Normalized state
                }
                else 
                {
                    IntPtr handle = processes[0].MainWindowHandle;
    //If winform minimized the normalize and vice versa ....What to do here
//Maybe use GetWindowPlacement 
?? 
handle = processes[0].MainWindowHandle;
            if(if winform was minimized) //how to find this???
            {
                ShowWindow(handle, Normal);
            }
            else
            {
                ShowWindow(handle, Minimize);
            }

                }

我确实在 pinvoke.net 上找到了信息,但很困惑,所以希望能得到一些帮助。

谢谢

4

1 回答 1

0

下面的代码可以做到这一点,问题的另一部分由上面的 DeeMac 回答

 #region For getting the hadnle and min/max operation
        private const int SW_Minimize  = 6;
        private const int SW_Normal    = 1;

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

        private static WINDOWPLACEMENT GetPlacement(IntPtr hwnd)
        {
            WINDOWPLACEMENT placement = new WINDOWPLACEMENT();
            placement.length = Marshal.SizeOf(placement);
            GetWindowPlacement(hwnd, ref placement);
            return placement;
        }

        [DllImport("user32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        internal static extern bool GetWindowPlacement(
            IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);

        [Serializable]
        [StructLayout(LayoutKind.Sequential)]
        internal struct WINDOWPLACEMENT
        {
            public int length;
            public int flags;
            public ShowWindowCommands showCmd;
            public System.Drawing.Point ptMinPosition;
            public System.Drawing.Point ptMaxPosition;
            public System.Drawing.Rectangle rcNormalPosition;
        }

        internal enum ShowWindowCommands : int
        {
            Hide = 0,
            Normal = 1,
            Minimized = 2,
            Maximized = 3,
        }
        #endregion


 var processes = Process.GetProcessesByName("MyWinformApp");
                if (processes.Length == 0)
                {
                    Process.Start("MyWinformApp.exe");
                }
                else 
                {
                    IntPtr handle = processes[0].MainWindowHandle;
                    var placement = GetPlacement(handle);
                    if (String.Equals(placement.showCmd.ToString(), "Minimized")) 
                    {
                        ShowWindow(handle, SW_Normal);
                    }
                    else
                    {
                        ShowWindow(handle, SW_Minimize);
                    }

                }
于 2013-10-10T09:50:34.100 回答