我现在正在处理同样的问题。我使用 System.Drawing 在 Revit 窗口上绘图。
UIApplication m_pUIApp;
System.Drawing.Point m_pt1, m_pt2 = System.Drawing.Point.Empty
void DrawTask(System.Threading.CancellationToken ct)
{
while (!ct.IsCancellationRequested)
{
m_pt2 = Cursor.Position;
if (m_pt2.X < m_pUIApp.DrawingAreaExtents.Left + 2 ||
m_pt2.X > m_pUIApp.DrawingAreaExtents.Right - 20 ||
m_pt2.Y > m_pUIApp.DrawingAreaExtents.Bottom - 20 ||
m_pt2.Y < m_pUIApp.DrawingAreaExtents.Top + 2)
{
System.Threading.Thread.Sleep(20);
continue;
}
if (m_pt1 != System.Drawing.Point.Empty)
using (Graphics g = Graphics.FromHwnd(IntPtr.Zero))
{
g.DrawLine(Pens.Black, m_pt1, m_pt2);
}
System.Threading.Thread.Sleep(20);
}
}
然后我打电话
m_pt1 = Cursor.Position;
var cts = new System.Threading.CancellationTokenSource();
Task.Run(() => DrawTask(cts.Token))
当我开始点选择以及cts.Cancel();
完成或捕获异常时。它有一些缺点:如果您平移、缩放或失去 Revit 焦点,它会变得很奇怪。