一个可以从主应用程序启动的WinForms DLL

我创建了一个C#DLL,其中包含一些表单。 (我需要它是一个DLL,而不是Windows应用程序。)如何将其作为Windows应用程序运行? 我应该创建另一个应用程序并加载它吗? 怎么样? 我需要学习什么才能做到这一点? 如果我应该更多地解释我的问题,请告诉我。

如果您使用的是VS 2008:

首先,创建一个Windows窗体应用程序项目。 如果您不打算使用它,可以删除创建的默认表单(Form1.cs)。

在Solution Explorer中,右键单击References并选择Add Reference 。 这是您添加自定义设计的C#DLL的地方。

现在打开Program.cs,并进行以下更改:

using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; using ****your DLL namespace here**** namespace WindowsFormsApplication2 { static class Program { ///  /// The main entry point for the application. ///  [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new [****your startup form (from the DLL) here****]); } } } 

如果DLL包含断开连接的表单,您可能需要在winforms项目中添加一个类来协调表单行为。

您可以向DLL添加表单,然后在DLL中使用表单调用Application.Run中的公共静态函数。

然后,您可以从C#Application项目中调用此公共静态方法(在添加对DLL的引用之后)。

您可以使用RunDll32启动它,但是在它工作之前您可能需要稍微调整一下dll。 您可能需要在入口点放置Application.Run 。 这样您就不需要编译另一个整个应用程序来使用它。

以下代码未经测试,但我认为它应该可行。

 public static void myDllEntryPoint() { Application.run(new MyFormInDll()); } 

运行您的应用程序

 rundll32.exe myDll.dll,myDllEntryPoint