📜 ⬆️ ⬇️

Useful stuff when developing in C # under AutoCAD

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 talk about solving the problem of focus transfer in AutoCAD using the Modeless Window.

Briefly about the problem. To display custom dialogs 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 have 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 event handler:
Document _acDoc;

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

What happens when you press a button? And nothing, until we click the mouse in the AutoCAD window.
Avoiding unnecessary manual translation of the focus using only the .NET API of AutoCAD does not work out, so you have to use P / Invoke. This is done simply:
1. Add

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

2. We change our method
')
internal void Clear()
{
SetFocus(Application.DocumentManager.MdiActiveDocument.Window.Handle);

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

Everything.

Source: https://habr.com/ru/post/168971/


All Articles