用VC.NET制作WinForm应用程序

         新的.NET Framework出世了,随之而来的是新的开发方法,开发理念。这本无可厚非的,但微软似乎总是偏爱她的嫡传(VB,VC#), .NET Framework似乎也是为他们量身定做的。 唯独大众化的C++被冷落了(虽然MFC也进化到了7.0,对标准C++支持也比以前强了),先进的RAD开发模式始终远离VC++(也许是MFC框架太成熟了,改起来不方便),不过微软为了让VC++也沐浴点.NET的光泽,推出了MC++(Managed Extensions for C++.),C++

  程序员眼光一亮,这回我们也可以追随.NET了(虽然依然不能像VB,VC#程序员那样用鼠标指指点点就可以完成一个程序)。Microsoft .NET Framework Class Library .NET 框架类库

  用惯了MFC和API编程,冷不丁的转向. NET Framework可能还有些不习惯,不过用时间长了,你会发现. NET Framework也不错么。其实我们还是在用”MFC”,只不过这次是 “MFC……Microsoft .NET Framework Class Library, 微软.NET 框架类库”。

  下面我用一个例子来谈谈我的体会我将用MC++制作一个WinForm Application,WinForm是.NET的标准GUI界面,功能十分之强大。下面我们就来看看用MC++如何制作一个简单的Hello,World!程序。

  由于文章篇幅有限,MC++的特性我在这里就不详细介绍了,我将在制作过程中稍加解释。下面就开始了:

  在VS.NET中,建立一个VC++新项目,我们把她取名为VCWinFormApp,当然它是个托管的C++应用程序类型了。之后点击“确定”。等待吧!(我的赛扬366正在接受着VS.NET的考验)一袋烟的功夫,该创建的文件都已经创建好了。看看吧,有什么东西,哇,只有

  VCWinFormApp.cpp一个主要文件(我们经常认为是.h,..cpp比较重要),stdafx.h和stdafx.cpp文件里什么也么有。(想一想原来MFC为我们在stdafx.h中添加了那么多东东)。不过不要怕,.NET Framework就是这样,她会让你变懒的(需要你添加的代码很少)。在看看VCWinFormApp.cpp吧。

//////////////////////////////////////////////////////////

#include "stdafx.h"

#using <mscorlib.dll>

#include <tchar.h>

using namespace System;

// 这是此应用程序的入口点

int _tmain(void)

{

// TODO: 请用您自己的代码替换下面的示例代码。

Console::WriteLine(S"Hello World");

return 0;

}

//////////////////////////////////////////////////////////////


#using <mscorlib.dll>

using namespace System;

  两句我们以前未见过的语句,这里简单解释一下, system 是.NET Framework的最基础的命名空间。

  System 命名空间包含基本类和基类,这些类定义常用的值和引用数据类型、事件和事件处理程序、接口、属性和异常处理。

  mscorlib.dll是专为MC++定制的dll文件,她为.NET Framework接纳C++铺平了道路。

  上面的程序仅仅是一个console程序而不是我们要做的WinForm Application,下面的代码会将她改写成一个WinForm Application。

预备知识:
The #using Directive-------用来将一些MC++使用的metadata(元数据)导入程序。如

#using <mscorlib.dll>

// 这是使用应用程序向导生成的 VC++

// 应用程序项目的主项目文件。

// This is the main project file for VC++ application project

// generated using an Application Wizard.

#include "stdafx.h"

#using <mscorlib.dll>

#include <tchar.h>

#using <System.dll>

#using <System.Drawing.dll>

#using <System.Windows.Forms.dll>

#using <System.Data.dll>

using namespace System;

using namespace System::Drawing;

using namespace System::Collections;

using namespace System::ComponentModel;

using namespace System::Windows::Forms;

using namespace System::Data;

namespace myspace

{

public __gc class Form1 : public System::Windows::Forms::Form

{

private:

Button* button1;

Label* label1;

System::ComponentModel::Container* components;

public:

Form1()

{

//

// Required for Windows Form Designer support

//

components = NULL;

InitializeComponent();

//

// TODO: Add any constructor code after

// InitializeComponent call

//

}


protected:

void Dispose( bool disposing )

{

if( disposing )

{

if (components != NULL)

{

components->Dispose();

}

}

Form::Dispose( disposing );

}


private:

void InitializeComponent()

{

button1 = new Button();

label1 = new Label();

SuspendLayout();

//

// button1

//

button1->Location = Point(23, 96);

button1->Size=System::Drawing::Size(100,60);

button1->Name = "button1";

button1->TabIndex = 0;

button1->Text = "Start my first C++ WinForm Application";

button1->Click += new System::EventHandler(this,

&Form1::button1_Click);

//

// label1

//

label1->Location = Point(150, 84);

label1->Name = "label1";

label1->TabIndex = 1;

//

// Form1

//

AutoScaleBaseSize = System::Drawing::Size(5, 13);

ClientSize = System::Drawing::Size(292, 273);

Controls->Add(label1);

Controls->Add(button1);

Name = "Form1";

Text = "Form1";

ResumeLayout(false);

}

private:

void button1_Click(Object* sender, System::EventArgs* e)

{

label1->Text = "Hello World!";

}

};

}

// This is the entry point for this application

int __stdcall WinMain()

{

Application::Run(new myspace::Form1());

return 0;

}

  我相信上面的程序大多数C++程序员都应该能看懂的(尤其是熟悉MFC,ATL的程序员)所以我也不必多说。

posted @ 2006-09-29 14:05  TangHuawei  阅读(272)  评论(0编辑  收藏  举报