今天做了一个小软件,总结了一些经验与大家共享。源代码过一段时间我可能公布,希望与大家共同学习。
1、用sender的方式增强代码的健壮性
procedure TMainfrm.CBAutoRunClick(Sender: TObject);
Const
SIGNINREGISTRY = 'WebSuction';
begin
if (Sender as TCheckBox).Checked then //用sender as...的方式可适应
//性更强
AddToAutoRun(Application.ExeName,SIGNINREGISTRY)
else DelAutoRun(SIGNINREGISTRY);
end;
即使Checkbox1改了名字也不怕
又如:
procedure TMainfrm.N1Click(Sender: TObject);
begin
if (Sender as TMenuItem).Caption = '暂停(&S)' then
begin
(Sender as TMenuItem).Caption := '开始(&R)';
FWebPageSaver.Pause;
end
else
begin
(Sender as TMenuItem).Caption := '暂停(&S)';
FWebPageSaver.ReStart;
end;
end;2、不要出现魔法数
function ExtractFileNameFromText( AText : string): string;
Const
MAXLENGTH = 250;//Max length of filename
var
LTextLength, I : integer;
LString : string;
begin
LString := AText;
LTextLength := Length(LString);
for I := 0 to LTextLength-1 do
begin
if IsInvalidChar(LString[I]) then
LString[I] := 'n';//Change the Invalid char with 'n'
end;//在返回语句与前面的代码之间用空行隔开
result := LeftStr(LString,MAXLENGTH);//让人一看就知道MAXLENGTH是什么意思,比直接写250好
end;3、错落有致
procedure TMainfrm.WMHotKey(var Msg : TWMHotKey);
begin
if (Msg.HotKey = FHotKeyId) and (ClipBoard.HasFormat(CF_TEXT)) and
(not ClipBoard.HasFormat(CF_PICTURE)) then//不要超过一行能容纳的字数
FWebPageSaver.NewTextFile(ClipBoard.AsText);
end;4、不要直接使用Tform2单元的全局Form2变量,那样就破坏了封装性
procedure TMainfrm.SBNextClick(Sender: TObject);
var
LSelectedIndex : integer;
FormDisplay : Tform2;
begin
LSelectedIndex := LBWebPage.ItemIndex;
if LSelectedIndex <> -1 then
begin
FormDisplay := Tform2.Create(self);
FormDisplay.SetContent(FWebCracker.GetWebText(LSelectedIndex));
FormDisplay.Show;
end;
end;
在TForm2中定义 SetContent方法
procedure TWebCrackfrm.SetContent(AText:string);
begin
Memo.Clear;
Memo.Lines.Add(AText);
end;5 用面向对象的方法使用delphi。
这是我做这个软件最大的体会,以前我用面向过程的方法做过这个软件,代码思路特别乱,现在用了OO的方法就是不一样
这个一句两句可说不清楚,等我公布源码后大家自己看吧,也希望大家多提建议!
1、用sender的方式增强代码的健壮性
procedure TMainfrm.CBAutoRunClick(Sender: TObject);
Const
SIGNINREGISTRY = 'WebSuction';
begin
if (Sender as TCheckBox).Checked then //用sender as...的方式可适应
//性更强
AddToAutoRun(Application.ExeName,SIGNINREGISTRY)
else DelAutoRun(SIGNINREGISTRY);
end;
即使Checkbox1改了名字也不怕
又如:
procedure TMainfrm.N1Click(Sender: TObject);
begin
if (Sender as TMenuItem).Caption = '暂停(&S)' then
begin
(Sender as TMenuItem).Caption := '开始(&R)';
FWebPageSaver.Pause;
end
else
begin
(Sender as TMenuItem).Caption := '暂停(&S)';
FWebPageSaver.ReStart;
end;
end;2、不要出现魔法数
function ExtractFileNameFromText( AText : string): string;
Const
MAXLENGTH = 250;//Max length of filename
var
LTextLength, I : integer;
LString : string;
begin
LString := AText;
LTextLength := Length(LString);
for I := 0 to LTextLength-1 do
begin
if IsInvalidChar(LString[I]) then
LString[I] := 'n';//Change the Invalid char with 'n'
end;//在返回语句与前面的代码之间用空行隔开
result := LeftStr(LString,MAXLENGTH);//让人一看就知道MAXLENGTH是什么意思,比直接写250好
end;3、错落有致
procedure TMainfrm.WMHotKey(var Msg : TWMHotKey);
begin
if (Msg.HotKey = FHotKeyId) and (ClipBoard.HasFormat(CF_TEXT)) and
(not ClipBoard.HasFormat(CF_PICTURE)) then//不要超过一行能容纳的字数
FWebPageSaver.NewTextFile(ClipBoard.AsText);
end;4、不要直接使用Tform2单元的全局Form2变量,那样就破坏了封装性
procedure TMainfrm.SBNextClick(Sender: TObject);
var
LSelectedIndex : integer;
FormDisplay : Tform2;
begin
LSelectedIndex := LBWebPage.ItemIndex;
if LSelectedIndex <> -1 then
begin
FormDisplay := Tform2.Create(self);
FormDisplay.SetContent(FWebCracker.GetWebText(LSelectedIndex));
FormDisplay.Show;
end;
end;
在TForm2中定义 SetContent方法
procedure TWebCrackfrm.SetContent(AText:string);
begin
Memo.Clear;
Memo.Lines.Add(AText);
end;5 用面向对象的方法使用delphi。
这是我做这个软件最大的体会,以前我用面向过程的方法做过这个软件,代码思路特别乱,现在用了OO的方法就是不一样
这个一句两句可说不清楚,等我公布源码后大家自己看吧,也希望大家多提建议!
解决方案 »
- 急求下面试题的答案 请教高手
- 如何使【某个字符串与数据表中的值进行比较】?
- 第一次亲密接触delphi
- 请问,怎样才能让打开的程序,运行时不在任务栏上显示出来? 这时程序能正常在桌面上运行1
- 在线等你来拿。100分,先来先拿!
- 如何设置这样的时间列,存储为200020209但显示给用户的是2002/02/09
- 急,急,怎样做一个类似windows的开始菜单
- 报表问题。实现把数据逐行累计放于后面一个字段。怎么实现。懂的朋友快来啊!!
- 获取对应字体的大小列表和设置字体大小
- 朱小军请进!
- 这几天一直在看delphi的书,有一个问题向大家请教?
- 怎样得到在TAB 控件中用户是选择那一个TAB page页,急!!!
[email protected]
你怎么访问你创建的TForm2呢?
FormDisplay := Tform2.Create(self);
FormDisplay.SetContent(FWebCracker.GetWebText(LSelectedIndex));
FormDisplay.Show;
Delphi编码原则中就有这些.比如多用With等.
[email protected]
[email protected]
[email protected]
http://online.sdu.edu.cn/home/sduhome/lincosoft/websuctioncode.rar
或http://www.efile.com.cn/?liumazi
[email protected]
因为是用SHOW的,所以一直执行。也许楼主是打算用完让用户CLOSE的。
也就是要求98/2000/XP下都可以,问下 VB里GETCURSORPOS这个API在XP下可以用吗?有什么区别?
[email protected]
[email protected]