2013-11-09 31 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窗體」,就像上面一樣。兩種方法來獲得相同的結果。