1

我想创建一个新组件,当我调用 .show() 方法时它会显示启动画面。该组件必须像一个带有图像的 Windows 窗体,并且以毫秒为单位传递类似参数的持续时间。为此,我应该在 Visual Studio 中选择哪种类型的项目?如果我选择一个 ClassLibrary,它会创建一个 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()
    }

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

4

2 回答 2

1

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

//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();
    }

希望这就是你要找的

于 2013-11-09T18:29:37.063 回答
1

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

当您选择“Windows 窗体控件库”项目模板时,您会自动获得它。其中还自动添加了一个UserControl。您不需要,只需右键单击项目中的 UserControl1.cs 项并选择删除。Add New Item 选择“Windows Form”,同上。两种方法可以得到相同的结果。

于 2013-11-09T18:35:30.397 回答