SetForegroundWindow-Sendkeys问题 [英] SetForegroundWindow - Sendkeys problem

查看:106
本文介绍了SetForegroundWindow-Sendkeys问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有此应用程序,需要从其他应用程序中获取选定的文本.我通过向应用程序发送CTRL + C并使用剪贴板来做到这一点:

So I have this application where I need to get the selected text from an other application. I do this by sending CTRL+C to the app and using the clipboard:

private void button1_Click(object sender, EventArgs e)
{
    GetText();
}

private void GetText()
{
    try
    {
        Clipboard.Clear();
        SetForegroundWindow(Process.GetProcessesByName("app")[0].MainWindowHandle);
        SendKeys.SendWait("^c");
    }
    catch (Exception ex)
    {
        Trace.WriteLine(ex);
    }
}



一切正常,我得到了想要的文字.
但是当我想在抓取文本的同时使用文本时,我似乎遇到了问题.

当我这样做时:



All works fine, I get the text that I want.
BUT when I want to use the text at the same time that I''m grabbing it I seem to get a problem.

When I do:

 private void button1_Click(object sender, EventArgs e)
 {
     GetText();
     DoSomething();
 }

private void DoSomething(){
     string str = Clipboard.GetText();
     ...
     ...
}



似乎在执行button1_click的过程中,字符串为空.虽然最后,当一切都完成后,我的剪贴板中有选定的文本.

因此,只有在button1_click完成后,剪贴板才会获取我选择的文本.

我现在的解决方案是使用2个按钮:一个用于将文本加载到剪贴板中,另一个用于对其进行处理.
这不是理想的,所以我希望有一个解决方案来解决我的问题,所以我只需要使用一个按钮即可.



It seems that during the process of the button1_click the string is empty. Though in the end, when everything is done, my clipboard has the selected text.

So the clipboard only gets my selected text when button1_click is completed.

My solution now is to use 2 buttons: one to load the text in the clipboard, and one to do something with it.
This is not ideal, so I''m hoping there''s a solution for my problem so I only have to use one button.

推荐答案

这很简单-问题是您不了解事情的运作方式!

当您将CTRL-C发送到另一个进程时,并不意味着它立即处理它们并返回.而是将密钥排队到该过程的消息列表中,并在应用程序良好且准备就绪时进行处理-这可能是在您的Click事件处理程序完成时进行的,但是如果另一个应用程序特别忙碌并且可能又过了几分钟也可能会在几分钟后处理. /或写得不好.

除此之外,通过剪贴板在应用程序之间传输数据是做事的一种非常糟糕的方法:如果没有别的事情,它会破坏用户在剪贴板上所不知道的东西.如果我正在处理一个复杂的图像,而我刚刚花了半个小时来构建一个图层并将其放置在剪贴板上以粘贴到另一个图像中,那么您的应用程序就会出现并进行装箱,那么我就不会有点逗乐.您的应用程序将很快被卸载,以至EXE文件会被擦伤!

寻找更好的方法.这是一个糟糕的解决方案,会产生无法预测的结果.
This is pretty simple - the problem is that you don''t understand how things work!

When you send CTRL-C to a different process, it doesn''t mean that it processes them immediately and returns. Instead the keys get queued into the message list for the process and will be handled when the application is good and ready - this may be when your Click event handler finishes, but it could also be several minutes later if the other application is particularly busy and / or badly written.

Besides that, transferring data between applications via the Clipboard is a very bad way to do things: if nothing else it destroys anything the user has put on the clipboard without them knowing. If I am working on a complex image, and I have just spent half an hour building a layer and putting it on the clipboard to paste into a different image, then your application comes along and bins it, then I am not going to be even slightly amused. Your app will be uninstalled so quickly that the EXE file will get friction burns!

Find a better way. This is a bad solution that produces unpredictable results.


这篇关于SetForegroundWindow-Sendkeys问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆