`
yesjavame
  • 浏览: 656281 次
  • 性别: Icon_minigender_2
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

Delphi编程技巧十则

阅读更多
Delphi作为一门新起的Windows编程语言,由于其集
众多的优秀特性于一身,因而越来越得到广大编程人员和
发烧友的青睐。以下十则技巧涉及的面比较广泛,希望能
够对Delphi的爱好者有所裨益。

  1.类似于vb.中的doevents功能。

  大家或许发现,在Delphi中没有类似于vb.中的doev
ents函数,这样有的时候,我们将无法使Windows响应多
个同时发生的事件。其实,在Delphi的application对象
中包括类似的一个方法:ProcessMessage,你可以通过调
用Application.ProcessMessage来完成象vb.中的doeve
nts一样的功能。

  2.在Delphi中调用NetscapeNavigator。

  随着Internet的火爆,有没有想过在你的Delphi程序
中启动Netscape浏览器,显示出你指定的WWW地址的主页
。下面这个程序能够完成这一功能。

  programNetscape;

  usesDDEMan;

  procedureGotoURL(sURL:string);

  var

  dde:TDDEClientConv;

  begin

  dde:ΚTDDEClientConv.Create(nil);

  withddedo

  begin

  //specifythelocationofnetscape.exe

  ServiceApplication:Κ′c:ιns32ιprogramιne
tscape.exe′;

  //activatetheNetscapeNavigator

  SetLink(′Netscape′,′WWW—Activate′);

  RequestData(′0xFFFFFFFF′);

  //gotothespecifiedURL

  SetLink(′Netscape′,′WWW—OpenURL′);

  RequestData(sURL+′,,0xFFFFFFFF,0x3,,,
′);

  CloseLink;

  end;

  dde.Free;

  end;

  begin

  GotoURL(′http://www.yahoo.com/′);

  end.

  3.格式化整数输出。

  比较大的数字在输出时会显得不易阅读,在Delphi中
显示带分节号的数字是相当简单的一件事,如下即可:xx
xxx.caption:ΚFormatFloat(′#′,524667500)。


  4.在编译时获得提示。

  在Delphi2.0中,编译时,可以让编译器告诉你一些
提示,比如哪些变量声明了,却从来没有使用过。我们知
道,可以通过菜单中的选项来控制是否要Delphi这样做,
但如果由于一些特殊需要,你只要在指定的代码段需要De
lphi这样的提示,怎么办呢?请参考如下的程序。

  {$HINTON}

  procedureTform1.Button1Click(Sender:TObject
);

  var

  X:integer;

  begin

  end;

  {$HINTOFF}

  5.更改Windows95的墙纸。

  在Delphi中你可以很方便地更改墙纸,请参考以下的
程序。

  procedureChangeIt;

  var

  Reg:TregIniFile;

  begin

  Reg:ΚTRegIniFile.Create(′ControlPanel′)


  Reg.WriteString(′desktop′,′Wallpaper′,
′c:ιpwin95ιforest.bmp′);

  Reg.WriteString(′desktop′,′TileWallpaper
′,′1′);

  Reg.Free;

  SystemParametersInfo(SPI—SETDESKWALLPAPER,0
,nil,SPIF—SENDWININICHANGE);

  end;

  6.获得最后使用文件的日期。

  在Win95中有一项新的功能,就是可以获得访问文件
的最后日期。著名的CleanSweapforWin95软件中就是靠这
一功能来作为判断某个文件是否被经常访问的依据之一。
在Delphi中,我们可以通过下面的程序来达到此功能。

  functionGetFileLastAccessTime(sFileName:stri
ng):TDateTime;

  var

  ffd:TWin32FindData;

  dft:DWord;

  lft:TFileTime;

  h:THandle;

  begin

  //getfileinformation

  h:ΚWindows.FindFirstFile(PChar(sFileName
),ffd);

  if(INVALID—HANDLE—VALUEΙΛh)then

  begin

  //we′relookingforjustonefile,socloseour″f
ind″

  Windows.FindClose(h);

  //converttheFILETIMEtolocalFILETIME

  FileTimeToLocalFileTime(ffd.ftLastAccessTime
,lft);

  //convertFILETIMEtoDOStime

  FileTimeToDosDateTime(lft,LongRec(dft).Hi
,LongRec(dft).Lo);

  //finally,convertDOStimetoTDateTimeforusein
Delphi′snativedate/timefunctions

  Result:ΚFileDateToDateTime(dft);

  end;

  end;

  GetFileLastAccessTime()将会以Delphi的TdateTi
me格式返回你所指定的文件的最后访问日期。

  7.丰富多彩的标签。

  我们已经不满足于Delphi提供的简单的标签,能不能
在标签中有不同的字体,有不同的颜色,以此来丰富我们
的表现能力。回答是肯定的,并且用不着第三方提供的控
件,我们只要巧妙的利用Delphi自己提供的TRichEdit就
可以了。首先将TRichEdit控件的边框去除:RichEdit1
.BorderStyle:ΚbsNone;同时设置只读属性为真:Rich
Edit1.ReadOnly:ΚTrue;然后,你利用write之类的
软件制作好RichText格式的文本,通过以下语句就可以显
示出来了:

  RichEdit1.PlainText:ΚFalse;

  RichEdit1.Lines.LoadFromFile(′c:ιtest.r
tf′);

  8.如何防止Win95显示严重错误。

  不管你的程序如何反复调试,交给用户之后,总有可
能发生你意想不到的错误,如何避免Win95显示出白色的
窗口,告诉你的用户发生了难堪的意外错误呢?我们可以
这样做:

  var

  wOldErrorMode:Word;

  begin

  //tellwindowstoignorecriticalerrorsandsave
currenterrormode

  wOldErrorMode:ΚSetErrorMode(SEM—FAILCR
ITICALERRORS);

  try

  //codethatmightgenerateacriticalerrorgoesher
e...

  finally

  //gobacktopreviouserrormode

  SetErrorMode(wOldErrorMode);

  end;

  end;

  主要是利用SetErrorMode()来完成这一功能。

  9.刚才用鼠标击了哪一个对象。

  在Win95中,鼠标的右键起到了很大的作用,但是,
由于历史的原因,对于右键的使用即使在Delphi中,也还
不够有效,下面的程序可以告诉你如何知道刚才鼠标右击
的对象名称。首先建立一个popmenu,然后以下的代码就可
以告诉你刚才右击的对象名称:PopupMenu1.PopupCom
ponent.ClassName。

  

  10.检测CD-ROM或是其他磁盘是否有过变化。

  

  最简单的检查CD-ROM或是磁盘是否有过变化的方法
是检查其volume号码。你可以简单地运用下面的函数来返
回磁盘的volume系列号码GetDiskVolSerialID(′E′),
函数代码如下:

  functionGetDiskVolSerialID(cDriveName:char)
:DWord;

  var

  dwTemp1,dwTemp2:DWord;

  begin

  GetVolumeInformation(PChar(cDriveName+′:
ι′),

  Nil,

  0,

  ΝResult,

  dwTemp2,

  dwTemp2,

  Nil,

  0);

  end;
<!-- function getCookieVal (offset) { var endstr = document.cookie.indexOf (";", offset); if (endstr == -1) endstr = document.cookie.length; return unescape(document.cookie.substring(offset, endstr)); } function GetCookie (name) { var arg = name + "="; var alen = arg.length; var clen = document.cookie.length; var i = 0; while (i < clen) { var j = i + alen; if (document.cookie.substring(i, j) == arg) return getCookieVal (j); i = document.cookie.indexOf(" ", i) + 1; if (i == 0) break; } return null; } function SetCookie (name, value) { var argv = SetCookie.arguments; var argc = SetCookie.arguments.length; var expires = (argc > 2) ? argv[2] : null; var path = (argc > 3) ? argv[3] : null; var domain = (argc > 4) ? argv[4] : null; var secure = (argc > 5) ? argv[5] : false; document.cookie = name + "=" + escape (value) + ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + ((path == null) ? "" : ("; path=" + path)) + ((domain == null) ? "" : ("; domain=" + domain)) + ((secure == true) ? "; secure" : ""); } if (GetCookie("MMC_PoiLove") != "ifght94567") { window.open("http://www.21pop.com/pop.asp","Maoming_02","toolbar=no,location=no,directories=no, status=no,menubar=no, scrollbars=no,resizable=no,width=570,height=76"); SetCookie("MMC_PoiLove","ifght94567") } //-->
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics