Console with a WinForm

Often, you need a console window together with a WinForm application. It can be very handy for debugging purposes while developping, but also for a (temporary) logging of some data. It is very simple to do. The following program demonstrates it, using P/Invoke.

Start a new Windows application, drop a CheckBox on the form, name it ViewConsole, and copy the following code into it.

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;    // needed to call external
                                         // application (winAPI dll)

namespace WindowsApplication1
{
   partial class Form1: Form
   {
      public Form1()
      {
         InitializeComponent();
      }

      private void ViewConsole_CheckedChanged(object sender,
                                              EventArgs e)
      {
         if (ViewConsole.Checked)
            Win32.AllocConsole();
         else
            Win32.FreeConsole();
      }
   }

   public class Win32
   {
      [DllImport("kernel32.dll")]
      public static extern Boolean AllocConsole();
      [DllImport("kernel32.dll")]
      public static extern Boolean FreeConsole();
   }
}


Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • The hard facts on SaaS adoption in over 80,000 enterprises: Public vs. private companies Mid-market vs. large enterprise GoogleApps, Office365, Salesforce & more Why security is a growing concern Fill out the form to download the full cloud adoption report.

  • On-demand Event Event Date: December 18, 2014 The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this webcast and join industry experts as …

Most Popular Programming Stories

More for Developers

RSS Feeds