为SplashScreen创建一个C#组件

为SplashScreen创建一个C#组件,c#,user-controls,splash-screen,C#,User Controls,Splash Screen,我想创建一个新组件,当我调用.show()方法时,它会显示一个初始屏幕。该组件必须类似于Windows窗体,具有图像和以毫秒为单位的持续时间,并传递类似参数。我应该在VisualStudio中选择什么类型的项目来实现这一点?如果我选择一个类库,它会创建一个dll类,但如果我选择一个新的ControlLibrary,它会创建一个新控件,但我不能使用Windows窗体 protected int nSec; public SplashScreen(string img, int n

我想创建一个新组件,当我调用.show()方法时,它会显示一个初始屏幕。该组件必须类似于Windows窗体,具有图像和以毫秒为单位的持续时间,并传递类似参数。我应该在VisualStudio中选择什么类型的项目来实现这一点?如果我选择一个类库,它会创建一个dll类,但如果我选择一个新的ControlLibrary,它会创建一个新控件,但我不能使用Windows窗体

    protected int nSec;

    public SplashScreen(string img, int nSec)
    {
        // duration
        this.nSec = nSec;

        // background splash screen
        this.BackgroundImage = Image.FromFile("img.jpg");

        InitializeComponent();
    }

    private void SplashScreen_Load(object sender, EventArgs e)
    {
        timer1.Interval = nSec * 1000;
        timer1.Start();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        this.Close()
    }

我希望在以后的其他工作中重用此“组件”,而不必每次都创建一个新组件。

听起来他们希望您创建一个类库,并让它为您创建表单

//Whatever other usings you want
using System.Windows.Forms;  //Include the win forms namespace so you create the form

namespace ClassLibrary1
{
public static class Class1
{

    public static Form CreateNewForm()
    {

        var form1 = new Form();
        form1.Width = 200;
        form1.Height = 200;
        form1.Visible = true;
        form1.Activate();        //Unsure if you need to call Activate...
        //You're going to want to modify all the values you want the splash screen to have here
        return form1;

    }   

}
}

所以在另一个项目中,比如控制台应用程序,我可以引用我刚刚创建的类库,调用CreateForm函数,它会在运行时弹出一个宽度和高度为200的表单

using ClassLibrary1; //You'll need to reference this

    //Standard console app template

    static void Main(string[] args)
    {
        var x = Class1.CreateNewForm(); //Bam form pops up, now just make it a splash screen.
        Console.ReadLine();
    }

希望这就是您想要的

避免假设这些项目模板背后有魔力,您可以轻松地自行配置项目。使用类库项目模板很好,只需在创建项目后右键单击该项目,选择“添加新项”,然后选择“Windows窗体”。除了添加表单并在设计器中打开它之外,这还向项目的“引用”节点添加了两项:System.Drawing和System.Windows.Forms


当您选择“Windows窗体控件库”项目模板时,将自动获取该模板。它还自动添加了一个UserControl。如果不需要,只需右键单击项目中的UserControl1.cs项并选择Delete。添加新项目以选择“Windows窗体”,如上所述。有两种方法可以获得相同的结果。

您需要类似Windows窗体的东西,但不能使用Windows窗体。您的意思是不能使用Windows窗体项目模板吗?或者你不能使用表单对象?听起来他们想让你创建一个类库并让它创建表单,然后你可以调用引用该库并在需要启动屏幕时调用所需的函数。但是我应该创建一个类库,其中包含一个带有启动屏幕的Windows表单?