C# 从控制台应用程序最小化并规范化winform
我有两个独立的进程——一个是控制台应用程序,另一个是winform应用程序。现在,如果winform最小化,那么控制台应用程序应该对其进行规范化,反之亦然。我该怎么做?此外,控制台应用程序启动winform,并且在启动时它应该处于其规范化位置。我怎样才能做到这一点呢C# 从控制台应用程序最小化并规范化winform,c#,winforms,visual-studio-2010,console-application,C#,Winforms,Visual Studio 2010,Console Application,我有两个独立的进程——一个是控制台应用程序,另一个是winform应用程序。现在,如果winform最小化,那么控制台应用程序应该对其进行规范化,反之亦然。我该怎么做?此外,控制台应用程序启动winform,并且在启动时它应该处于其规范化位置。我怎样才能做到这一点呢 var processes = Process.GetProcessesByName("MyWinformApp"); if (processes.Length == 0)
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上找到了一些信息,但是我很困惑,所以希望能得到一些帮助
谢谢下面的代码就是这样做的,问题的另一部分由上面的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);
}
}