专注于控制台应用程序

时间:2019-03-06 14:52:07

标签: c# excel focus console-application setforegroundwindow

我正在创建一个程序,可以通过控制台应用程序控制excel,但是我需要不断关注控制台。我已经尝试了一些方法,例如SetForegroundWindow(),但是没有用。我怎样才能做到这一点?我需要将它放在while循环中,这样,每次我插入一个字符(完成代码后就不必每次都按Enter键)时,它才保留在控制台应用程序中,并且不会改变焦点。

foreach (Process p in prcss)
        {
            p.WaitForInputIdle();
            IntPtr h = p.MainWindowHandle;
            SetForegroundWindow(h);

            if (p.ProcessName == prog || p.ProcessName == progcaps)
            {
                Console.WriteLine("\n\nProcess: {0} ID: {1}\n", p.ProcessName, p.Id); //Imprime apenas o que queremos        

            }

            string pname = p.ProcessName;

            //Enter
            SendKeys.SendWait("~");
            SendKeys.SendWait("{F2}");

            while (true)
            {
                //Code goes here
            }
       }

0 个答案:

没有答案