B站: https://www.bilibili.com/video/BV12s411c7ct/
相关论坛:https://fishc.com.cn/forum-120-1.html
P9:结构化程序设计
- system里timer定时器组件,inteval属性单位为毫秒,1000毫秒为一秒
- 隐藏窗体边框: borderStyle 设置为 bsNone
- autosize 属性设置为true,控件自动调整大小
- 不等于: <>
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls;
type
TForm1 = class(TForm)
Timer1: TTimer;
Label1: TLabel;
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Timer1Timer(Sender: TObject);
begin
Label1.Font.Size :=random(40);
Form1.Left:=random(500);
Form1.Top:=random(600);
end;
end.
P10:结构化程序设计 if else
如果是单语句,if结构如下,复合语句用 begin … end;
if () then
s1 //无分号
else
s2; //有分号
猜数字示例,有调用windows API:
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)
Label1: TLabel;
Edit1: TEdit;
Button1: TButton;
Label2: TLabel;
Label3: TLabel;
Button2: TButton;
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
var
randomNum: Integer;
guessNum: Integer;
procedure TForm1.Button1Click(Sender: TObject);
begin
guessNum := strtoint(Edit1.Text);
if guessNum = randomNum then
// Delphi封装的消息窗口
ShowMessage('bingo!')
else if guessNum > randomNum then
ShowMessage('你猜大了!')
else
begin
// ShowMessage('你猜小了!')
// windows 风格消息框
// 可以在delphi帮助中搜索 MessageBox
MessageBox(handle, '消息内容:你猜小了', '标题:提醒', MB_OK);
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
// 生成随机数
randomNum := random(101); // 0<=x<x
Label3.Caption := inttostr(randomNum);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
// 调用windows API 设置光标
// ES_CENTER ,中间
SetWindowLong(Edit1.handle, GWL_STYLE, GetWindowLong(Edit1.handle, GWL_STYLE)
or ES_CENTER);
Edit1.Invalidate; // 该函数的作用是使整个窗口客户区无效,需要重新绘制
// 生成随机数
randomNum := random(101); // 0<=x<x
Label3.Caption := inttostr(randomNum);
end;
end.
P11:结构化程序设计 多分支case语句
case (表达式) of
v1:s1;
v2:s2;
v3,v4:s4;
v5:
begin
end;
...
Vn:sn;
ELSE // 无匹配时执行,可省略
s;
end;
示例:
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
Timer1: TTimer;
procedure Timer1Timer(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
Form1.AutoSize := True;
Label1.AutoSize := True;
Form1.BorderStyle := bsNone;
Label1.Transparent := False; // 必须改为false,否则背景为父容器颜色
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
Label1.Font.Size := random(50);
Form1.Left := random(500);
Form1.Top := random(500);
// 0-7 共8种情况
case (random(8)) of
0:
Label1.Color := clBlack;
1:
Label1.Color := clYellow;
2:
Label1.Color := clRed;
3:
Label1.Color := clBlue;
4:
Label1.Color := clGreen;
5:
Label1.Color := clFuchsia;
6:
Label1.Color := clLtGray;
7:
Label1.Color := clOlive;
end;
// 0-7 共8种情况
case (random(8)) of
0:
Label1.Font.Color := clBlack;
1:
Label1.Font.Color := clYellow;
2:
Label1.Font.Color := clRed;
3:
Label1.Font.Color := clBlue;
4:
Label1.Font.Color := clGreen;
5:
Label1.Font.Color := clFuchsia;
6:
Label1.Font.Color := clLtGray;
7:
Label1.Font.Color := clOlive;
end;
end;
end.
P12 小结
对话框示例:
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;
Label1: TLabel;
Button1: TButton;
CheckBox1: TCheckBox;
procedure FormActivate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure CheckBox1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
var
numCanTry: Integer = 3;
const
password: string = 'password';
procedure TForm1.Button1Click(Sender: TObject);
begin
// 密码正确
if Edit1.Text = password then
messagebox(handle, 'correct!', '恭喜', MB_OK)
else
begin
numCanTry := numCanTry - 1;
if numCanTry <> 0 then
begin
messagebox(handle, 'wrong!', '提醒', MB_OK);
Label1.Caption := '请输入密码,你还有' + inttostr(numCanTry) + '次机会';
edit1.Text :='';
end // 注意这里没有分号才能和下面的else连起来
else
begin
messagebox(handle, 'wrong! you have no change!', '提醒', MB_OK);
close;
// 密码错误
end;
end;
end;
procedure TForm1.CheckBox1Click(Sender: TObject);
begin
if CheckBox1.checked = true then
Edit1.PasswordChar := #0
else
Edit1.PasswordChar := '*';
end;
procedure TForm1.FormActivate(Sender: TObject);
begin
// 对话框窗口
Form1.BorderStyle := bsDialog;
Label1.Caption := '请输入密码,你还有' + inttostr(numCanTry) + '次机会';
// edit 内容居中 , windows api 方法如下。也可以直接设置属性
// SetWindowLong(Edit1.Handle, GWL_STYLE, GetWindowLong(Edit1.Handle, GWL_STYLE) or ES_CENTER);
// Edit1.Invalidate; // 该函数的作用是使整个窗口客户区无效,需要重新绘制
Edit1.Alignment := taCenter;
Edit1.Text := '';
Edit1.PasswordChar := '*';
end;
end.
正文完