2009-10-26 162 views

回答

6

您应该能够添加引用了System.Windows.Forms的,然后是好去。您可能还必须将STAThreadAttribute应用于应用程序的入口点。

using System.Windows.Forms; 

class Program 
{ 
    [STAThread] 
    static void Main(string[] args) 
    { 
     MessageBox.Show("hello"); 
    } 
} 

...更复杂......

using System.Windows.Forms; 

class Program 
{ 
    [STAThread] 
    static void Main(string[] args) 
    { 
     var frm = new Form(); 
     frm.Name = "Hello"; 
     var lb = new Label(); 
     lb.Text = "Hello World!!!"; 
     frm.Controls.Add(lb); 
     frm.ShowDialog(); 
    } 
} 
4

是的,你可以在控制台中初始化一个表单。添加到System.Windows.Forms的一个参考,使用下面的示例代码:

System.Windows.Forms.Form f = new System.Windows.Forms.Form(); 
f.ShowDialog(); 
+0

我可以在downmods上得到一些评论吗? – 2009-10-26 20:09:26

+0

为什么这是低调?这可能不是很好的做法,但它绝对有可能。 – 2009-10-26 20:09:58

+0

这个工作没有STAThread属性吗? – 2009-10-26 20:15:41

1

你可以试试这个

using System.Windows.Forms; 

[STAThread] 
static void Main() 
{ 
    Application.EnableVisualStyles(); 
    Application.Run(new MyForm()); 
} 

再见。

4

常见的答案:

[STAThread] 
static void Main() 
{  
    Application.Run(new MyForm()); 
} 

替代品(从here拍摄)如果,例如 - 你想从比主应用程序的线程上推出的一种形式:

Thread t = new Thread(new ThreadStart(StartNewStaThread)); 

// Make sure to set the apartment state BEFORE starting the thread. 
t.ApartmentState = ApartmentState.STA; 
t.Start(); 

private void StartNewStaThread() { 
    Application.Run(new Form1()); 
} 

Thread t = new Thread(new ThreadStart(StartNewStaThread)); 
t.Start(); 

[STAThread] 
private void StartNewStaThread() { 
    Application.Run(new Form1()); 
}