📄 delphi2.html
字号:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="Author" content="SeaWave">
<meta name="KeyWords" content="Delphi,Programming">
<title>Delphi实用技巧(二)</title>
</head>
<body background="../../images/background.gif" bgcolor="#628394" text="#FFFFFF" 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" alt="delphilogo.gif (5358 bytes)" align="right" border="0" WIDTH="93" HEIGHT="128">
<ul type="square">
<li><a href="#disable"><font face="楷体_GB2312" size="4">禁止用户用Alt+Tab切换任务</font></a></li>
<li><a href="#ChangeDisplay"><font face="楷体_GB2312" size="4">动态改变当前显示模式</font></a></li>
<li><a href="#Logo"><font face="楷体_GB2312" size="4">当程序启动时显示徽标画面</font></a></li>
<li><a href="#TrayIcon"><font face="楷体_GB2312" size="4">将程序缩为任务条右下角的小图标</font></a></li>
<li><a href="#OnlyOne"><font face="楷体_GB2312" size="4">防止程序被重复执行</font></a></li>
<li><a href="#HowMany"><font face="楷体_GB2312" size="4">限制设计者在Project中加入某类构件的数目</font></a></li>
</ul>
<hr width="80%">
<p><a name="disable"></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"> 假定我们要设计一个Windows95的口令程序,该程序运行时需要覆盖整个桌面,并且不允许用户用Alt+Esc、Ctrl+Esc等系统组合键来切换到其他程序。为达到此目的,可按以下步骤:
</font>
<ul type="square">
<li><font face="楷体_GB2312" size="4">将Form的FormStyle属性设为fsStayOnTop</font></li>
<li><font face="楷体_GB2312" size="4">将Form的WindowState属性设为wsMaximized</font></li>
<li><font face="楷体_GB2312" size="4">在Form的OnCreate事件处理过程中为Windows发送一个屏幕保护程序正在运行的消息</font></li>
<li><font face="楷体_GB2312" size="4">当程序结束时清除屏幕保护程序运行标志。</font></li>
</ul>
<p><font face="楷体_GB2312" size="4">示例代码:</font></p>
<div align="center"><center>
<table border="0" cellpadding="2">
<tr>
<td><pre><font face="Courier New" color="#000000">procedure TForm1.FormCreate(Sender: TObject);
var
temp: Integer;
begin
SystemParametersInfo(SPI_SCREENSAVERRUNNING, 1, @temp, 0);
end;
procedure Form1.OnClose(Sender: TObject; var Action: TCloseAction);
var
temp: Integer;
begin
SystemParametersInfo(SPI_SCREENSAVERRUNNING, 0, @temp, 0);
end;</font></pre>
</td>
</tr>
</table>
</center></div>
<p align="center"><a href="#topofpage">回到页首</a></p>
<hr width="80%">
<p><a name="ChangeDisplay"></a></p>
<div align="center"><center>
<table border="1" cellpadding="0" cellspacing="0">
<tr>
<td><font face="楷体_GB2312" size="5"><strong><em>动态改变当前显示模式</em></strong></font></td>
</tr>
</table>
</center></div>
<p><font face="楷体_GB2312" size="4"> Windows 95提供一个API函数EnumDisplaySettings()来获取所有显示模式的有关参数,包括颜色数、分辨率等等,下面的示例代码首先用该函数正确地填充TDevMode类型变量的各字段,然后指定新的数据,再调用ChangeDisplaySettings()函数来改变显示模式:</font></p>
<div align="center"><center>
<table border="0" cellpadding="2">
<tr>
<td><pre><font face="Courier New" color="#000000">procedure NewScreenSize(W,H: Integer);
var
DevMode: TDevMode;
begin
if EnumDisplaySettings(nil,0,DevMode) then begin
with DevMode do begin
dmPelsWidth := W;
dmPelsHeight := H;
dmFields := DM_PELSWIDTH or DM_PELSHEIGHT;
end;
ChangeDisplaySettings(DevMode, 0);
end;
end;</font></pre>
</td>
</tr>
</table>
</center></div>
<p align="center"><a href="#topofpage">回到页首</a></p>
<hr width="80%">
<p><a name="Logo"></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"> Delphi在启动时会首先显示一个图片,等到将所有的初始化工作全做完后该图片自动消失。为达到这一效果,建立一个示例程序。<br>
建立一个新Application,包括两个Form,其中Form1为主窗口,Form2为要显示的图片Form,不妨将Form2的边框类型设为无,同时在Form2中加入一个TImage类的构件Image1,Align属性为Client,AutoSize设为True,然后为Image1指定任意一个图片。<br>
选Project1/Options菜单,点取Application页,将Form2从左边列表框中移到右边列表框,这样Project1.dpr源程序就不会生成建立Form2的代码。下一步是手工修改Project1.DPR源代码,因为Form2应该在Form1之前建立并显示,但不能使用Application的CreateForm方法(第一个用此方法建立的Form被认为是主Form),具体的代码如下,当运行至Application.CreateForm(...)一行时Form2已经建立并显示出来,然后该行建立主Form并激发主Form的OnCreate事件,你可以在OnCreate事件处理过程中完成你的初始化工作。</font></p>
<div align="center"><center>
<table border="0" cellpadding="0" cellspacing="0">
<caption><font face="Arial" size="4"><strong>Project1.dpr</strong></font></caption>
<tr>
<td><pre><font face="Courier New" color="#000000">program Project1;
uses
Forms,
Unit1 in 'Unit1.pas' {Form1},
Unit2 in 'Unit2.pas' {Form2};
{$R *.RES}
begin
Application.Initialize; {</font><font color="#000000">程序初始化</font><font face="Courier New" color="#000000">}
Form2 := TForm2.Create(Application); {</font><font color="#000000">建立徽标</font><font face="Courier New" color="#000000">Form}
Form2.Show; {</font><font color="#000000">显示徽标</font><font face="Courier New" color="#000000">Form}
Form2.Update; {</font><font color="#000000">重画徽标</font><font face="Courier New" color="#000000">Form</font><font color="#000000">,确保其中的图片被显示出来</font><font face="Courier New" color="#000000">}
Application.CreateForm(TForm1, Form1); {</font><font color="#000000">建立主</font><font face="Courier New" color="#000000">Form}
Form2.Hide; {</font><font color="#000000">隐藏徽标</font><font face="Courier New" color="#000000">Form</font><font color="#000000">,使主</font><font face="Courier New" color="#000000">Form</font><font color="#000000">显示出来</font><font face="Courier New" color="#000000">}
Form2.Free; {</font><font color="#000000">释放徽标</font><font face="Courier New" color="#000000">Form</font><font color="#000000">占用的资源</font><font face="Courier New" color="#000000">}
Application.Run; {</font><font color="#000000">程序运行</font><font face="Courier New" color="#000000">}
end.</font></pre>
</td>
</tr>
</table>
</center></div>
<p><font face="楷体_GB2312" size="4"> 在主Form的OnCreate事件处理过程中我们用一些延时代码来模拟耗时的初始化工作,这样可以更清楚地看到Form2显示并停留数秒,直到主Form的OnCreate事件处理过程执行完毕后自动消失。</font></p>
<div align="center"><center>
<table border="0" cellpadding="0" cellspacing="0">
<caption><strong><font face="Arial" size="4">Unit1.pas</font></strong></caption>
<tr>
<td><pre><font face="Courier New" color="#000000">unit unit1;
interface
uses
Windows, Messages, SysUtils,
Classes, Graphics, Controls,
Forms, ExtCtrls;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
public
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
{ OnCreate</font><font color="#000000">事件处理过程,延时五秒 </font><font face="Courier New" color="#000000">}
procedure TForm1.FormCreate(Sender: TObject);
var
CurrTime: TSystemTime;
Sec: Word;
begin
GetSystemTime(CurrTime);
Sec := CurrTime.wSecond + 5;
while Sec>CurrTime.wSecond do
GetSystemTime(CurrTime);
end;
end.</font></pre>
</td>
</tr>
</table>
</center></div>
<p><font face="楷体_GB2312" size="4"> 执行这个程序,会看到令人满意的效果:启动画面成功地显示出来,而且当鼠标移至画面上时会自动变成沙漏形状,五秒钟后画面消失,主窗口出现。</font></p>
<p align="center"><a href="#topofpage">回到页首</a></p>
<hr width="80%">
<p><a name="TrayIcon"></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>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -