Chinaunix首页 | 论坛 | 博客
  • 博客访问: 14481272
  • 博文数量: 5645
  • 博客积分: 9880
  • 博客等级: 中将
  • 技术积分: 68081
  • 用 户 组: 普通用户
  • 注册时间: 2008-04-28 13:35
文章分类

全部博文(5645)

文章存档

2008年(5645)

我的朋友

分类:

2008-04-28 20:47:10

下载本文示例代码
p>  Delphi是Borland公司推出的一个可视化、面向对象的快速应用程序开发工具,继16位的1.0版本后,又推出了32位的2.0、3.0版本,很受开发人员的欢迎。下面就将我在学习Delphi过程中的一些心得奉献给大家,希望能给您带来帮助和启示。   一、如何使Form在不同的聊环直率下显示出同样大小?   我喜欢把我的15英寸显示器置于800×600的模式下,因为这时的色彩和字体都比较好看,但是在这种模式下用Delphi设计的程序运行于别的机器上时往往由于分辨率不同、字体大小不同显得很难看,相信不少朋友和我一样遇到同样的情况,当然我们可以“强迫”别人也使用800×600的分辨率。那么有没有办法使自己的程序“专业化”一些,能够自适应屏幕的分辨率呢,答案是肯定的,因为Delphi 提供了ScaleBy这个过程,利用这个过程我们可以在FormCreate时轻易地控制form 的外观。实现的代码如下: implementation const ScreenWidth:LongInt=800; ScreenHeight:LongInt=600; {I designed my form in 800×600 mode.} {$ R*.DFM} procedure TMainForm.FormCreate(Sender:TObject); var OldFormWidth:integer; begin Scaled:=TRUE; if(Screen.width $#@60;$#@62; ScreenWidth)then begin OldFormWidth:=Width Height:=longint(Height)*longint(Screen.Height)DIV ScreenHeight; Width:=longint(Width)*longint(Screen.Width)DIV ScreenWidth; ScaleBy(Screen.Width,ScreenWidth); Font.Size:=(Width DIV OldFormWidth)*FontSize; end; end;   二、如何判断一个程序是否已在运行?   在某些时候我们通常需要自己编制的程序只可以有一份拷贝在运行,如何作到这一点呢?通常我们可以用GetWindowsWord获得窗口句柄,再用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, Controls, 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 $#@60;$#@62; 0)AND(GetLastError()=ERROR_ALREADY_EXISTS))then begin CloseHandle(hSem); hWndMe:=FindWindow(nil,wTtl);//获得当前运行的窗口句柄,改变标题 SetWindowText(hWndMe,zzzzzzz);//这样才可以寻找其他instance //寻找这个视窗的instance,然后将它置于Z-order顶层 hWndMe:=FindWindow(nil,wTtl); if(hWndMe $#@60;$#@62; 0)then begin if IsIconic(hWndMe) then ShowWindow (hWndMe, SW_SHOWNORMAL) else SetForegroundWindow(hWndMe); end; Result:=True; end; end; procedure TForm.FormCreate(Sender:TObject); begin if DOIExist(Self.Caption)then Halt; end; p>  Delphi是Borland公司推出的一个可视化、面向对象的快速应用程序开发工具,继16位的1.0版本后,又推出了32位的2.0、3.0版本,很受开发人员的欢迎。下面就将我在学习Delphi过程中的一些心得奉献给大家,希望能给您带来帮助和启示。   一、如何使Form在不同的聊环直率下显示出同样大小?   我喜欢把我的15英寸显示器置于800×600的模式下,因为这时的色彩和字体都比较好看,但是在这种模式下用Delphi设计的程序运行于别的机器上时往往由于分辨率不同、字体大小不同显得很难看,相信不少朋友和我一样遇到同样的情况,当然我们可以“强迫”别人也使用800×600的分辨率。那么有没有办法使自己的程序“专业化”一些,能够自适应屏幕的分辨率呢,答案是肯定的,因为Delphi 提供了ScaleBy这个过程,利用这个过程我们可以在FormCreate时轻易地控制form 的外观。实现的代码如下: implementation const ScreenWidth:LongInt=800; ScreenHeight:LongInt=600; {I designed my form in 800×600 mode.} {$ R*.DFM} procedure TMainForm.FormCreate(Sender:TObject); var OldFormWidth:integer; begin Scaled:=TRUE; if(Screen.width $#@60;$#@62; ScreenWidth)then begin OldFormWidth:=Width Height:=longint(Height)*longint(Screen.Height)DIV ScreenHeight; Width:=longint(Width)*longint(Screen.Width)DIV ScreenWidth; ScaleBy(Screen.Width,ScreenWidth); Font.Size:=(Width DIV OldFormWidth)*FontSize; end; end;   二、如何判断一个程序是否已在运行?   在某些时候我们通常需要自己编制的程序只可以有一份拷贝在运行,如何作到这一点呢?通常我们可以用GetWindowsWord获得窗口句柄,再用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, Controls, 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 $#@60;$#@62; 0)AND(GetLastError()=ERROR_ALREADY_EXISTS))then begin CloseHandle(hSem); hWndMe:=FindWindow(nil,wTtl);//获得当前运行的窗口句柄,改变标题 SetWindowText(hWndMe,zzzzzzz);//这样才可以寻找其他instance //寻找这个视窗的instance,然后将它置于Z-order顶层 hWndMe:=FindWindow(nil,wTtl); if(hWndMe $#@60;$#@62; 0)then begin if IsIconic(hWndMe) then ShowWindow (hWndMe, SW_SHOWNORMAL) else SetForegroundWindow(hWndMe); end; Result:=True; end; end; procedure TForm.FormCreate(Sender:TObject); begin if DOIExist(Self.Caption)then Halt; end; 下载本文示例代码


Delphi使用心得Delphi使用心得Delphi使用心得Delphi使用心得Delphi使用心得Delphi使用心得Delphi使用心得Delphi使用心得Delphi使用心得Delphi使用心得Delphi使用心得Delphi使用心得Delphi使用心得Delphi使用心得Delphi使用心得
阅读(177) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~