从控制台应用程序最小化和规范化winform

时间:2013-10-10 08:18:27

标签: c# winforms visual-studio-2010 console-application

我有两个独立的进程 - 一个是控制台应用程序,另一个是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上找到了相关信息,但感到困惑,所以会感激一些帮助。

由于

1 个答案:

答案 0 :(得分: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);
                    }

                }