【问题标题】:Focus matching word in opened notepad with Process method in C#使用 C# 中的 Process 方法在打开的记事本中聚焦匹配单词
【发布时间】:2021-04-15 16:32:47
【问题描述】:

我正在尝试将 CTRL-F 机制添加到我在 Visual Studio 中的 C# 应用程序中。

我的应用程序使用 Process.start 方法打开 Windows 记事本并查找给定的单词。我想在记事本中找到这个词并集中它。

我有办法搜索这个词,但是如果用户单击 CTRL-F,如何将注意力集中到正确的行上?

有人知道怎么做吗?或者也许有一个可以提供帮助的框架?

我的应用程序是关于通过在文本框中键入任何字符串序列并获取包含它的文件来搜索任何文件,之后我有这个文件的打开功能,我想要获得焦点单词,我在打开的记事本中输入(因为我得到了包含这个字符串的 txt 文件)

例如,我在文本框中输入“hello”并获取 file.txt,打开后在其中输入“hello”字样

【问题讨论】:

  • 不清楚您的应用程序如何与记事本交互。是否有一个记事本 API 允许您编写该应用程序的脚本?听起来您可能正在尝试使用 Windows 10 UI 打开和驱动记事本的脚本,这是真的吗?或者您是否正在尝试构建行为类似于记事本的东西。你需要更清楚。祝你好运,欢迎!
  • 我的应用程序是关于通过在文本框中键入任何字符串序列并获取包含它的文件来搜索任何文件,之后我有这个文件的打开函数并且我想要获得焦点单词,它我在打开的记事本中输入(因为我得到了包含此字符串的 txt 文件)
  • 例如,我在文本框中输入“hello”并获取 file.txt,打开后在其中输入“hello”字样

标签: c# process filesystems notepad


【解决方案1】:

执行此操作的代码:

private async void button2_Click(object sender, EventArgs e)
{
    Process.Start("yourProcess");
    this.WindowState = FormWindowState.Minimized;
    await Task.Delay(1000);
    SendKeys.Send("^(f)");
    foreach (char c in textBox1.Text)
    {
        SendKeys.Send("{" + c + "}");
    }
    SendKeys.Send("{ENTER}");
    //if you want to automatycally remove search window use:
    SendKeys.Send("{ESC}");
}

【讨论】:

  • 谢谢,但是你知道ctrl+f点击后怎么做“find”方法吗?
  • 或者你可以使用 PreviewKeyDown 事件
  • 所以在尝试 set "SendKeys.Send("^{F}") in if(keybind) 现在我必须在这个方法块内使用 focus() 方法做一些事情?(我正在考虑如何处理并获得最终的重点匹配词)
  • 我不明白你要做什么。
  • 您到底需要什么? @SilverRaven655
【解决方案2】:

这是我现在(但不工作)里面的代码:“private async void OpeningFile_Click(object sender, RoutedEventArgs e)”

        try
        {
            var startInfo = new ProcessStartInfo();

            startInfo.FileName = PathResult.ToString();
            startInfo.Arguments = "\"" + PathResult.ToString() + "\"";
            startInfo.UseShellExecute = true;

            Process.Start(startInfo);
       
            MessageBox.Show(ControlHelper.WordToFind);
           
            await Task.Delay(1000);

           
            System.Windows.Forms.SendKeys.SendWait("^(f)");

            foreach(char x in ControlHelper.WordToFind)
            {
                System.Windows.Forms.SendKeys.SendWait("{" + x + "}");
            }

            System.Windows.Forms.SendKeys.SendWait("{ENTER}");
            
            System.Windows.Forms.SendKeys.SendWait("{ESC}");          
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message + "\n" + PathResult.ToString());
        }
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-14
    • 2022-12-17
    • 1970-01-01
    • 1970-01-01
    • 2012-06-29
    • 1970-01-01
    相关资源
    最近更新 更多