序
Delphi出到8时,我曾安装过。当时第一感觉是失望,因为熟悉的vcl视觉不见了;再一个感觉是陌生,因为delphi改动了它的代码,我们再要写东西就得遵循MS的.net命名空间做事。更重要的是,我对使用delphi做b/s开发没有信心。好不爽了一阵子后,我转向了java平台。
但后来,我看到asp.net真的非常好,而且delphi也可以实现它,这让我有种想回见老友的冲动。但当时没时间学,所以还不太懂。我对IntraWeb和asp.net两种实现都很有兴趣,很想试试。在后来,c# builder1.0的试用让我对borland多少有了点好感,但还是觉得它是跟屁虫,已经没有ms抗衡的力量了。这让我想起了加菲猫的一句话,如果打不过你的敌人,最好的办法就是加入他们。
今天,我对delphi有了另一种态度。不再苛刻地要求它就是最好最快的,而是希望自己在b/s也能用得上delphi,并觉得还好用,这就足够了。至于它外观和空间的变化,在delphi8后,我已开始接受,毕竟delphi不走.net就没什么路可走了。
当我意外地得到borland寄来的delphi2005试用版时,我就想得到了一款正在流行的游戏一样,非常想试玩一下。可是,borland的注册太没有“中国特色”了,害得我跑到网上弄了个注册机。不做D版用户还不太习惯。
(一)Hello World.
Delphi2005是个集成环境,包括了delphi、c#,好像还可以使用vb.net,但这不是常规菜单里的内容。我感觉borland对此款软件的命名有问题,应该叫borland.net2005才对,不然用delphi开发c#,听起来有点搞笑。
先来用delphi写个Hello World吧。在2005里,开发delphi有三种不同的途径,自然应用环境也不同。分别是:
1 VCLForms Application for .NET
2 WindowsForms Application for .NET
3 VCLForms Application for Win32.
下面是三个方式下的Hello World.
1 VCLForms Application for .NET
单元代码:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
edit1.Text :='Hello World.';
end;
end.
窗体代码:
object Form1: TForm1
Left = 0
Top = 0
Width = 281
Height = 138
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object Button1: TButton
Left = 88
Top = 56
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 0
OnClick = Button1Click
end
object Edit1: TEdit
Left = 8
Top = 8
Width = 249
Height = 21
TabOrder = 1
end
end
这看起来,和从前的win32开发没有什么不同。单元和窗体分开,分别作处理和持久化工作。而在2中这两个工作合并在一个pas文件里了。
2 WindowsForms Application for .NET
unit WinForm;
interface
uses
System.Drawing, System.Collections, System.ComponentModel,
System.Windows.Forms, System.Data;
type
TWinForm = class(System.Windows.Forms.Form)
{$REGION 'Designer Managed Code'}
strict private
/// <summary>
/// Required designer variable.
/// </summary>
Components: System.ComponentModel.Container;
TextBox1: System.Windows.Forms.TextBox;
Button1: System.Windows.Forms.Button;
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
procedure InitializeComponent;
procedure Button1_Click(sender: System.Object; e: System.EventArgs);
{$ENDREGION}
strict protected
/// <summary>
/// Clean up any resources being used.
/// </summary>
procedure Dispose(Disposing: Boolean); override;
private
{ Private Declarations }
public
constructor Create;
end;
[assembly: RuntimeRequiredAttribute(TypeOf(TWinForm))]
implementation
{$AUTOBOX ON}
{$REGION 'Windows Form Designer generated code'}
/// <summary>
/// Required method for Designer support -- do not modify
/// the contents of this method with the code editor.
/// </summary>
procedure TWinForm.InitializeComponent;
begin
Self.TextBox1 := System.Windows.Forms.TextBox.Create;
Self.Button1 := System.Windows.Forms.Button.Create;
Self.SuspendLayout;
//
// TextBox1
//
Self.TextBox1.Location := System.Drawing.Point.Create(72, 40);
Self.TextBox1.Name := 'TextBox1';
Self.TextBox1.Size := System.Drawing.Size.Create(152, 21);
Self.TextBox1.TabIndex := 0;
Self.TextBox1.Text := '';
//
// Button1
//
Self.Button1.Location := System.Drawing.Point.Create(80, 160);
Self.Button1.Name := 'Button1';
Self.Button1.Size := System.Drawing.Size.Create(136, 32);
Self.Button1.TabIndex := 1;
Self.Button1.Text := 'Button1';
Include(Self.Button1.Click, Self.Button1_Click);
//
// TWinForm
//
Self.AutoScaleBaseSize := System.Drawing.Size.Create(6, 14);
Self.ClientSize := System.Drawing.Size.Create(292, 273);
Self.Controls.Add(Self.Button1);
Self.Controls.Add(Self.TextBox1);
Self.Name := 'TWinForm';
Self.Text := 'WinForm';
Self.ResumeLayout(False);
end;
{$ENDREGION}
procedure TWinForm.Dispose(Disposing: Boolean);
begin
if Disposing then
begin
if Components <> nil then
Components.Dispose();
end;
inherited Dispose(Disposing);
end;
constructor TWinForm.Create;
begin
inherited Create;
//
// Required for Windows Form Designer support
//
InitializeComponent;
//
// TODO: Add any constructor code after InitializeComponent call
//
end;
procedure TWinForm.Button1_Click(sender: System.Object; e: System.EventArgs);
begin
TextBox1.Text:='Hello World!';
end;
end.
3 VCLForms Application for Win32.
它的代码和1完全一样。
最后是用c#写的helloworld.它只有.net一种方式,因为它诞生在.net时代。
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace Project1
{
/// <summary>
/// Summary description for WinForm.
/// </summary>
public class WinForm : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
public WinForm()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(72, 88);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(120, 21);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "textBox1";
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
//
// button1
//
this.button1.Location = new System.Drawing.Point(88, 176);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// WinForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Name = "WinForm";
this.Text = "WinForm";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new WinForm());
}
private void button1_Click(object sender, System.EventArgs e)
{
textBox1.Text="Hello World.";
}
private void textBox1_TextChanged(object sender, System.EventArgs e)
{
}
}
}