473,403 Members | 2,183 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Why should I write code in header file in VC++.Net winforms?

Hi,

In VC++.Net 2005 visual studio, if I create a new winform drag and drop
a button, then double click it to write click event code, then it auto
generates the template code for the event in Form1.h file rather than
Form1.cpp file. This behaviour is against standard C++ coding
standards, where my header file is supposed to just have declarations.
I read in other topics that this is due to C# association of single
file concept with the winform.

Can someone (from MS or MS experts) explain why is this done? and how
can I avoid it to generate template code in header file and write my
code in .cpp file?

Thanks and Regards,
Shil

Feb 4 '06 #1
4 3047
Shil wrote:
Hi,

In VC++.Net 2005 visual studio, if I create a new winform drag and
drop a button, then double click it to write click event code, then
it auto generates the template code for the event in Form1.h file
rather than Form1.cpp file. This behaviour is against standard C++
coding standards, where my header file is supposed to just have
declarations. I read in other topics that this is due to C#
association of single file concept with the winform.

Can someone (from MS or MS experts) explain why is this done? and how
can I avoid it to generate template code in header file and write my
code in .cpp file?


That's the way the designers work. You can adapt to that style or not use
the designer - there's little other choice. The designers were designed for
C# and VB and don't support the header/implementation file idiom commonly
used for C and C++ programming.

-cd
Feb 4 '06 #2
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.

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.
Apparently nowadays if you start a console project you must have a console,
and if you start a winform project you must use the visually-oriented
(drag-and-drop controls) Form.

Mind you, I think the drag-and-drop paradigm is great, I just wish I didn't
have to use it if I don't want a console as part of the deal. I like to
create my own custom Forms, and dislike it when the language writes
automatic code for me (and places it in a "do not touch" area of the
source). I'm sure this is possible, and my igorance is raising it's ugly
head again...

[==P==]

"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:%2****************@TK2MSFTNGP11.phx.gbl...
Shil wrote:
Hi,

In VC++.Net 2005 visual studio, if I create a new winform drag and
drop a button, then double click it to write click event code, then
it auto generates the template code for the event in Form1.h file
rather than Form1.cpp file. This behaviour is against standard C++
coding standards, where my header file is supposed to just have
declarations. I read in other topics that this is due to C#
association of single file concept with the winform.

Can someone (from MS or MS experts) explain why is this done? and how
can I avoid it to generate template code in header file and write my
code in .cpp file?


That's the way the designers work. You can adapt to that style or not use
the designer - there's little other choice. The designers were designed
for C# and VB and don't support the header/implementation file idiom
commonly used for C and C++ programming.

-cd

Feb 6 '06 #3
Mind you, I think the drag-and-drop paradigm is great, I just wish I
didn't
have to use it if I don't want a console as part of the deal. I like to
create my own custom Forms, and dislike it when the language writes
automatic code for me (and places it in a "do not touch" area of the
source). I'm sure this is possible, and my igorance is raising it's ugly
head again...


If you create a Form project and examine its code,
you'll see that there is nothing there that you cannot type in yourself.
Code generated by MFC wizards can be a bit obscure, but .NET forms code is
plain to read.
If you want to code that stuff by hand there is nothing to prevent you form
doing so.
You can then use your own form as the startup form.

If you don't want to start from the form that is generated for you if you
create a new forms project,
just throw it away and provide your own form.

--

Kind regards,
Bruno.
br**********************@hotmail.com
Remove only "_nos_pam"
Feb 6 '06 #4
"Peter Oliphant" <po*******@RoundTripInc.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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.
</fwiw>
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::Drawing;
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;
}
//---------------------------------------------------------

Feb 6 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

242
by: James Cameron | last post by:
Hi I'm developing a program and the client is worried about future reuse of the code. Say 5, 10, 15 years down the road. This will be a major factor in selecting the development language. Any...
16
by: Jason | last post by:
Hi, I need a way to use random numbers in c++. In my c++ project, when using the mingw compiler I used a mersenne twister that is publicly available and this did its job well. Now I have...
5
by: Henri Schomäcker | last post by:
Hi folks, I have a quite big class, which I want to use on UNIX-like systems and on win32. Until now, everything is absolutely portable. But now I need to read a directory and use the os...
4
by: Andrew Ward | last post by:
Hi All, I was wondering if it is possible to use precompiled headers without having to include a <stdafx.h> or whatever in every source file. My problem is that I have a project that makes heavy...
15
by: TT (Tom Tempelaere) | last post by:
Hey everyone, I noticed that unlike C#, managed C++ still uses headers and implementation files. My question is, is there any consensus of where to put the implementation of the managed class? ...
6
by: Abubakar | last post by:
Hi, I'm working on a project in unmanaged c++. I was writing all (most of) my code in header files, that is, no seperation of code in header and cpp files, as usually is done in c++. I feel pretty...
4
by: Duncan Smith | last post by:
I have a VS2005 C++ MFC project which #imports a type library. The goal is to introduce some managed code eventually, but for starters I just need to set the /clr compiler option and build the...
2
by: tomb | last post by:
I had originally posted this to the comp.lang.C++.moderated group, and was advised to bring this question here. I haven't used C++ in quite a few years, and that was when I was learning and made...
0
by: dot | last post by:
I spent a few headache filled days trying to use GMP on windows (XP pro) I finally got it to work and since I found little help on the Web I thought someone might find what i did useful. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.