c# - 获取运行 Windows 窗体控件的应用程序

标签 c# winforms autocad-plugin

我正在制作一个 Autocad 插件,它使用 Windows Forms 运行良好 我还创建了一个用户控件 (winforms),以便将来以我喜欢的任何形式进行复制。

问题是

From the control's code, how do I get the instance of the application running that control?

(Probably a pure winforms problem)

条件:

Within the plug-in I can get the Autocad Application instance with no problem.

此用户控件应位于一个单独的程序集 (dll) 中,以便在插件应用程序中引用,因此它无法直接访问应用程序实例。


关于场景的一些解释:

有一个 Main Assembly 由 Autocad 作为插件运行。 该程序集已实例化 Autocad 应用程序。

现在我有一些有用的表单控件可以与 Autocad 一起使用,它们位于单独的程序集中。 (那是因为我想在我喜欢的许多不同插件中使用它们)。

因此,Autocad 运行主程序集,而主程序集 运行单独程序集 的控件。

为了正常工作,这些控件需要访问运行主程序集的 Autocad 应用程序。

今天我将应用程序用作控件中的一个属性,我必须在使用它们之前设置它。 (如果我忘记设置它,则会引发异常)。因为我无法控制带有参数的创建者。

我希望控件能够检测到它们正在运行的应用程序,因此我避免了这种解决方法。

最佳答案

请看下面的代码

public class MyCommands {

    [CommandMethod("NS", "TEST", "TEST", CommandFlags.Modal)]
    public void TestCommand() // This method can have any name
    {
        Form fromAutoCADAPI = new TestForm();
        Form independent1 = new TestForm();
        Form independent2 = new TestForm();

        //Using AutoCAD application
        Autodesk.AutoCAD.ApplicationServices.Application.ShowModelessDialog(fromAutoCADAPI);

        independent1.Show();
        independent2.Show();

        //Using Windows Forms Application
        var count = System.Windows.Forms.Application.OpenForms.Count; //should be 3

        Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(count.ToString());

    }
}

如果这是您已经知道的内容,那么您可能应该粘贴一些示例代码,以帮助了解您在代码中的具体位置。这就是我使用 AutoCAD 应用程序和 Windows 窗体应用程序的方式。如果您不想获取 WPF 应用程序,则可以使用

var application = System.Windows.Application.Current;

关于c# - 获取运行 Windows 窗体控件的应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16358379/

相关文章:

c# - EditorHelper 是否使用 ViewBag 将数据传递给相应的模板?

c# - 组合框的 SelectedValue 不反射(reflect)传递给空项时的更改

C# Func<> - 寻找解释 - 而不是解决方案

c# - 使用箭头键调用方法 C#

c# - 具有多个参数的 Web API 路由

windows - ElementHost 中的 Application.Current 为空

c# - 在另一个应用程序中的 MessageBox 中捕获按钮单击事件

c# - 使用 Lisp 函数将 .NET 插件加载到 AutoCAD 2014

python - 使用 Python 保存 AutoCAD 文件 (.dwg)

c# - 在 Autocad 绘图中查找特定属性中具有特定值的对象