用VC++.net制作WinForm Application (转)

用VC++.net制作WinForm Application (转)[@more@]

  VC++.NET制作WinFoRM ApplicationXML:namespace prefix = o ns = "urn:schemas-microsoft-com:Office:office" />

新的.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 SCOrlib.dll>

#include

 

using namespace System;

 

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

int _tmain(void)

{

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

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

  return 0;

}

//

#using

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

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

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

 

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

// generated using an Application Wizard.

 

#include "stdafx.h"

 

#using

#include

 

#using

#using

#using windows.Forms.dll>

#using

 

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;

}

ASPectratio="t" v:ext="edit">word" />

 

 

 

 

 

 

 

 

 

 

 

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

  有关VC++制作.NET程序的资料可以到www.codoguru.comwww.codeproject.com去看看。

本文例子参考了” Building a .NET Windows Forms App in Visual C++ .NET “,

作者:Kate Gregory

  2002.10.4

 


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10748419/viewspace-1008667/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/10748419/viewspace-1008667/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值