Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
529 views
in Technique[技术] by (71.8m points)

c# - Excel CustomTaskPane with WebBrowser control - keyboard/focus issues

I am having this exact issue https://social.msdn.microsoft.com/Forums/vstudio/en-US/e417e686-032c-4324-b778-fef66c7687cd/excel-customtaskpane-with-webbrowser-control-keyboardfocus-issues?forum=vsto

Also mentioned here https://connect.microsoft.com/VisualStudio/feedback/details/521465/the-focus-issue-between-excel-cells-and-excel-customtaskpane-with-webbrowser-control

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.

enter image description here

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.

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;
}

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

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

EDIT: 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

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.

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...