[英]Windows form from console
我想使用C#從控制台生成Windows窗體。 大致類似於Linux中的display
,並修改其內容等。這可能嗎?
您應該能夠為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();
}
}
是的,您可以在控制台中初始化表單。 添加對System.Windows.Forms的引用,並使用以下示例代碼:
System.Windows.Forms.Form f = new System.Windows.Forms.Form();
f.ShowDialog();
常見答案:
[STAThread]
static void Main()
{
Application.Run(new MyForm());
}
替代方案(取自此處 ),例如,您想要從主應用程序以外的線程啟動表單:
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());
}
你可以試試這個
using System.Windows.Forms;
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new MyForm());
}
再見。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.