Compile Options

  • Thread starter Thread starter A.M
  • Start date Start date
A

A.M

Hi,

I need to develop an application that acts as both consolde or windows
formas application based on command line args.

If have two choice as compiler output options: Console Application or
Windows Application.

If I choose Console Application as compiler output, and then I run the
application in windows mode,it shows a blank black window and the dialog box
window.
If I choose Windows Application, Then the application can not run in console
mode and [Console.Write] or [Console.Read] won't work.

Is it possible that a windows form application can also show a console
window?

Thanks,
Alan
 
A.M said:
If I choose Console Application as compiler output, and then I run the
application in windows mode,it shows a blank black window and the
dialog box window.

Yup, the process is marked as requiring a console, so windows will always
create a console for it.
If I choose Windows Application, Then the application can not run in
console mode and [Console.Write] or [Console.Read] won't work.

Ah, but Consol.In and Console.Out can be replaced with TextReader/TextWriter
(use SetIn and SetOut methods), so you can direct Console Read/Write to your
TextReader/TextWriter, I've attached some very bare bones code that shows
this:

using System;
using System.IO;
using System.Text;
using System.Windows.Forms;

// Basic dialog that displays the text
class GUIConsole : Form
{
ListBox lb;
public GUIConsole()
{
lb = new ListBox();
lb.Dock = DockStyle.Fill;
this.Controls.Add(lb);
}
public void WriteLine(string s)
{
// this is not satisfactory, it ignores newlines in the string
lb.Items.Add(s);
}
}

// Very basic text writer, need to implement WriteLines that take data types
other
// than string, need to implement Write
class Writer : TextWriter
{
GUIConsole cons;
public Writer(GUIConsole cons)
{
this.cons = cons;
cons.Show();
}
public override Encoding Encoding
{
get{ return null; }
}
public override void Close()
{
cons.Visible = false;
base.Close();
}
// All of the dormatted WriteLines call this
public override void WriteLine(string s)
{
cons.WriteLine(s);
}
}

// Application form
class App : Form
{
GUIConsole console;
App()
{
console = new GUIConsole();
Console.SetOut(new Writer(console));
this.Click += new EventHandler(this.click);
}
// If you click on the form, it test will be printed on the 'console'
void click(object o, EventArgs args)
{
Console.WriteLine("test");
}
static void Main()
{
Application.Run(new App());
}
}

Richard
 
Compile it as Console application - this doesn't prevent you from showing
Windows Forms as well.
 
Thank you for help.

If you do thar, The problem is you always have a blank console window beside
the winforms window!

Alan


cody said:
Compile it as Console application - this doesn't prevent you from showing
Windows Forms as well.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
A.M said:
Hi,

I need to develop an application that acts as both consolde or windows
formas application based on command line args.

If have two choice as compiler output options: Console Application or
Windows Application.

If I choose Console Application as compiler output, and then I run the
application in windows mode,it shows a blank black window and the dialog box
window.
If I choose Windows Application, Then the application can not run in console
mode and [Console.Write] or [Console.Read] won't work.

Is it possible that a windows form application can also show a console
window?

Thanks,
Alan
 
Hi AM,

As for the scenario you mentioned, I think Richard's suggestion on defining
a customer "Stream" object (the GUIConsole) and redirect our winform
application's Console output to the "GUIConsole" is considerable. Richard
use a "listbox" as the output in its sample, you can event directly
display console 's output on your main form.
Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
You mean you want to let it pop up if you want, not seeing it all the time?
Well, you could call cmd.exe/command.com and get its window handle
(interop). But writing something in there will surely be not easy.
Why don't you simply create an additional normal winforms window and put a
multiline textbox in there? You can also make it look like the original
command window.

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
A.M said:
Thank you for help.

If you do thar, The problem is you always have a blank console window beside
the winforms window!

Alan


cody said:
Compile it as Console application - this doesn't prevent you from showing
Windows Forms as well.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
A.M said:
Hi,

I need to develop an application that acts as both consolde or windows
formas application based on command line args.

If have two choice as compiler output options: Console Application or
Windows Application.

If I choose Console Application as compiler output, and then I run the
application in windows mode,it shows a blank black window and the
dialog
box
window.
If I choose Windows Application, Then the application can not run in console
mode and [Console.Write] or [Console.Read] won't work.

Is it possible that a windows form application can also show a console
window?

Thanks,
Alan
 
Hi Alan,

Have you considered Richard's suggestion on Redirect the Console's output
to our custom Writer? Here is a simple demo I built through Richard's
suggestion which redirect the "Console" output to the Main Winform, just as
the console:


======================================================
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Text;

namespace GUIConsoleApp
{
public class GUIConsoleForm : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox txtConsole;
private System.ComponentModel.Container components = null;
private GUIConsoleWriter writer = null;

public GUIConsoleForm()
{
InitializeComponent();
writer = new GUIConsoleWriter(this);
Console.SetOut(writer);
}


protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

public void WriteLine(string str)
{
txtConsole.Text = txtConsole.Text + System.Environment.NewLine + str;
txtConsole.SelectionStart=this.txtConsole.Text.Length;
txtConsole.ScrollToCaret();
}


#region Windows ´°ÌåÉè¼ÆÆ÷Éú³ÉµÄ´úÂë

private void InitializeComponent()
{
this.txtConsole = new System.Windows.Forms.TextBox();
this.SuspendLayout();

this.txtConsole.BackColor =
System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(0)),
((System.Byte)(64)));
this.txtConsole.Dock = System.Windows.Forms.DockStyle.Fill;
this.txtConsole.ForeColor = System.Drawing.Color.White;
this.txtConsole.Location = new System.Drawing.Point(0, 0);
this.txtConsole.Multiline = true;
this.txtConsole.Name = "txtConsole";
this.txtConsole.ReadOnly = true;
this.txtConsole.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.txtConsole.Size = new System.Drawing.Size(544, 317);
this.txtConsole.TabIndex = 0;
this.txtConsole.Text = "";
this.txtConsole.MouseDown += new
System.Windows.Forms.MouseEventHandler(this.txtConsole_MouseDown);

this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackColor = System.Drawing.SystemColors.ControlDarkDark;
this.ClientSize = new System.Drawing.Size(544, 317);
this.Controls.Add(this.txtConsole);
this.Name = "GUIConsoleForm";
this.Text = "GUIConsole";
this.Load += new System.EventHandler(this.GUIConsoleForm_Load);
this.ResumeLayout(false);

}
#endregion


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


private void GUIConsoleForm_Load(object sender, System.EventArgs e)
{
Console.WriteLine("Form Load!");
}

private void txtConsole_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
Console.WriteLine("MouseDown at (" + e.X + " , " + e.Y + ")");
}
}

class GUIConsoleWriter : TextWriter
{
GUIConsoleForm _console;

public GUIConsoleWriter(GUIConsoleForm console)
{
this._console = console;
}
public override Encoding Encoding
{
get{ return null; }
}
public override void Close()
{
_console.Visible = false;
base.Close();
}

public override void WriteLine(string s)
{
_console.WriteLine(s);
}
}

}
==========================================

Hope helps. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
Back
Top