在.net控制台应用程序中显示消息框

如何在.net c#或vb 控制台应用程序中显示消息框? 就像是:

Console.WriteLine("Hello World"); MessageBox.Show("Hello World"); 

要么

 Console.WriteLine("Hello") MsgBox("Hello") 

分别在c#和vb中。
可能吗?

我们可以在控制台应用程序中显示一个消息框。 但首先在您的vb.net或c#控制台应用程序中包含此引用

 System.Windows.Forms; 

参考:

要在vb.net程序中添加引用,请右键单击(在解决方案资源管理器中)项目名称 – >然后添加reference->然后.Net->然后选择System.Windows.Forms。
要在c#程序中添加引用,请右键单击解决方案资源管理器中显示的项目文件夹,然后添加引用 – > .Net – >选择System.Windows.Forms。

然后你可以为c#console应用程序执行以下代码:

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; namespace ConsoleApplication6 { class Program { static void Main(string[] args) { MessageBox.Show("Hello World"); } } } 

对于vb.net应用程序,您可以在包含上述参考后简单地编码

 Module Module1 Sub Main() MsgBox("Hello") Console.ReadKey() End Sub End Module 

从这个答案改编成相关问题。

要在控制台应用程序中包含一个简单的消息框,您可以按照以下步骤操作。

  1. 创建属性为的属性

使用System.Runtime.InteropServices;

 [DllImport("User32.dll", CharSet = CharSet.Unicode)] public static extern int MessageBox(IntPtr h, string m, string c, int type); 
  1. 使用该属性调用消息框。

    MessageBox((IntPtr)0,“asdasds”,“我的消息框”,0);

      using System; using System.Runtime.InteropServices; namespace AllKeys { public class Program { [DllImport("User32.dll", CharSet = CharSet.Unicode)] public static extern int MessageBox(IntPtr h, string m, string c, int type); public static void Main(string[] args) { MessageBox((IntPtr)0, "Your Message", "My Message Box", 0); } } }