Excel CustomTaskPane与WebBrowser控件 - 键盘/焦点问题 [英] Excel CustomTaskPane with WebBrowser control - keyboard/focus issues

查看:198
本文介绍了Excel CustomTaskPane与WebBrowser控件 - 键盘/焦点问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个确切的问题 https://social.msdn.microsoft.com/Forums/vstudio/en-US/e417e686-032c-4324-b778-fef66c7687cd/excel- customtaskpane-with-webbrowser-control-keyboardfocus-issues?forum = vsto

此处还提到 https://connect.microsoft.com/ VisualStudio / feedback / details / 521465 / the-focus-issue-between-excel-cells-and-excel-customtaskpane-with-webbrowser-control

我正在使用Visual Studio Professional 2013编写一个Excel 2010插件。我创建了一个简单的CustomTaskPane与System.Windows.Forms.WebBrowser子文件。该插件可以正常工作,我可以通过点击并更改复选框的状态来浏览webbrowser。

I am writing an Excel 2010 plugin using Visual Studio Professional 2013. I've created a simple CustomTaskPane with a System.Windows.Forms.WebBrowser child filing it. The plugin works just fine and I am able to navigate inside the webbrowser by click and change the state of checkboxes.

当我点击输入文本框时,我得到焦点,我看到光标闪烁,但是当我开始输入文本时,会将文本发送到Excel并写入单元格而不是浏览器中的文本框。

When I click on an input textbox I get focus and I see the cursor blinking, but when I start typing the text is sent to Excel and written into a cell instead of the textbox inside the browser.

当加载功能时,我添加了自定义任务窗格。 / p>

I add the custom taskpane when the ribbon loads.

private void Ribbon_Load(object sender, RibbonUIEventArgs e)
{
  TaskPaneView taskPaneView = new TaskPaneView();
  Microsoft.Office.Tools.CustomTaskPane myTaskPane = Globals.ThisAddIn.CustomTaskPanes.Add(taskPaneView, "Title");
  myTaskPane.Visible = true;
}

当我点击文本框,然后点击 F6 它正常工作。 customtaskpane标题稍微变暗,文本被捕获在文本框中。

When I click on the textbox then hit F6 it works correctly. The customtaskpane header darkens slightly and text is captured in the textbox.

我如何解决这个问题,这样当我点击输入文本框时,文本就会转到框中的Excel?

How can i fix this issue so that when I click on the input textbox the text goes into the box instead of Excel?

编辑:好的,我做了一些更多的测试。如果我添加事件在我的TaskPaneView跟踪鼠标输入并点击他们工作,但只有当我删除Web浏览器的孩子。意思是Web浏览器以某种方式阻止这些事件,并阻止TaskPaneView了解它有重点。如果我还在浏览器旁边的TaskPaneView中添加了一个文本框窗体控件,则文本框工作完全正常,TaskPaneView了解它具有焦点,然后浏览器中的输入文本框开始工作。如果我直接在Web浏览器上调用焦点方法,TaskPaneView可以理解它具有焦点,并且一切都很完美。所以很明显,问题并不是键盘,而是TaskPaneView的问题没有被告知,当浏览器被点击时,它就有焦点,所以按键到达错误的区域。如果我能找到一种方法来使TaskPaneView了解它的焦点,每一个都应该工作。

Ok, I did some more testing. If I add events on my TaskPaneView to track mouse enter and click they work, but only if I remove the web browser child. Meaning the web browser is somehow blocking these events and preventing the TaskPaneView from understanding it has focus. If I also added a textbox form control into the TaskPaneView along side the browser, the textbox works totally fine and the TaskPaneView understands it has focus and then the input text field inside the browser then starts works. If I call the focus method directly on the web browser, the TaskPaneView understands it has focus and everything works perfectly. So clearly the issue isn't really with the keyboard, but instead an issue of the TaskPaneView not being told it has focus when the browser is clicked on so the keystrokes go to the wrong area. If I can find a way to make TaskPaneView understand it has focus everythign should work.

推荐答案

确定我能够解决问题使用以下代码

Ok I was able to fix the issue using the following code

protected override void WndProc(ref Message m)
{
  if(m.Msg == 528 && !this.Focused)
  {
    this.Focus();
  }
  base.WndProc(ref m);
}

我将这个函数添加到我的TaskPaneView,这是一个只有一个UserControl的webbrowser子。我没有深入了解为什么或如何工作,但基本上我认为发生了什么是我正在拦截WndProc,这是一些处理发送到窗口的消息的低级功能。我用它来检查邮件是否是528,我认为是notifyParent。我不知道这是正是我应该听的哪个消息,但它似乎工作。

I added this function to my TaskPaneView which is simply a UserControl with that webbrowser child. I don't have a deep understanding of why or how this works, but basically I think what's happening is i'm intercepting WndProc which is some low-level function that process messages sent to the window. I use it to check to see if the message is 528, which I think means notifyParent. I don't know if this is exactly which message I should be listening for, but it seems to work.

一旦我有正确的消息消息,我检查TaskPaneView是否具有焦点,如果没有,我给它的焦点与 focus() 函数。我以前做过测试,显示如果我在TaskPaneView上手动调用了 focus ,一切都正常。所以如果我没有焦点,那么手动请求焦点,我们都很好。

Once I have the right message message I check to see if the TaskPaneView has focus and if not, I give it focus with the focus() function. I did testing earlier that showed if I manually invoked focus on the TaskPaneView everything worked fine. So if I don't have focus, then manually request focus and we are all good.

如果有人可以提供更详细的解释,为什么这样工作,所以我可以更好地理解它,但至少我解决了这个问题。感谢 Jeremy Thompson ,让我以新的方式思考这个问题。

I would appreciate it if someone can provide a more detailed explaination as to why this works so I can understand it better, but at least I solved the problem. Thanks Jeremy Thompson for getting me thinking about this issue in a new way.

这篇关于Excel CustomTaskPane与WebBrowser控件 - 键盘/焦点问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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