Useful little things when developing on C # under AutoCAD

  • Tutorial
In continuation of my previous post habrahabr.ru/post/164305 I continue to publish interesting subtleties when developing in C # under AutoCAD. Today we will focus on solving the problem of transferring focus to AutoCAD when using Modeless Window.

Briefly about the problem. To display your own dialog boxes created in WPF, you must use the Application.ShowModelessWindow (_mw) method;

public class CommandClass
{
static MainWindow _mw;

[CommandMethod("OpenMdlWindow")]
public static void OpenWindow()
{
if (_mw == null)
{
_mw = new MainWindow();
Application.ShowModelessWindow(_mw);
}

_mw.Closed += _mw_Closed;
}
}

Suppose we created a button on our form that, when clicked, starts the process of cleaning the drawing using the standard _purge command . To do this, add a method to the button click event handler:
Document _acDoc;

internal void Clear()
{
_acDoc.SendStringToExecute(
"._-PURGE " + "_ALL " + "\n" + "_N ",
false, false, false
);
};

What happens when the button is clicked? But nothing until we click with the mouse in the AutoCAD window.
Avoid unnecessary manual focus translation using only the .NET AutoCAD .NET API, but you will have to use P / Invoke. This is done simply:
1. Add

[DllImport("user32.dll")]
extern static IntPtr SetFocus(IntPtr hWnd);

2. Change our method

internal void Clear()
{
SetFocus(Application.DocumentManager.MdiActiveDocument.Window.Handle);

_acDoc.SendStringToExecute(
"._-PURGE " + "_ALL " + "\n" + "_N ",
false, false, false
);
}

. Everything.

Also popular now: