Delphi是Borland公司推出的一个可视化、面向对象的快速应用程序开发工具,继16位的1.0版本
后,又推出了32位的2.0、3.0版本,很受开发人员的欢迎。下面就将我在学习Delphi过程中的一些心得奉献给
大家,希望能给您带来帮助和启示。
一、如何使Form在不同的屏幕分辨率下显示出同样大小?
我喜欢把我的15英寸显示器置于800×600的模式下,因为这时的色彩和字体都比较好看,但是在这种模式下
用Delphi设计的程序运行于别的机器上时往往由于分辨率不同、字体大小不同显得很难看,相信不少朋友和我一样遇到
同样的情况,当然我们可以“强迫”别人也使用800×600的分辨率。那么有没有办法使自己的程序“专业化”一些,能
够自适应屏幕的分辨率呢,答案是肯定的,因为Delphi提供了ScaleBy这个过程,利用这个过程我们可以在Fo
rmCreate时轻易地控制form的外观。
实现的代码如下:
implementation
const
ScreenWidth:LongInt=800;
ScreenHeight:LongInt=600;
{I designed my form in800×600 mode.}
{$ R*.DFM}
procedure TMainForm.FormCreate(Sender:TObject);
var
OldFormWidth:integer;
begin
Scaled:=TRUE;
if(Screen.width<> ScreenWidth)then
begin
OldFormWidth:=Width
Height:=longint(Height)*longint(Screen.Height)DIV S
creenHeight;
Width:=longint(Width)*longint(Screen.Width)DIV Scre
enWidth;
ScaleBy(Screen.Width,ScreenWidth);
Font.Size:=(Width DIV OldFormWidth)*FontSize;
end;
end;
二、如何判断一个程序是否已在运行?
在某些时候我们通常需要自己编制的程序只可以有一份拷贝在运行,如何作到这一点呢?通常我们可以用GetWi
ndowsWord获得窗口句柄,再用GetClassName获得并比较ClassName来达到目的。
...
Result:=true;
if GetWindowWord(Wnd, Gww_HINSTANCE)
=hPrevInst then
begin
GetClassName(Wnd,ClassName,30);
if StrIComp(ClassName,'TApplication')=0 then
begin
TargetWindow^:=Wnd;
Result:=false;
end;
end;
...
后来我曾在网上看过一段关于此方面的说明,它介绍了另一种方法,是通过在内存中建立旗语标志实现此功能的,试
了一下效果很好,具体的过程见下面的程序与注释:
unit prevcode;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Con
trols, Forms, Dialogs;
type
TForml=class(TForm)
procedure FormCreate(Sender:TObject);
private
{Private declarations}
public
{Public declarations}
end;
function DoIExist(WndTitle:String):Boolean;
var
Form1:TForm1;
implementation
{$R*.DFM
function DoIExist(WndTitle:String):Boolean;
var
hSem:THandle;
hWndMe,hWnPrev:HWnd;
semNm,wTtl:Array[0..256]of Char;
beigin
Result:=False;
StrPCopy(semNm,'SermaphoreName');
StrPCopy(wTtl,WndTitle);
hSem:=CreateSemaphore(nil,0,1,semNm);//如果第一次运行则建立一个
标志
//检查这个标志是否存在
if(hSem<>0)AND(GetLastError()=ERROR_ALREADY_EXISTS)
)then
begin
CloseHandle(hSem);
hWndMe:=FindWindow(nil,wTtl);//获得当前运行的窗口句柄,改变标题SetW
indowText(hWndMe,'zzzzzzz');//这样才可以寻找其他instance//寻找这个视窗
的instance,然后将它置于Z-order顶层hWndMe:=FindWindow(nil,wTtl);
if(hWndMe<>0)then
begin
if IsIconic(hWndMe) then ShowWindow(hWndMe, SW_SHOW
NORMAL)
else
SetForegroundWindow(hWndMe);
end;
Result:=True;
end;
end;
procedure TForm.FormCreate(Sender:TObject);
begin
if DOIExist(Self.Caption)then
Halt;
end;
end.
【相关论坛】 【发表评论】