简体   繁体   English

c#从控制台应用程序显示Windows窗体

[英]c# display windows form from console application

我正在研究控制台应用程序,我目前在我的项目中创建了一个Windows窗体(名称:Fcon),有没有可能的方法从控制台显示fcon?

The easiest option is to start a windows forms project, then change the output-type to Console Application. 最简单的选择是启动Windows窗体项目,然后将输出类型更改为控制台应用程序。 Alternatively, just add a reference to System.Windows.Forms.dll, and start coding: 或者,只需添加对System.Windows.Forms.dll的引用,然后开始编码:

using System.Windows.Forms;

[STAThread]
static void Main() {
    Application.EnableVisualStyles();
    Application.Run(new Form()); // or whatever
}

The important bit is the [STAThread] on your Main() method, required for full COM support. 重要的一点是Main()方法上的[STAThread],这是完全COM支持所必需的。

Another way : 其他方式 :

using System.Runtime.InteropServices;

private void Form1_Load(object sender, EventArgs e)
{
    AllocConsole();
}

[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool AllocConsole();

If you research this topic , just on Google you can find so much way to implement your question : my suggest is to try some method and verify what is more adapt to you. 如果您研究这个主题,只需在Google上,您就可以找到很多方法来实现您的问题:我的建议是尝试一些方法并验证哪些更适合您。

i have found other solution 我找到了其他解决方案

testform tf = new testform(); tf.Show();

just created forms object in main 刚刚在main中创建了表单对象

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 控制台应用程序来自 Windows 表单应用程序 C# - Control console application from Windows form application C# 将 C# 代码从控制台迁移到 Windows 窗体应用程序 - Migrate C# code from Console to Windows Form Application C#将Windows窗体应用程序添加到控制台应用程序 - C# Add Windows Form Application To A Console Application 如何连接控制台应用程序和Windows窗体应用程序C# - How to connect console application and Windows form application C# 如何将控制台应用程序中的变量值传递给 C# 中的 windows 表单应用程序(Visual Studio) - How do i pass a variable value from Console application to windows form application in C# (visual studio) 在C#(Windows窗体应用程序)中使用控制台窗口时发生异常 - Exception when using console window in C# (Windows form application) 如何在C#Windows Form应用程序中使用Console.write() - How Console.write() in C# Windows Form Application 在Windows窗体应用程序项目中使用C#控制台引用 - C# console reference use on windows form application project C#从退出时阻止控制台应用程序构建为Windows应用程序? - C# Prevent Console application build as Windows application from exit? 代替C#Windows应用程序中的dataGridView,我应该如何使用控制台应用程序显示来自sql server的表? - Instead of dataGridView in C# windows application, what should I use for console application to display a table from sql server?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM