1

Possible Duplicate:
How can I bring my application window to the front?

I am having an issue with SwitchToThisWindow

using System;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace BringToFront
{
    public partial class Form1 : Form
    {
        [DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
        public static extern IntPtr FindWindow(String className, String windowName);

        [DllImport("user32.dll", SetLastError = true)]
        static extern void SwitchToThisWindow(IntPtr hWnd, bool turnOn);

        [DllImport("user32.dll")]
        public static extern IntPtr GetForegroundWindow();

        IntPtr activeWindowHandle = GetForegroundWindow();

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (!checkBox1.Checked)
                    bringToFront(comboBox1.SelectedItem.ToString());
                else
                    timer1.Enabled = true;
            }
            catch
            {
                MessageBox.Show("Please choose a Process Name");
            }
        }

        public static void bringToFront(string title)
        {
            IntPtr handle = FindWindow(null, title);
            if (handle == IntPtr.Zero)
            {
                return;
            }
            SwitchToThisWindow(handle, true);
        }

        private void comboBox1_Click(object sender, EventArgs e)
        {
            comboBox1.Items.Clear();
            Process[] process = Process.GetProcesses();
            foreach (Process processes in process)
            {
                if (!String.IsNullOrEmpty(processes.MainWindowTitle))
                    comboBox1.Items.Add(processes.MainWindowTitle.ToString());
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Process process = Process.GetCurrentProcess();
            string title = process.ProcessName.ToString();
            IntPtr handle = FindWindow(null, title);

            if (activeWindowHandle != handle)
                bringToFront(comboBox1.SelectedItem.ToString());
            if (!checkBox1.Checked)
                timer1.Enabled = false;
        }
    }
}

As you can see I'm trying to bring the process that is selected to the front and keep it in the front by doing a timer every 5 seconds and rebringing it to the front. It works perfectly when running the application through Microsoft Visual Studios, but when I run the program as a standalone, it works how every other function like this does and only makes it flash in taskbar instead of bringing it to the front.

Why are the permissions different and is there anyway to fix this?

4

1 回答 1

3

通过@ReedCopsy here的解决方案,我建议在切换到该窗口后将所选句柄设为 TopMost。使用此解决方案,没有新的应用程序可以成为所选窗口的顶部。

将以下内容添加到您的代码中:

 [DllImport("user32.dll")]
 static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);

 static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
 const UInt32 SWP_NOSIZE = 0x0001;
 const UInt32 SWP_NOMOVE = 0x0002;
 const UInt32 SWP_SHOWWINDOW = 0x0040;

并通过添加对以下内容的调用来更改您的 bringToFront SetWindowPos

   public static void bringToFront(string title)
    {
        IntPtr handle = FindWindow(null, title);
        if (handle == IntPtr.Zero)
        {
            return;
        }
        SwitchToThisWindow(handle, true);


        // Call this way:
        SetWindowPos(handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
    }
于 2012-10-20T06:42:07.550 回答