1

I want a Word application to come to front, see code below:

string caption = wordApp.Caption;
IntPtr handler = FindWindow(null, caption);
SetForegroundWindow(handler);
wordApp.Visible = true;

The errors I got are:

Error CS0103: The name 'FindWindow' does not exist in the current context  
Error CS0103: The name 'SetForegroundWindow' does not exist in the current context

I guess I miss a reference even though the compiler does not say so. Am I right?

Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44
user3041065
  • 185
  • 2
  • 9

3 Answers3

2

Since you are using C#, use Process.GetProcessesByName(), something like this:

Process[] processes = Process.GetProcessesByName("WINWORD");
SetForegroundWindow(processes[0].Handle);

In order to use SetForegroundWindow(), you have to have this:

[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);
Malganis
  • 338
  • 3
  • 5
1

This might be of good help to your problem: FindWindow MSDN link

The library that contains it is under User32.lib

vishnu
  • 730
  • 2
  • 6
  • 26
1

You need to declare your functions before using them, I'm assuming you want to call the Windows APIs: FindWindow and SetForeGroundWindow.

[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);

You can find more examples in MSDN documentation.

Rodrigo Silva
  • 432
  • 1
  • 6
  • 18