这里提供一个有时可行,有时不灵的办法,仅供参考学习。发现有改进的地方,可以联系博主一起讨论改进,上代码。
先整一个帮助的类里面包含键盘的消息
public static class Helper
{
[DllImport("user32.dll")]
private static extern IntPtr SetActiveWindow(IntPtr hWnd);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern void keybd_event(Keys bVk, byte bScan, uint dwFlags, uint dwExtraInfo);
/// <summary>
/// 发送键盘消息
/// </summary>
/// <param name="proc"></param>
/// <param name="key"></param>
public static void SendKeys(IntPtr proc, Keys key)
{
SetActiveWindow(proc);
SetForegroundWindow(proc);
keyevent(key, 0, 0, 0);
keyevent(key, 0, 2, 0);
keyevent(key, 0, 0, 0);
keyevent(key, 0, 2, 0);
}
然后就是在主体程序中使用
public class SendEsc : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIDocument uidoc = commandData.Application.ActiveUIDocument;
Document doc = uidoc.Document;
FamilySymbol fs = doc.GetElement(new ElementId(91406)) as FamilySymbol;
commandData.Application.Application.DocumentChanged += AddElement;
try
{
uidoc.PromptForFamilyInstancePlacement(fs);
}
catch
{
}
commandData.Application.Application.DocumentChanged -= AddElement;
return Result.Succeeded;
}
private void AddElement(object sender, Autodesk.Revit.DB.Events.DocumentChangedEventArgs e)
{
Helper.SendKeys(Autodesk.Windows.ComponentManager.ApplicationWindow, System.Windows.Forms.Keys.Escape);
}
}
这里举得例子是用了上次的放置实例的例子,里面的documentchange事件是监控如果增加一个实例的时候,就自动传回两次Esc键,结束命令。今天的分享就到这里。
bug以修复
转载至:https://blog.csdn.net/Oneal5354/article/details/109406494