C# 如何在c语言中最小化/恢复窗口#

C# 如何在c语言中最小化/恢复窗口#,c#,pinvoke,native,C#,Pinvoke,Native,我试图最小化标题位于字符串p中的窗口(由用户在运行时指定)。该窗口保证是主窗口,因为用户只能从主窗口中进行选择。我尝试过showCmd、flags和两者的混合,但是每次,无论窗口是否最小化,我都会看到一个对话框,其中说明“最小化”。我怎样才能修好它 private void button1_Click(object sender, EventArgs e) { foreach (Process proc in Process.GetProcesses()) { i

我试图最小化标题位于
字符串p
中的窗口(由用户在运行时指定)。该窗口保证是主窗口,因为用户只能从主窗口中进行选择。我尝试过showCmd、flags和两者的混合,但是每次,无论窗口是否最小化,我都会看到一个对话框,其中说明
“最小化”
。我怎样才能修好它

private void button1_Click(object sender, EventArgs e)
{
    foreach (Process proc in Process.GetProcesses())
    {
        if (proc.MainWindowTitle.Contains(p))
        {
            IntPtr handle = proc.Handle;
            Program.WINDOWPLACEMENT wp = new Program.WINDOWPLACEMENT();
            Program.GetWindowPlacement(handle, ref wp);
            if ((wp.showCmd & 2) == 2)
            {
                wp.showCmd = 9;
                MessageBox.Show("Restoring");
            }
            else
            {
                wp.showCmd = 2;
                MessageBox.Show("Minimizing");
            }
            wp.length = Marshal.SizeOf(wp);
            Program.SetWindowPlacement(handle, ref wp);
            break;
        }
    }  

}
我用什么作为p-

string p;
PictureBox i;
bool windowShowing = false;
bool minimized = false;
public TaskbarItem(string window)
{
    InitializeComponent();
    p = window;
    button1.Text = window;
}
要获取窗口,请执行以下操作:

class WindowEnumerator
{
    public delegate bool EnumDelegate(IntPtr hWnd, int lParam);

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool IsWindowVisible(IntPtr hWnd);

    [DllImport("user32.dll", EntryPoint = "GetWindowText", ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
    public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpWindowText, int nMaxCount);

    [DllImport("user32.dll", EntryPoint = "EnumDesktopWindows", ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
    public static extern bool EnumDesktopWindows(IntPtr hDesktop, EnumDelegate lpEnumCallbackFunction, IntPtr lParam);

    [DllImport("user32")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr i);


    public static List<IntPtr> GetChildWindows(IntPtr parent)
    {
        List<IntPtr> result = new List<IntPtr>();
        GCHandle listHandle = GCHandle.Alloc(result);
        try
        {
            EnumWindowProc childProc = new EnumWindowProc(EnumWindow);
            EnumChildWindows(parent, childProc, GCHandle.ToIntPtr(listHandle));
        }
        finally
        {
            if (listHandle.IsAllocated)
                listHandle.Free();
        }
        return result;
    }
    public delegate bool EnumWindowProc(IntPtr hWnd, IntPtr parameter);

    public static LinkedList<IntPtr> EnumMethod2(bool includeChild)
    {
        LinkedList<IntPtr> pss = new LinkedList<IntPtr>();
        Process[] ps = Process.GetProcesses();
        foreach (Process p in ps)
        {
            string s = p.MainWindowTitle;
            if (s.Equals("") || s.Equals("Mod_Taskbar"))
            {
                continue;
            }
            LinkedListNode<IntPtr> node = new LinkedListNode<IntPtr>(p.MainWindowHandle);
            pss.AddLast(node);
            if (includeChild){
                List<IntPtr> l = GetChildWindows(p.MainWindowHandle);
                foreach (IntPtr i in l)
                {
                    StringBuilder stb = new StringBuilder(255);
                    WindowEnumerator.GetWindowText(i, stb, 255);
                    if (stb.ToString().StartsWith("Address:") || stb.ToString().EndsWith("View") || stb.ToString().EndsWith("Control") ||
                        stb.ToString().EndsWith("Bar") || stb.ToString().EndsWith("bar") || stb.ToString().Contains("Host"))
                    {
                        continue;
                    }
                    if (Vars.excludes.Contains(stb.ToString()))
                    {
                        continue;
                    }
                    pss.AddAfter(node, i);
                }
            }
        }
        return pss;
    }
    private static bool EnumWindow(IntPtr handle, IntPtr pointer)
    {
        GCHandle gch = GCHandle.FromIntPtr(pointer);
        List<IntPtr> list = gch.Target as List<IntPtr>;
        list.Add(handle);
        return true;
    }
}

您是否检查了wp.showCmd的值

请尝试使用以下代码:

foreach (Process proc in Process.GetProcesses()) 
{ 
    if (proc.MainWindowTitle.Contains(p)) 
    { 
        IntPtr handle = proc.Handle; 
        Program.WINDOWPLACEMENT wp = new Program.WINDOWPLACEMENT(); 
        Program.GetWindowPlacement(handle, ref wp); 
        if ((wp.showCmd & 2) == 2) 
        { 
            wp.showCmd = 9; 
            MessageBox.Show("Restoring"); 
        } 
        else 
        { 
            wp.showCmd = 2; 
            MessageBox.Show("Minimizing"); 
        } 
        wp.length = Marshal.SizeOf(wp); 
        Program.SetWindowPlacement(handle, ref wp); 
        break; 
    } 
}  

您是否检查了wp.showCmd的值

请尝试使用以下代码:

foreach (Process proc in Process.GetProcesses()) 
{ 
    if (proc.MainWindowTitle.Contains(p)) 
    { 
        IntPtr handle = proc.Handle; 
        Program.WINDOWPLACEMENT wp = new Program.WINDOWPLACEMENT(); 
        Program.GetWindowPlacement(handle, ref wp); 
        if ((wp.showCmd & 2) == 2) 
        { 
            wp.showCmd = 9; 
            MessageBox.Show("Restoring"); 
        } 
        else 
        { 
            wp.showCmd = 2; 
            MessageBox.Show("Minimizing"); 
        } 
        wp.length = Marshal.SizeOf(wp); 
        Program.SetWindowPlacement(handle, ref wp); 
        break; 
    } 
}  

谢谢,现在我知道它捕获了一个名为HelperMsgListenerWnd的iTunes窗口,而不是任务栏-Microsoft Visual Studio。有什么帮助吗?现在到底是什么问题?您单击Visual Studio窗口,但它会更改iTunes窗口?我单击按钮最小化Visual Studio,会看到消息框“最小化”,而没有其他内容。我再次点击,得到“恢复”,我要么得到一个透明的窗口,标题是随机字母,要么是一个没有边框的空白方块@aKzenTTry使用ShowWindowAsync函数,将句柄作为第一个参数传递,将2或9作为第二个参数传递。谢谢,这对我很有用。请注意,您可能需要以管理员身份运行您的流程才能使其正常工作。这可以通过添加app.manifest文件并使用“谢谢”来实现,现在我知道它捕获了一个名为HelperMsgListenerWnd的iTunes窗口,而不是任务栏-Microsoft Visual Studio。有什么帮助吗?现在到底是什么问题?您单击Visual Studio窗口,但它会更改iTunes窗口?我单击按钮最小化Visual Studio,会看到消息框“最小化”,而没有其他内容。我再次点击,得到“恢复”,我要么得到一个透明的窗口,标题是随机字母,要么是一个没有边框的空白方块@aKzenTTry使用ShowWindowAsync函数,将句柄作为第一个参数传递,将2或9作为第二个参数传递。谢谢,这对我很有用。请注意,您可能需要以管理员身份运行您的流程才能使其正常工作。这可以通过添加app.manifest文件并使用
this.WindowState = FormWindowState.Minimized;