最大化或恢复窗口不起作用

时间:2014-06-02 09:03:49

标签: c# winforms winapi

我有一个Windows窗体,用于检查进程是否最小化。如果它是它应该最大化它并将窗口带到前面。如果窗口未最大化并且在后台,则代码正常工作,窗口显示在前面。但是,当我检查isiconic,即最小化时,它不会恢复或最大化窗口。我的代码是

[DllImport("user32.dll", CharSet = CharSet.Auto)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern
            bool SetForegroundWindow(IntPtr hWnd);
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern
            bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
        [DllImport("user32.dll")]
        private static extern
            bool IsIconic(IntPtr hWnd);
        [DllImport("user32.dll")]
        private static extern int ShowWindow(IntPtr hWnd, int nCmdShow);
        [DllImport("user32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool PrintWindow(IntPtr hWnd, IntPtr hDC, uint nFlags);

        private const int SW_HIDE = 0;
        private const int SW_SHOWNORMAL = 1;
        private const int SW_SHOWMINIMIZED = 2;
        private const int SW_SHOWMAXIMIZED = 3;
        private const int SW_SHOWNOACTIVATE = 4;
        private const int SW_RESTORE = 9;
        private const int SW_SHOWDEFAULT = 10;
        public Form1()
        {
            InitializeComponent();
            this.Hide();
        }

        private void Form1_Load(object sender, EventArgs e)
        {            
            this.Visible = false;

            Process[] processes = Process.GetProcesses();            
            foreach (var process in Process.GetProcessesByName("Connectivity.VaultPro"))
            {
                IntPtr hWnd = process.MainWindowHandle;
                // if iconic, we need to restore the window
                if (IsIconic(hWnd))
                {
                    //ShowWindowAsync(hWnd, SW_RESTORE);
                    ShowWindow(hWnd, SW_RESTORE);
                    //SetForegroundWindow(hWnd);
                }
                else
                {
                    ShowWindowAsync(hWnd, SW_RESTORE);                    
                    SetForegroundWindow(hWnd);
                }
                // bring it to the foreground
                SetForegroundWindow(hWnd);

            }



            if (Directory.Exists(@"C:\Temp"))
            {
                if (File.Exists(@"C:\Temp\temp.png"))
                {
                    File.Delete(@"C:\Temp\temp.png");
                }
            }
            else
            {
                Directory.CreateDirectory(@"C:\Temp");
            }
}

在if条件下,我已经使用ShowWindow尝试了它,并且还尝试使用SW_SHOWMAXIMIZED和SW_RESTORE。有人能告诉我代码中可能存在的问题吗? 提前致谢

0 个答案:

没有答案