2013-11-09 24 views
1

我想创建一个新组件,当我调用.show()方法时,它将显示一个启动画面。该组件必须像Windows窗体一样具有图像和以毫秒为单位的持续时间,如同参数一样传递。我应该在Visual Studio中选择哪种类型的项目?如果我选择一个ClassLibrary,它创建一个dll类,但如果我选择一个新的ControlLibrary它创建一个新的控件,但我不能使用Windows窗体。为SplashScreen创建一个C#组件

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

我希望在未来的其他工作中重复使用此“组件”,而不是每次都创建一个新组件。

+0

您需要像Windows窗体一样的东西,但不能使用Windows窗体。你的意思是你不能使用Windows窗体项目模板?或者你不能使用Form对象? –

+0

听起来像他们希望你创建一个类库并让它创建表单,那么显然可以调用引用该库并在需要启动屏幕时调用所需的函数。 –

+0

但我应该创建一个包含Windows窗体的ClassLibrary和我的Splash Screen? – user2921326

回答

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

希望的高度,这就是你要找的

1

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

当您选择“Windows窗体控件库”项目模板。其中还自动添加了一个UserControl。您不需要的,只需右键单击项目中的UserControl1.cs项并选择删除。添加新项目以选择“Windows窗体”,就像上面一样。两种方法来获得相同的结果。