激活控制台窗口

时间:2011-09-05 14:43:26

标签: c# windows

我在控制台应用程序中打开Word。即使在我显示Word应用程序之后,如何确保控制台仍然是活动窗口?

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);
        }
    }
}

1 个答案:

答案 0 :(得分:2)

您必须使用对SetForegroundWindow API的调用才能得到您的要求:

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

完整的例子已经在SO中了:

bring a console window to front in c#