2018【Delphi7】《零基础入门学习Delphi》(from b站)(1)

B站: https://www.bilibili.com/video/BV12s411c7ct/

相关论坛:https://fishc.com.cn/forum-120-1.html

P1: 第一个程序

添加组件的三种方法

  • 单击组件,然后在form上拉矩形画出来
  • 双击组件添加到窗口的中心位置(这个应该是最好用的,因为组件是默认大小)
  • 按shift键后点击组件,然后可以在form上连续点击添加同一类组件

实现的功能:

  • 显示,隐藏欢迎词:点击显示按钮显示欢迎词,显示按钮变为隐藏按钮,点击隐藏按钮隐藏欢迎词
  • 点击关闭按钮关闭程序
  • 窗口居中: poDesktopCenter
  • hint提示
unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Button_Show: TButton;
    Button_Close: TButton;
    Label_Welcome: TLabel;
    Edit_Welcome: TEdit;
    procedure Button_ShowClick(Sender: TObject);
    procedure Button_CloseClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button_CloseClick(Sender: TObject);
begin
  Application.Terminate;
end;

procedure TForm1.Button_ShowClick(Sender: TObject);
begin

  // 返回两个双字节字符串是否相同
  if WideSameText(Button_Show.Caption, '显示') then
  begin
    Label_Welcome.Caption := '欢迎学习Delphi!';
    Button_Show.Caption := '隐藏';
    Button_Show.Hint := '隐藏文本';
    Edit_Welcome.Text := '欢迎学习Delphi!';
  end
  else
  begin
    Label_Welcome.Caption := '';
    Edit_Welcome.Text := '';
    Button_Show.Caption := '显示';
    Button_Show.Hint := '显示文本';
  end;

end;

end.

格式化代码: CTRL + D

P2:基本数据类型和表达式1

  • 整数
  • 浮点数
  • 字符:单个字符
  • 字符串: 字符和字符串都是用单引号引用的
  • 布尔:true,false

可以直接参考Delphi11.3 帮助文件。

平台无关的整数类型:

Type Range Format Alias
ShortInt
SmallInt
FixedInt
Integer
Int64` Signed 64-bit
Byte
Word
FixedUInt
Cardinal
UInt64` Unsigned 64-bit

实数类型:

Type Platform Approximate Positive Range Significant decimal digits Size in bytes
Real48 all 2.94e-39 .. 1.70e+38 11-12 6
Single all 1.18e-38 .. 3.40e+38 7-8 4
Double all 2.23e-308 .. 1.79e+308 15-16 8
Real all 2.23e-308 .. 1.79e+308 15-16 8
Extended 32-bit Intel Windows 3.37e-4932 .. 1.18e+4932 10-20 10
64-bit Intel Linux 64-bit Intel macOS 3.37e-4932 .. 1.18e+4932 10-20 16
64-bit Intel Windows ARM platforms (32-bit Android, 64-bit Android, 64-bit iOS, 64-bit macOS) 2.23e-308 .. 1.79e+308 15-16 8
Comp` 10-20 8
Currency` 10-20 8

P3:基本数据类型和表达式2

变量声明:

  • interface中定义的变量,其他unit可以使用该变量
  • implementation 中定义的变量,本Unit内都可以使用
  • 过程和函数中定义的变量,仅在该过程或函数中有效

示例,计算输入的四位整数的各个位数:

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Edit_Number: TEdit;
    Button1: TButton;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    procedure FormActivate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  theInt: Integer;
Const // 定义常量,用 = 不用 :=   ,大写
  CONST_INT=100;
begin
  theInt := StrToInt(Edit_Number.Text);
  Label1.Caption := Concat('个位:', IntToStr(theInt mod 10));

  // div 是整数除法
  // mod 是求余数
  theInt := theInt div 10;
  Label2.Caption := Concat('十位:', IntToStr(theInt mod 10));

  theInt := theInt div 10;
  Label3.Caption := Concat('百位:', IntToStr(theInt mod 10));

  theInt := theInt div 10;
  Label4.Caption := Concat('千位:', IntToStr(theInt mod 10));
end;

procedure TForm1.FormActivate(Sender: TObject);
begin
  // 标签初始化 : 依次为个,十,百,千位
  Label1.Caption := '';
  Label2.Caption := '';
  Label3.Caption := '';
  Label4.Caption := '';
end;

end.

P4:基本数据类型和表达式3

算术运算符:

  • +,-,* 加减乘
  • / 除法运算符的结果总是实数(浮点数)
  • div: 整数除法,结果为整数,只能用于整数相除
  • mod: 求余数,用于整数

字符串运算符: + ,用于字符串连接

加减乘除计算器示例:

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    procedure FormActivate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

var
  num1, num2, result: double;

procedure TForm1.Button1Click(Sender: TObject);
begin
  // 加法
  Label1.Caption := '+';
  num1 := StrToFloat(Edit1.Text);
  num2 := StrToFloat(Edit2.Text);
  result := num1 + num2;
  Edit3.Text := FloatToStr(result);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  Label1.Caption := '-';
  num1 := StrToFloat(Edit1.Text);
  num2 := StrToFloat(Edit2.Text);
  result := num1 - num2;
  Edit3.Text := FloatToStr(result);
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  Label1.Caption := '*';
  num1 := StrToFloat(Edit1.Text);
  num2 := StrToFloat(Edit2.Text);
  result := num1 * num2;
  Edit3.Text := FloatToStr(result);
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
  Label1.Caption := '/';
  num1 := StrToFloat(Edit1.Text);
  num2 := StrToFloat(Edit2.Text);
  result := num1 / num2;
  Edit3.Text := FloatToStr(result);
end;

procedure TForm1.FormActivate(Sender: TObject);
begin
  Edit1.Text := '';
  Edit2.Text := '';
  Edit3.Text := '';
  Label1.Caption := '';
  Label2.Alignment := taCenter; // 文字居中,属性值不加引号
  Label2.Caption := '   =';
end;

end.

P5:基本数据类型和表达式4,逻辑(按位)运算符

位运算符必须是整数:

Operator Operation Operand Types Result Type Example
not 按位取反 integer integer not X
and 按位与 integer integer X and Y
or 按位或 integer integer X or Y
xor 按位异或 integer integer X xor Y
shl 向左移位 integer integer X shl 2
shr 向右移位 integer integer Y shr I
  • 与运算经常用于把某些位设置为0:只要这些位为0即可
  • 或运算经常用于把某些位设置为1:只要这些位为1即可
  • 异或运算经常用于把某些位设置取反:只要这些位为1即可

示例中,32位整数Integer表示为二进制字符串的工作,由两个函数完成:

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    procedure FormActivate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Edit1Exit(Sender: TObject);
    procedure Edit2Exit(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

var
  num1, num2, result, result2, tempInteger: Integer;
  // tempStr: string;

function get_bit_of_integer(i: Integer; position_from_right: Integer): Integer;
// 获取整数 i 的二进制形式从右数第 position_from_right   位的值
// 结果为0或1
//
var
  tempInteger: Integer;
begin
  // 如果是第1位,不需要移动
  // 如果是第2位,需要移动1位
  tempInteger := i shr (position_from_right - 1); // 注意加括号
  get_bit_of_integer := tempInteger and 1; // 返回值,用函数名返回
end;

function get_bit_str_of_integer(i: Integer): String;
// 获取32位整数的二进制字符串表示
var
  tempStr: String;
  loopTag: Integer;
begin
  tempStr := '';
  for loopTag := 1 to 32 do
  begin
    tempStr := IntToStr(get_bit_of_integer(i, loopTag)) + tempStr;
  end;
  get_bit_str_of_integer := tempStr;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  // Not  第一个数
  result := not num1;
  Label5.Caption := 'Not ' + IntToStr(num1);
  Label6.Caption := get_bit_str_of_integer(result);

end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  // and
  result := num1 and num2;
  Label5.Caption := IntToStr(num1) + ' and ' + IntToStr(num2);
  Label6.Caption := get_bit_str_of_integer(result);

end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  // or
  result := num1 or num2;
  Label5.Caption := IntToStr(num1) + ' or ' + IntToStr(num2);
  Label6.Caption := get_bit_str_of_integer(result);
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
  // xor
  result := num1 xor num2;
  Label5.Caption := IntToStr(num1) + ' xor ' + IntToStr(num2);
  Label6.Caption := get_bit_str_of_integer(result);
end;

procedure TForm1.Button5Click(Sender: TObject);
begin
    // num1 shr 2bit
  result := num1 shr 2;
  Label5.Caption := IntToStr(num1) + ' ShR 2';
  Label6.Caption := get_bit_str_of_integer(result);
end;

procedure TForm1.Button6Click(Sender: TObject);
begin
  // num1 shl 2bit
  result := num1 shl 2;
  Label5.Caption := IntToStr(num1) + ' ShL 2';
  Label6.Caption := get_bit_str_of_integer(result);
end;

procedure TForm1.Edit1Exit(Sender: TObject);
begin
  // 32位整数
  num1 := StrToInt(Edit1.Text);
  Label1.Caption := '第一个数的二进制表示:';
  Label2.Caption := get_bit_str_of_integer(num1);
end;

procedure TForm1.Edit2Exit(Sender: TObject);
begin
  num2 := StrToInt(Edit2.Text);
  Label3.Caption := '第二个数的二进制表示:';
  Label4.Caption := get_bit_str_of_integer(num2);
end;

procedure TForm1.FormActivate(Sender: TObject);
begin
  Edit1.Text := '';
  Edit2.Text := '';
  Label1.Caption := '';
  Label2.Caption := '';
  Label3.Caption := '';
  Label4.Caption := '';
  Label5.Caption := '';
  Label6.Caption := '';
end;

end.

P6:基本数据类型和表达式5,赋值语句

赋值语句在 Delphi中用的是 :=, 而不是=

extended的精度:

  • On Intel 32-bit Windows systems, the size of System.Extended is 10 bytes.
  • On Intel 64-bit Windows systems the System.Extended is only 8 bytes. This difference can adversely affect numeric precision in floating-point operations.

实际操作来看,在windows下选择目标平台为 Windows 32-bit 时结果为10,选择目标平台为 Windows 64-bit 时结果为8,这里指的是编译的应用程序位数,并非windows操作系统位数。

program Project1;

{$APPTYPE CONSOLE}
{$R *.res}

uses
  System.SysUtils;

begin
  try
    { TODO -oUser -cConsole Main : Insert code here }
    Writeln('当前Extended类型的长度:');
    Writeln(IntToStr(SizeOf(Extended)));

    readln; //等待输入,用户输入字符后按回车将会退出(如果没有这个函数,上面那句在屏幕输出字符后一闪就退出了)end.
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;

end.
  • abs(x): 绝对值
  • Trunc(x): 返回浮点数的整数部分,类型为 Int64
  • Round(x)返回浮点数的整数部分,小数部分四舍五入,类型为 Int64:
  • Int(x)返回浮点数的整数部分,类型为 Extended
  • Frac(x)返回浮点数的小数部分,类型为 Extended
  • sqr(x):平方$x^2$,类型为 Extended
  • sqrt(x):算术平方根$x^{1/2}$,类型为 Extended
  • 三角函数;sin(x),cos(x)
  • 指数函数Experience(x) : $e^x$
  • 自然对数函数 LN(x)
  • random(x), 返回一个$0≤Y<x$ 的随机整数Y
  • random(),返回一个$0≤Y<1$ 的随机小数Y
  • pos(s1,s2) 返回s1在s2中首次出现的位置,没找到返回0, 位置是从1开始的

P7:基本数据类型和表达式6,字符串处理函数与过程

  • lowerCase(),upperCase: 大小写转换
  • CompareStr():Integer, 根据ASCII码值比较字符串大小, 如果前者大于后者,返回值大于零; 若两个字符串相等,返回0,区分大小写
  • CompareText():Integer, 同上,但不分区大小写
  • length(x):字符串长度
  • appendStr(s1,s2),合并字符串 比 s1:=s1+s2执行效率高
  • copy(s,m,n) 截取字符串从s中截取第m个字符开始,长度为n的字符串
  • delete(s,m,n) : 这是个过程不是函数,并且s只能是个变量
  • insert(s1,s2,k):将s1插入s2的第k个字符处,实际上是插到第k个字符的前面,s1,s2不能为常量
program Project1;

{$APPTYPE CONSOLE}
{$R *.res}

uses
  System.SysUtils;

begin
  var
    temp, temp2: string;

  try
    { TODO -oUser -cConsole Main : Insert code here }
    // 字符串中用 '' 转义为单引号
    // 获取ASCII值使用函数ord
    writeln('a对应整数:' + inttostr(ord('a')));
    writeln('A对应整数:' + inttostr(ord('A')));

    temp := 'CompareStr(''abc'',''Abc''):' + inttostr(CompareStr('abc', 'Abc'));
    writeln(temp);

    temp := 'CompareText(''abc'',''Abc''):' +
      inttostr(CompareText('abc', 'Abc'));
    writeln(temp);

    temp := 'Length(''abc''):' + inttostr(Length('abc'));
    writeln(temp);

    temp := 'Length(''你好!''):' + inttostr(Length('你好!'));
    writeln(temp);

    temp := 'cd 在abcd字符串中的位置:' + inttostr(pos('cd', 'abcd'));
    writeln(temp);

    temp := 'copy(''HelloWorld!'',6,6) 的结果:' + copy('HelloWorld!', 6, 6);
    writeln(temp);

    temp := 'copy(''你好世界!'',3,3) 的结果:' + copy('你好世界!', 3, 3);
    writeln(temp);

    temp := 'delete(''你好世界!'',3,3) 的结果:';
    writeln(temp);
    temp := '你好世界';
    delete(temp, 3, 3);
    writeln(temp);

    temp := 'Insert(''你好世界!'',''1234'',3) 的结果:';
    writeln(temp);
    temp := '你好世界!';
    temp2 := '1234';
    Insert(temp, temp2, 3);
    writeln(temp2);

    readln;
  except
    on E: Exception do
      writeln(E.ClassName, ': ', E.Message);
  end;

end.

输出:

a对应整数:97
A对应整数:65
CompareStr('abc','Abc'):32
CompareText('abc','Abc'):0
Length('abc'):3
Length('你好!'):3
cd 在abcd字符串中的位置:3
copy('HelloWorld!',6,6) 的结果:World!
copy('你好世界!',3,3) 的结果:世界!
delete('你好世界!',3,3) 的结果:
你好
Insert('你好世界!','1234',3) 的结果:
12你好世界!34

P8:基本数据类型和表达式7

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  input: string;
  result: Integer;
begin
  input := Edit1.Text;
  result := comparestr(input, 'password');
  if (result = 0) then
    Edit1.Text := 'correct!'
  else
    Edit1.Text := 'incorrect!'
end;

end.

正文完