⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 delphi1.html

📁 对于学习很有帮助
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="Author" content="SeaWave">
<meta name="GENERATOR" content="Microsoft FrontPage 3.0">
<meta name="KeyWords" content="Delphi,Programming">
<title>Delphi实用技巧(一)</title>
</head>

<body bgcolor="#628394" text="#FFFFFF" background="../../images/background.gif" link="#FFFF00" vlink="#FFFF00">

<p><a name="topofpage"></a></p>
<div align="center"><center>

<table border="1" cellpadding="0" cellspacing="0">
  <tr>
    <td><p align="center"><font face="楷体_GB2312" size="6" color="#FFFFFF"><strong><em>Delphi实用技巧(一)</em></strong></font></td>
  </tr>
</table>
</center></div>

<p align="center"><a href="../programming.html"><font face="楷体_GB2312" size="4">返回“编程交流”</font></a></p>

<p><img src="delphilogo.gif" align="right" border="0" WIDTH="93" HEIGHT="128"> 

<ul type="square">
  <li><a href="delphi1.html#CloseMDIChild"><font face="楷体_GB2312" size="4">正确关闭MDI子窗口</font></a></li>
  <li><a href="delphi1.html#CreateObject"><font face="楷体_GB2312" size="4">动态建立不常用的对象</font></a></li>
  <li><a href="delphi1.html#CurrentPos"><font face="楷体_GB2312" size="4">获取RichEdit构件的当前光标位置</font></a></li>
  <li><a href="delphi1.html#LastAccessTime"><font face="楷体_GB2312" size="4">获取文件的最后被访问时间</font></a></li>
  <li><a href="delphi1.html#ProcessMessage"><font face="楷体_GB2312" size="4">在程序执行期间让其他控件能响应消息</font></a></li>
  <li><a href="delphi1.html#SelfName"><font face="楷体_GB2312" size="4">获取程序本身所在路径和文件名</font></a></li>
  <li><a href="delphi1.html#MyComponent"><font face="楷体_GB2312" size="4">怎样制作自己的控件</font></a></li>
</ul>

<hr width="80%">

<p><a name="CloseMDIChild"></a></p>
<div align="center"><center>

<table border="1" cellpadding="0" cellspacing="0">
  <tr>
    <td><font face="楷体_GB2312" size="5"><strong><em>正确关闭一个MDI子窗口</em></strong></font></td>
  </tr>
</table>
</center></div>

<p><font face="楷体_GB2312" size="4">  Delphi中MDI子窗口的关闭方式默认为缩小而不是关闭,所以当你单击子窗口右上角的关闭按钮时会发觉该子窗口只是最小化,而不是你预期的那样被关闭。解决办法是在子窗口的OnClose事件处理过程中加入如下代码,示例:</font></p>
<div align="center"><center>

<table border="0" cellpadding="2">
  <tr>
    <td nowrap><pre><font face="Courier New" color="#000000">procedure ChildForm.OnClose</font><font color="#000000">(</font><font face="Courier New" color="#000000">Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
end;</font></pre>
    </td>
  </tr>
</table>
</center></div>

<p><font face="楷体_GB2312" size="4">  Delphi为一个Form的关闭行为指定了四种方式,分别是:</font></p>
<div align="center"><center>

<table border="0" cellpadding="2">
  <tr>
    <td><font face="Courier New" color="#000000">caNone</font></td>
    <td><font face="楷体_GB2312" size="4">禁止Form被关闭</font></td>
  </tr>
  <tr>
    <td><font face="Courier New" color="#000000">caHide</font></td>
    <td><font face="楷体_GB2312" size="4">Form不被关闭,但是被隐藏。被隐藏的Form仍然可以被程序访问。</font></td>
  </tr>
  <tr>
    <td><font face="Courier New" color="#000000">caFree</font></td>
    <td><font face="楷体_GB2312" size="4">Form被关闭,并且释放其占用的资源。</font></td>
  </tr>
  <tr>
    <td><font face="Courier New" color="#000000">caMinimize</font></td>
    <td><font face="楷体_GB2312" size="4">Form被最小化而不是被关闭,这是MDI子窗口的默认关闭行为。</font></td>
  </tr>
</table>
</center></div>

<p align="center"><a href="delphi1.html#topofpage">回到页首</a></p>

<hr width="80%">

<p><a name="CreateObject"></a></p>
<div align="center"><center>

<table border="1" cellpadding="0" cellspacing="0">
  <tr>
    <td><strong><em><font face="楷体_GB2312" size="5">动态建立不常用的对象</font></em></strong></td>
  </tr>
</table>
</center></div>

<p><font face="楷体_GB2312" size="4">  不常使用的对象,例如一个About对话框应该只在需要的时候来动态地建立,这样可以避免浪费资源。举便来说,假定你建立了一个应用程序,并为其建立了一个About对话框,则生成的代码将在程序初始化建立主Form时也一并建立了About对话框的Form以及其关对象,这个About对话框将一直占用部分资源直到程序结束,即使用户从未激活这个About对话框。<br>
  较好的做法是让Delphi在建立主Form时不自动建立About对话框(或其他不常用的对象),激活Delphi主菜单的“Project”-“Options...”项,选取“Application”页,你会看到有两个列表框,左边列表框中的项目表示在程序初始化时将自动建立的所有Form,选择除主Form外的其他Form,单出“&gt;”按钮,将这些不需要在程序初始化时与主Form一并建立的东西全部移动右边的列表框中,这样,你的应用程序在初始化时就只建立主窗口,而不是自动建立一大堆用户可能从不访问的窗口。<br>
  下面的代码段说明了怎样动态地建立并激活一个About对话框:</font></p>
<div align="center"><center>

<table border="0" cellpadding="2">
  <tr>
    <td><pre><font face="Courier New" color="#000000">procedure MainForm.AboutItemClick(Sender:TObject);
begin
  with TAboutBox.Create(Application) do begin
    try
      ShowModal;
    finally
      Free;
    end;
  end;
end;</font></pre>
    </td>
  </tr>
</table>
</center></div>

<p><font face="楷体_GB2312" size="4">  上例动态地建立了一个TAboutBox类的Form,然后以MODAL方式来激活这个Form,其间无论出现什么错误,都保证会释放该动态对象所占的资源。</font></p>

<p align="center"><a href="delphi1.html#topofpage">回到页首</a></p>

<hr width="80%">

<p><a name="CurrentPos"></a></p>
<div align="center"><center>

<table border="1" cellpadding="0" cellspacing="0">
  <tr>
    <td><font face="楷体_GB2312" size="5" color="#FFFFFF"><strong><em>获取RichEdit的当前光标位置</em></strong></font></td>
  </tr>
</table>
</center></div>

<p><font face="楷体_GB2312" size="4">  在RichEdit的OnSelectionChange事件处理过程中加入适当的代码可以实时地获得当前光标位置,示例如下:</font></p>
<div align="center"><center>

<table border="0" cellpadding="2">
  <tr>
    <td><pre><font face="Courier New" color="#000000">procedure TForm1.RichEdit1SelectionChange(Sender: TObject);
var
  X, Y: LongInt;
begin
  Y := SendMessage(RichEdit1.Handle, EM_LINEFROMCHAR,
    RichEdit1.SelStart, 0);
  X := RichEdit1.SelStart -
    SendMessage(RichEdit1.Handle, EM_LINEINDEX, Y, 0);
  Inc(Y);  // </font><font color="#000000">光标的行号
  </font><font face="Courier New" color="#000000">Inc(X);  // </font><font color="#000000">光标的列号
</font><font face="Courier New" color="#000000">end;</font></pre>
    </td>
  </tr>
</table>
</center></div>

<p align="center"><a href="delphi1.html#topofpage">回到页首</a></p>

<hr width="80%">

<p><a name="LastAccessTime"></a></p>
<div align="center"><center>

<table border="1" cellpadding="0" cellspacing="0">
  <tr>
    <td><strong><em><font face="楷体_GB2312" size="5">获取文件最后被访问的时间</font></em></strong></td>
  </tr>
</table>
</center></div>

<p><font face="楷体_GB2312" size="4">  某些软件需要获取文件的最后被访问时间,这一属性是DOS文件系统所没有的,无法用传统的函数来做到。Windows提供一个函数GetFileTime做此项操作,在Delphi中可方便地调用,示例如下:</font></p>
<div align="center"><center>

<table border="0" cellpadding="2">
  <tr>
    <td><pre><font face="Courier New" color="#000000">procedure GetFileLastAccessTime(FileName: PChar);
var
  CreateFT, LastAccessFT, LastWriteFT: TFileTime;
  ST: TSystemTime;
  F: Integer;
begin
  { </font><font color="#000000">首先要用</font><font face="Courier New" color="#000000">Windows</font><font color="#000000">的标准</font><font face="Courier New" color="#000000">API</font><font color="#000000">函数以读方式打开文件 </font><font face="Courier New" color="#000000">}
  F := CreateFile(FileName, GENERIC_READ, 0,
         nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  if F=INVALID_HANDLE_VALUE then begin
    ShowMessage('Can not open file!');
    Exit;
  end;
  { </font><font color="#000000">取文件时间 </font><font face="Courier New" color="#000000">}
  if GetFileTime(F, @CreateFT, @LastAccessFT, @LastWriteFT) then
  begin
    { </font><font color="#000000">转换为系统时间并显示 </font><font face="Courier New" color="#000000">}
    FileTimeToSystemTime(LastAccessFT, ST);
    Label1.Caption := Format('%d-%d-%d, %d:%d:%d',
       [ST.wYear, ST.wMonth, ST.wDay,
        ST.wHour, ST.wMinute,ST.wSecond]);
  end;
  CloseHandle(F);  // </font><font color="#000000">记住关闭文件
</font><font face="Courier New" color="#000000">end;</font></pre>
    </td>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -