在Visual中,我创建了一个Word 2016文档项目。在本文档中,我添加了一个自定义ActionsPane控件。该控件所做的唯一事情就是向活动文档中添加一个PlainTextContentControl。
if (Globals.ThisDocument.Content.Application.ActiveDocument == null) return;
var tagControl = Globals.ThisDocument.Controls.AddPlainTextContentControl(Guid.NewGuid().ToString());
tagControl.PlaceholderText = @"PLACEHOLDER";
tagControl.LockContents = true;这一切都很好,在Word文档中添加和选择了明文控件。但是我想要的是添加控件,光标跳到控件的末尾,这样用户就可以直接开始键入。自动选择新添加的控件。我怎么才能改变这一切呢?
我已经试过了:
var range = Globals.ThisDocument.Content;
range.Application.Selection.Collapse();有人能帮我吗?谢谢。
编辑: 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);
}这也没用。
发布于 2016-02-16 21:28:43
我终于成功了。这不是最干净的方式,但它的作用就像一种魅力。我做的唯一代码是:
private void MoveCursorToEndOfLastAddedTag(PlainTextContentControl ctrl)
{
System.Windows.Forms.SendKeys.Send("{F10}");
System.Windows.Forms.SendKeys.Send("{F10}");
System.Windows.Forms.SendKeys.Send("{RIGHT}");
}它发送2倍的F10密钥。第一次将焦点设置为带状,第二次将焦点返回到文档。使用正确的键,我将从PlainTextContentControl中删除所选内容。
谢谢你们的帮助。
https://stackoverflow.com/questions/35419756
复制相似问题