2

I am opening Word from within a Console Application. How can I ensure the Console remains the active window, even after I have shown the Word Application?

using System;
using Microsoft.Office.Interop.Word;

namespace WordDocumentObject {
    class Program {
        static void Main(string[] args) {
            _Application app = new Application();
            app.Visible = true;

            //Activate the console window here

            Console.WriteLine("Press any key to continue...");
            Console.ReadKey(true);
        }
    }
}
Suman Banerjee
  • 1,923
  • 4
  • 24
  • 40
Zev Spitz
  • 13,950
  • 6
  • 64
  • 136

1 Answers1

2

you will have to use a call to the SetForegroundWindow API to get what you ask:

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

full example is already in SO:

bring a console window to front in c#

Community
  • 1
  • 1
Davide Piras
  • 43,984
  • 10
  • 98
  • 147