仅在屏幕上获取范围的范围坐标 [英] Getting Range coordinates only for Ranges on the screen
问题描述
我目前正在使用以下方法在文档中查找Range
的坐标:
I am currently using the following method to find the coordinates of a Range
within a document:
private Rectangle GetRangeCoordinates(Window w, Range r)
{
int left = 0;
int top = 0;
int width = 0;
int height = 0;
w.GetPoint(out left, out top, out width, out height, r);
return new Rectangle(left, top, width, height);
}
除非Range
出现在屏幕上相当大的距离(只有几页),否则它会很好地工作,在这种情况下,我会遇到以下异常:
This works really well unless the Range
is off the screen by a fairly large margin (quite a few pages), in which case I get the following exception:
System.Runtime.InteropServices.COMException(0x800A1066):命令 Microsoft.Office.Interop.Word.Window.GetPoint(Int32& ScreenPixelsLeft,Int32& ScreenPixelsTop,Int32& ScreenPixelsWidth, Int32& ScreenPixelsHeight,对象obj)在 [ProjectName].[TaskpaneName] .GetRangeCoordinates(Window w,Range r) 在 [... somePath ...] [TaskpaneName] .cs:line 66
System.Runtime.InteropServices.COMException (0x800A1066): Command failed at Microsoft.Office.Interop.Word.Window.GetPoint(Int32& ScreenPixelsLeft, Int32& ScreenPixelsTop, Int32& ScreenPixelsWidth, Int32& ScreenPixelsHeight, Object obj) at [ProjectName].[TaskpaneName].GetRangeCoordinates(Window w, Range r) in [...somePath...][TaskpaneName].cs:line 66
有没有一种方法可以确定Range
是否在屏幕上,所以我只能在出现该方法时调用它?
Is there a way to figure out if a Range
is on screen or not, so that I can only call this method when it is?
推荐答案
这就是我的方法.
我为Application
和Range
创建了一些扩展方法:
I created a few extension methods for Application
and Range
:
public static class ApplicationExensions
{
// more (rather than less)
// does not do headers and footers
public static Range GetCurrentlyVisibleRange(this Application application)
{
try
{
Window activeWindow = application.ActiveWindow;
var left = application.PointsToPixels(activeWindow.Left);
var top = application.PointsToPixels(activeWindow.Top);
var width = application.PointsToPixels(activeWindow.Width);
var height = application.PointsToPixels(activeWindow.Height);
var usableWidth = application.PointsToPixels(activeWindow.UsableWidth);
var usableHeight = application.PointsToPixels(activeWindow.UsableHeight);
var startRangeX = left;// + (width - usableWidth);
var startRangeY = top;// + (height - usableHeight);
var endRangeX = startRangeX + width;//usableWidth;
var endRangeY = startRangeY + height;//usableHeight;
Range start = (Range) activeWindow.RangeFromPoint((int) startRangeX, (int) startRangeY);
Range end = (Range) activeWindow.RangeFromPoint((int) endRangeX, (int) endRangeY);
Range r = application.ActiveDocument.Range(start.Start, end.Start);
return r;
}
catch (COMException)
{
return null;
}
}
}
public static class RangeExtensions
{
public static bool Intersects(this Range a, Range b)
{
return a.Start <= b.End && b.Start <= a.End;
}
public static Rectangle? GetCoordinates(this Range range)
{
try
{
Application application = range.Application;
Window window = application.ActiveWindow;
int left = 0;
int top = 0;
int width = 0;
int height = 0;
window.GetPoint(out left, out top, out width, out height, range);
return new Rectangle(left, top, width, height);
}
catch (COMException e)
{
return null;
}
}
}
然后我像这样使用它们:
And then I used them like this:
Range currentlyVisibleRange = application.GetCurrentlyVisibleRange();
if (currentlyVisibleRange.Intersects(rng)){
var coords = rng.GetCoordinates();
}
这篇关于仅在屏幕上获取范围的范围坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!