简体   繁体   English

C#取消选择COMobject WordDocument

[英]C# Deselect COMobject WordDocument

In Visual Studio I've created a Word 2016 Document project. 在Visual Studio中,我创建了Word 2016 Document项目。 To this document I've added a custom ActionsPane control. 在此文档中,我添加了一个自定义ActionsPane控件。 The only thing this control does is adding a PlainTextContentControl to the active document. 该控件唯一要做的就是向活动文档中添加一个PlainTextContentControl。

if (Globals.ThisDocument.Content.Application.ActiveDocument == null) return;
        var tagControl = Globals.ThisDocument.Controls.AddPlainTextContentControl(Guid.NewGuid().ToString());
tagControl.PlaceholderText = @"PLACEHOLDER";
tagControl.LockContents = true;

This all works fine, the plaintextcontrol is added and selected in the Word document. 一切正常,在Word文档中添加并选择了plaintextcontrol。 But what I want is that the control is added and that the cursor will jump to the end of the control so a user can directly start typing. 但是我想要的是添加了控件,并且光标将跳到控件的末尾,因此用户可以直接开始键入。 The newly added control is automatically selected. 新添加的控件将自动选择。 How can I turn this of? 我该如何解决?

I have already tried: 我已经尝试过:

var range = Globals.ThisDocument.Content;
range.Application.Selection.Collapse();

Can anyone help me out here? 有人可以帮我从这里出去吗? Thanks. 谢谢。

Edit: Als tried this solution. 编辑:Als尝试此解决方案。

private static IntPtr documentHandle;
        public delegate bool EnumChildProc(IntPtr hwnd, int lParam);

        [DllImport("user32.dll")]
        public static extern System.IntPtr SetFocus(System.IntPtr hWnd);
        [DllImport("user32.dll")]
        public static extern int EnumChildWindows(IntPtr hWndParent, EnumChildProc callback, int lParam);
        [DllImport("user32.dll")]
        public static extern int GetWindowText(IntPtr hWnd, StringBuilder title, int count);

        private static bool EnumChildWindow_Handle(IntPtr handle, int lparam)
        {
            StringBuilder s = new StringBuilder(50);
            GetWindowText(handle, s, 50);
            Debug.WriteLine(s.ToString());
            if (s.ToString() == "Microsoft Word-document")
            {
                documentHandle = handle;
            }
            return true;
        }

 private void MoveCursorToEndOfLastAddedTag(PlainTextContentControl ctrl)
        {
            EnumChildProc EnumChildWindow = new EnumChildProc(EnumChildWindow_Handle);
            EnumChildWindows(Process.GetCurrentProcess().MainWindowHandle, EnumChildWindow, 0);
            SetFocus(documentHandle);
}

This also doesn't work. 这也不起作用。

Finally I have success. 终于我成功了。 It's not the most clean way, but it works like a charm. 这不是最干净的方法,但它就像一种魅力。 The only code I made is this: 我编写的唯一代码是这样的:

private void MoveCursorToEndOfLastAddedTag(PlainTextContentControl ctrl)
{
    System.Windows.Forms.SendKeys.Send("{F10}");
    System.Windows.Forms.SendKeys.Send("{F10}");
    System.Windows.Forms.SendKeys.Send("{RIGHT}");
}

It sends 2 times the F10 key. 它发送2次F10键。 First time the focus is set to the ribbon, the second time the focus is given back to the document. 第一次将焦点设置为功能区,第二次将焦点重新分配给文档。 With the right key I remove the selection from the PlainTextContentControl. 使用右键,我从PlainTextContentControl中删除选择。

Thank you all for your help. 谢谢大家的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM