1. public partial class Form1 : Form
    2. {
    3. //方法1
    4. ///调用user32函数
    5. [DllImport("user32.dll", EntryPoint = "SetFocus")]
    6. public static extern int SetFocus(IntPtr hWnd);
    7. public Form1()
    8. {
    9. InitializeComponent();
    10. }
    11. private void Button1_Click(object sender, EventArgs e)
    12. {
    13. Database db = HostApplicationServices.WorkingDatabase;
    14. var doc = acadApp.DocumentManager.MdiActiveDocument;
    15. using (var m_DocumentLock = doc.LockDocument())
    16. {
    17. ///设置cad窗口为焦点
    18. SetFocus(doc.Window.Handle);
    19. //方法2:调用CAD自带的Focus函数
    20. //Application.MainWindow.Focus();
    21. Editor ed = acadApp.DocumentManager.MdiActiveDocument.Editor;
    22. using (Transaction tr = db.TransactionManager.StartTransaction())
    23. {
    24. //using (EditorUserInteraction edUsrInt = ed.StartUserInteraction(this))
    25. //{
    26. // edUsrInt.End();
    27. // this.Focus();
    28. //}
    29. BlockTable bt = db.BlockTableId.GetObject(OpenMode.ForWrite) as BlockTable;
    30. BlockTableRecord modeSpace = db.CurrentSpaceId.GetObject(OpenMode.ForWrite) as BlockTableRecord;
    31. TextStyleTable tst = db.TextStyleTableId.GetObject(OpenMode.ForRead) as TextStyleTable;
    32. var pointRel = ed.GetPoint("\n拾取文字生成位置:");
    33. if (pointRel.Status != PromptStatus.OK) return;
    34. string lm = this.textBox1.Text;
    35. string mph = this.textBox2.Text;
    36. string cs = this.textBox3.Text;
    37. DBText text = new DBText();
    38. text.TextString = lm + mph + "号--" + cs;
    39. text.Position = pointRel.Value;
    40. text.VerticalMode = TextVerticalMode.TextVerticalMid;
    41. text.HorizontalMode = TextHorizontalMode.TextMid;
    42. text.AlignmentPoint = pointRel.Value;
    43. text.Height = 1;
    44. text.WidthFactor = 0.7;
    45. text.Layer = "门牌号";
    46. text.TextStyleId = tst["宋体"];
    47. modeSpace.AppendEntity(text);
    48. tr.AddNewlyCreatedDBObject(text, true);
    49. tr.Commit();
    50. }
    51. }
    52. }
    53. }