Word二次开发--用户编辑时自动显示提示窗口

VS2012提示功能看上去很神奇,那我们在Word中也做一个吧!

 

一、首先在ThisAddIn_Startup中添加DocumentContextOnUpdate事件

Globals.ThisAddIn.Application.ActiveDocument.CommandBars.OnUpdate += DocumentContextOnUpdate;

当用户输入文字内容后触发该事件

二、定义变量

public int CurserPositionX=0;
public int CurserPositionY=0;
public int rangewidth=0;
public int rangeheight=0;

三、事件代码

private void DocumentContextOnUpdate()
{

//得到当前编辑内容的位置(是光标位置,不是鼠标位置)
WordInterop.Range ra = Globals.ThisAddIn.Application.Selection.Range;
Globals.ThisAddIn.Application.ActiveDocument.ActiveWindow.GetPoint(out CurserPositionX,out CurserPositionY, out rangewidth,out rangeheight ,ra);

//创建无标题的非模态的窗口
TempWindowForTest windows= new TempWindowForTest();

//下面计算窗口出现的位置

int formLoadLocationX = 0;
int formLoadLocationY = 0;

formLoadLocationX=CurserPositionX+rangewidth;
formLoadLocationY=CurserPositionY+rangeheight;

//设置窗口显示的位置(一定要先设置窗口的StartPosition,否则Location不起作用)
windows.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
windows.Location = new Point(formLoadLocationX,formLoadLocationY);
//显示窗口
windows.Show();
}

四、word中的效果,如图:

  

看像VS2012中的提示窗口已经做了一半了!

posted @ 2022-03-12 00:04  多见多闻  阅读(368)  评论(0)    收藏  举报