Peter Oliphant said:
This might exist, but I'm looking for that cross between a console project
and a winform project. That is, I want to be able to create a C++.NET
application that starts by not showing EITHER a console or a form. I want
to be able to build my own form from 'scratch' (derived from Form), but
not have to start with a blank project. Something that sets up a 'hello
world' but doesn't actually put 'hello world' anywhere since no default
output screen is defined.
<fwiw>
Well, there is always a tradeoff between flexibility and ease of use. The
wizards limit flexibility to some extent to make the _most_ frequent tasks
easy. It's rarely easy to get a wizard to anything but what it knows best.
This use to exist in 2003 by virtue of the fact that if you started a
console project you could later on (or early on) disable the console.
Hmm, really? In my experience console projects always have consoles. The
only "exception" I know of is the case of a console process created via
CreateProcess() with the CREATE_NO_WINDOW or DETACHED_PROCESS flags. In
other cases the o/s, the command shell and the runtime conspire to create a
console.
Though I'm not sure it will, I hope the rest of this post will help you.
I started with a sample I put together for you some time ago that created a
..Net WinForms application without the wizard. The goal then was to
demonstrate a little drawing under .Net.
I've modified it so that it takes a single parameter. If it finds the string
"window" on the command line it creates a form. If it does not it will use
its parent's console or create a new one if its parent doesn't have one.
The sample is actually a Win32 application, compiled with the /CLR switch
linked as a windowed application ( /Subsystem:Windows ).
This is its header file:
//---------------------------------------------------------
#include <string>
#using <System.dll>
#using <mscorlib.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>
using namespace System;
using namespace System:
rawing;
using namespace System::Windows::Forms;
using namespace System::ComponentModel;
using namespace System::Runtime::InteropServices;
[ DllImport("KERNEL32.dll", EntryPoint="AllocConsole",
CallingConvention=CallingConvention::StdCall) ]
extern "C" unsigned short int AllocConsole(void);
[ DllImport("KERNEL32.dll", EntryPoint="AttachConsole",
CallingConvention=CallingConvention::StdCall) ]
extern "C" unsigned short int AttachConsole(int);
public __gc class HelloWorld : public Form
{
public:
HelloWorld();
static void Main();
static void Main2();
protected:
void OnPaint(PaintEventArgs __gc *);
void OnResize(EventArgs __gc *);
};
//---------------------------------------------------------
This is its source file:
//---------------------------------------------------------
#include "po.h"
void HelloWorld::Main()
{
Application::Run( new HelloWorld() );
}
void HelloWorld::Main2()
{
if ( AttachConsole(-1) == 0 )
AllocConsole();
Console::WriteLine("Hello, world!");
}
HelloWorld::HelloWorld()
{
Text = "Hello, world!";
BackColor = Color::White;
}
void HelloWorld::OnPaint(PaintEventArgs *pea)
{
Graphics __gc *grfx = pea->Graphics;
Rectangle rc;
grfx->DrawString("Hello, World!", Font, Brushes::Black, 0, 0);
rc = get_ClientRectangle();
rc.Width -= 1;
rc.Height -= 1;
grfx->DrawLine(Pens::Red, 0, 0, rc.Width, rc.Height);
grfx->DrawEllipse(Pens::Blue, rc);
}
void HelloWorld::OnResize(EventArgs *ea)
{
Invalidate();
}
// Hack avoids including <windows.h>
int __stdcall WinMain(int, int, char *pszCmd, int)
{
std::string cmdLine(pszCmd);
if ( cmdLine.find("window") != std::string::npos )
{
HelloWorld::Main();
return 0;
}
else if ( cmdLine.find("console") != std::string::npos )
{
HelloWorld::Main2();
return 0;
}
else
return -1;
}
//---------------------------------------------------------