将Ctrl + C发送到Chrome浏览器,无法获取所选文本 [英] Send Ctrl+C to chrome browser,can't get selected text

查看:416
本文介绍了将Ctrl + C发送到Chrome浏览器,无法获取所选文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1.这是winfrom项目

2.我注册一个热键(使用windows api)

3.override WndProc()方法,代码如下。 />
结果:当我在txt中选择文本时,< Clipboard.ContainsText()>表达式为真,其他人也是如此,但当前程序是chrome浏览器时,它是假的。



 受保护 覆盖  void  WndProc( ref 消息m)
{
开关(m.Msg)
{
case 0x0312: // RegisterHotKey句柄
if (m.WParam.ToString()。Equals(< span class =code-string> 225))
{
// 清除数据
Clipboard.Clear();
SendKeys.SendWait( ^ c);
SendKeys.Flush();
if (Clipboard.ContainsText()) // 仅当Chrome浏览器时,表达式为false
{
MessageBox.Show( 获取文本);
}
}
break ;
}
base .WndProc( ref m);
}

解决方案

完全有可能你没有读到MSDN数据所说的内容: http://msdn.microsoft.com/en-us/library /system.windows.forms.sendkeys.sendwait(v=vs.110).aspx [ ^ ]



SendKeys类容易受到时序问题的影响,一些开发人员不得不解决这些问题。更新后的实现仍然容易受到时序问题的影响,但速度稍快,可能需要更改解决方法.SendKeys类尝试使用以前的首先实现,如果失败,则使用新的实现。因此,SendKeys类在不同的操作系统上可能会有不同的行为。此外,当SendKeys类使用新的im时补充一下,SendWait方法不会等待消息被发送到另一个进程时处理。



所以你完全有可能需要在执行此操作时考虑计时问题:在剪贴板检查之前使用Thread.Sleep(5000)进行测试会有帮助吗?


删除行

 Clipboard.Clear (); 


1.this is winfrom project
2.I register a hotkey(use windows api)
3.override WndProc() method, code is below.
result:when i select text in txt,<Clipboard.ContainsText()>expression is true,others also,but when current program is chrome browser,it's is false.

protected override void WndProc(ref Message m)
{
    switch (m.Msg)
    {
        case 0x0312:    //RegisterHotKey handle
            if (m.WParam.ToString().Equals("225"))
            {
                //clear data
                Clipboard.Clear();
                SendKeys.SendWait("^c");
                SendKeys.Flush();
                if (Clipboard.ContainsText())//Only when the Chrome browser, the expression is false
                {
                    MessageBox.Show("get text");
                }
            }
            break;
    }
    base.WndProc(ref m);
}

解决方案

It's entirely possible that you didn't read what the MSDN data said: http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.sendwait(v=vs.110).aspx[^]

"The SendKeys class is susceptible to timing issues, which some developers have had to work around. The updated implementation is still susceptible to timing issues, but is slightly faster and may require changes to the workarounds. The SendKeys class tries to use the previous implementation first, and if that fails, uses the new implementation. As a result, the SendKeys class may behave differently on different operating systems. Additionally, when the SendKeys class uses the new implementation, the SendWait method will not wait for messages to be processed when they are sent to another process."

So it is entirely possible that you need to consider timing issues when you do this: perhaps testing with a Thread.Sleep(5000) before the Clipboard check would help?


remove line

Clipboard.Clear();


这篇关于将Ctrl + C发送到Chrome浏览器,无法获取所选文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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