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?