2010-07-07 9 views
4

我想我的C#.NET應用程序有一個窗體,但不是一個窗體。如何使一個應用程序有一個窗體,但不是一個窗體?

當我正常啓動Windows窗體應用程序,它就像形式是一切遵循的主人:

static void Main() 
    { 
     Application.EnableVisualStyles(); 
     Application.SetCompatibleTextRenderingDefault(false); 
     Application.Run(new Form1()); 
    } 

相反,我想啓動我的程序中,然後能夠顯示一種形式,但不是一種形式本身。換句話說,我不希望應用程序的主控制器是表單,我希望它是一個非視覺邏輯容器,它有能力顯示錶單,但不是表單本身。

我不確定我是否以清晰的方式提出問題,但我想聽聽想法。

+0

什麼會觸發窗體的顯示? – 2010-07-07 19:15:25

回答

1

是很常見的,以創建一個單獨的引導程序組件,您可以移動的主要形式的顯示器:

using System; 
using System.Windows.Forms; 

namespace Example 
{ 
    internal static class Program 
    { 
     [STAThread] 
     private static void Main() 
     { 
      new Bootstrapper().Run(); 
     } 
    } 

    public class Bootstrapper 
    { 
     public void Run() 
     { 
      // [Application initialization here] 
      ShowView(); 
     } 

     private static void ShowView() 
     { 
      Application.EnableVisualStyles(); 
      Application.SetCompatibleTextRenderingDefault(false); 
      Application.Run(new Form1()); 
     } 
    } 
} 
+0

正是我在找的東西。 – 2010-07-13 16:55:54

7

您可以使用Application.Run()來獲取消息循環運行。但你需要做東西聽輸入 - 也許系統托盤等

+1

這可能很明顯,但我只想指出,如果沒有主窗體,那麼應用程序將不得不有一些邏輯來決定何時調用Application.Exit() – 2010-07-07 19:10:21

1

創建應用程序作爲控制檯應用程序,然後調用Application.Run作爲馬克說,當你需要一個窗體。

+0

除了控制檯應用程序將強制創建的控制檯窗口。這可能不是Adam想要的。 – 2010-07-07 19:06:42

2

正如Mark_Gravell所指,Application.Run()阻塞,直到Form1關閉。您可以在單獨的線程上打開表單,但該線程基本上會被表單使用。當你想要退出exe時,你必須手動殺死每個線程。請參閱以下代碼。 (它不會創建一個控制檯窗口。我得到這個通過創建默認的WinForms應用程序,改變程序類)

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Windows.Forms; 
using System.Threading; 

namespace WindowsFormsApplication1 
{ 
    static class Program 
    { 

     static List<Thread> threads = new List<Thread>(); 
     /// <summary> 
     /// The main entry point for the application. 
     /// </summary> 
     [STAThread] 
     static void Main() 
     { 
      for (int i = 0; i < 10; i++) 
      { 
       StartThread(); 
       System.Threading.Thread.Sleep(500); 
      } 
      //kill each thread so the app will exit, otherwise, the app won't close 
      //until all forms are manually closed... 
      threads.ForEach(t => t.Abort()); 
     } 

     static void StartThread() 
     { 
      Thread t = new Thread(ShowForm); 
      threads.Add(t); 
      t.Start(); 
     } 

     static void ShowForm() 
     { 
      Application.EnableVisualStyles(); 
      Application.SetCompatibleTextRenderingDefault(false); 
      Application.Run(new Form1()); 
     } 
    } 
} 
5

你可以使用一個ApplicationContext代替。這會讓你獲得必要的消息循環,一旦你決定創建一個表單,它就會保持表單的活性。使您的程序類看起來類似於此:

static class Program { 
    [STAThread] 
    static void Main() { 
     Application.EnableVisualStyles(); 
     Application.SetCompatibleTextRenderingDefault(false); 
     AppContext = new ApplicationContext(); 
     Application.Run(AppContext); 
    } 
    public static void Quit() { 
     AppContext.ExitThread(); 
    } 
    public static ApplicationContext AppContext; 
} 

請注意,關閉最後一個窗口時,應用程序不會自動關閉。顯式調用ExitThread是必需的。

0

您也可以創建自己的ApplicationContext

static void Main() 
{ 
    Application.EnableVisualStyles(); 
    Application.SetCompatibleTextRenderingDefault(false); 
    Application.Run(AppController.Instance); 
} 

並在AppController的。 cs

namespace MyApplication 
{ 
    public class AppController 
    { 
     static AppController _AppController; 

     public LoginWIndow LoginWIndow; 

     //Constructor 
     public void AppController() 
     { 
      //Do what you will here, Start login form, bind events, w.e :) 

      if(true) //Your check 
      { 
       ShowLoginWindow(); 
      } 
     } 

     public void ShowLoginWindow() 
     { 
      LoginWIndow = new LoginWIndow(); 
      LoginWIndow.ClosedForm += new FormClosedEventHander(ExitApplication); 
      LoginWIndow.Show(); 
     } 

     public void ExitApplication(Object Sender, FormClosedEventArgs Args) 
     { 
      //Some shutdown login Logic, then 
      Application.Exit(); 
     } 

     static AppController Instance 
     { 
      get 
      { 
       if(_AppController == null) 
       { 
        _AppController = new AppController(); 
       } 
       return _AppController; 
      } 
     } 
    } 
} 
相關問題