简体   繁体   English

卡在前景窗口中

[英]Stuck with Foreground window

this is for windows desktop, not web... in C# using Visual Studio 2013. in windows 7. 在Windows 7中使用Visual Studio 2013在C#中用于Windows桌面,而不用于Web。

hello, I'm recreating a "Windows Speech Recognizer" from scratch... si working really well... what i need to do si for it to check what windows process are open (ie Notepad, Firefox, etc) then IF Notepad is open, do something... without having to CALL that action myself... like automatic... Open Notepad... an alert says "Hello Notepad"... I Close Notepad" an action says "Close Notepad"... I try 12948305380453 diferent things but the only thing i've acomplished is it writting a numer... and when i close and reopen notepad... the number is diferent... I need THE PROCESS NAME... like "notepad.exe" "firefox.exe" from the windows task manager... 你好,我正在从头开始创建一个“ Windows Speech Recognizer” ... si工作得很好...我需要做些什么来检查哪些Windows进程已打开(例如,记事本,Firefox等),然后是IF记事本是打开的,执行某项操作...无需自己调用该操作...就像自动...打开记事本...警报显示“您好记事本” ...我关闭记事本”,操作显示“关闭记事本”。 ..我尝试使用12948305380453的其他功能,但是我完成的唯一操作是写一个数字...当我关闭并重新打开记事本时...这个数字是不同的...我需要过程名称...如“ Windows任务管理器中的“ notepad.exe”“ firefox.exe” ...

sorry i dont have any code to post, because i dont actially have one with this part... thank you... 抱歉,我没有任何代码可发布,因为我实际上没有与此部分相关的代码...谢谢...

The program (notepad) will be open manualy so the code needs to constantly keep track of the task manager process. 该程序(记事本)将手动打开,因此代码需要不断跟踪任务管理器过程。 and give me the result in a variable inside the code (For example, string "PNAME", so i can use that variable in the rest of my code. 并在代码内的变量中提供结果(例如,字符串“ PNAME”,这样我就可以在其余代码中使用该变量。

I only need it to tell me if Notepad.exe is open on not (ie True Or False) to usit like 我只需要告诉我Notepad.exe是否以非打开(即True或False)来使用,就像

If notepad.exe is open do this If notepad.exe is closed do nothing 如果notepad.exe打开,则执行此操作如果notepad.exe关闭,则不执行任何操作

ok... the code ir Fairly large... so I will try to post the most relevant ones... 好的...代码ir相当大...所以我将尝试发布最相关的代码...

"usign" I'm using are... 我正在使用的“ usign”是...

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Speech;
using System.Speech.Synthesis;
using System.Speech.Recognition;
using System.Text;
using System.Windows.Forms;
using System.Threading;

I've inizialized a speech recognition enguine that does NOT use the windows UI or it's commands (Ie if I say Open Firefox" it does nothing because it is not in his grammar) 我已经将不使用Windows UI或命令的语音识别向导进行了初始化(即,如果我说“打开Firefox”,它什么也不做,因为它不在他的语法中)

then I'v loaded my own grammar 然后我加载了自己的语法

private Grammar CreateCustomGrammar()
{
    GrammarBuilder GeneralCommands = new GrammarBuilder();
    GeneralCommands.Append(new Choices("cut", "copy", "paste", "Read Clipboard", "help", "Close Recognition", "Turn Dictation On", "Read That"));
    return new Grammar(GeneralCommands);
   recognizer.LoadGrammar(GeneralCommands);
}

and added a switch to every word in the grammar so it knows what to do 并为语法中的每个单词添加了一个开关,以便它知道该怎么做

private void SpeechToAction(string text)
{
    DetermineText(text);

        switch (text)
        {
            case "cut":
                //cut();
                speed();
                break;
            case "copy":
                copy();
                break;
            case "paste":
                paste();
                break;
            case "Read Clipboard":
                readClipboard();
               break;
            case "Read That":
               readThat();
               break;
            case "Turn Dictation Off":
               TurnDictationOff();
               break;
            case "help":
               help();
               break;
            case "Close Recognition":
               Exit();
               break;
        }
}

all of that works in the background if i want to use another application "ie Notepad" 如果我想使用另一个应用程序“即记事本”,所有这些都将在后台运行

but if there are commands that i want to be ACTIVE JUST when "notepad" IS THE FOREGROUND application... there is where im stuck 但是如果当“记事本”是FOREGROUND应用程序时,如果有一些命令我想保持活动状态,那我就会陷入困境

I know the Theory... 我知道理论...

it will something like this 它会是这样的

(before all that code i've add) (在我添加的所有代码之前)

//here is the tricky part
      IF //notepad is the foreground and var notepadOK == 1
       {   
        private Grammar NEWNAME()
        {
            GrammarBuilder NEWNAME = new GrammarBuilder();
            GeneralCommands.Append(new Choices("Hole notepad", "save"));
            return new Grammar(NEWNAME);
            var notepadOK = 0
        }
       }
      IF //notepad is NOT in the foreground and notepadOK == 0
        {
           // unload Notepad Grammar (i remember how to do this part, just cant find //it in the code
        }

In order to look for- and deal with, processes in .net you should use the Process class using that you can list running processes and get information about each one 为了查找和处理.net中的进程,您应该使用Process类,该类可以列出正在运行的进程并获取有关每个进程的信息。

The sample for the Process.GetProcesses() method seems to nearly what you want, except you would do it continuously Process.GetProcesses()方法的示例似乎几乎是您想要的,但您会不断地这样做

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM