几个Delphi技巧


1、判断一个字符串是否包含于另外一个字符串的方法
例如:if pos('ab','abcd')<>0 then
messagedlg('ab是包含于abcd',mtConfirmation,[mbYes, mbNo],0);
pos(obj,target) 在target字符串中找出第一个出现obj的第一个字符位置,如果找不到,返回0.

2、如何使窗口全屏,类似游戏一样,而不是窗口的最大化! 
(1) BorderStyle 为 bsNone
(2) Windowstate 为 wsMaximized
(3) 退出时可加一个按钮之类的,写上 close 即可退出。

3、数字格式化输出
format('%8.2f',[123.456]),返回字符串' 123.46'。

4、播放一个wav文件
use mmsystem;
SndPlaySound('hello.wav',SND_FILENAME or SND_SYNC);

5、InputBox,InputQuery和ShowMessage函数的威力
uses dialogs;
var s,s1:string;
b:boolean;
begin
s:=trim(Inputbox('NewPassword','Password','masterkey'));
b:=s<>''
s1:=s;
if b then b:=InputQuery('Confirm Password','Password',s1);
if not b or (s1<>s) then ShowMessage('Password Failed');
end;

6、几个有关子目录的操作的过程
MkDir(str); ChDir(str); GetDir(DriveID,str); SetCurrentDir(str);
IOResult --上面几个过程调用成功即返回0值

7、将一个可视控件变成图形类型
例如将一个带背景的LABEL变成一个TIMAGE图片类型,可以这样做:
image1.width:=label1.width;
image1.height:=label1.height;
label1.perform(WM_PAINT,image1.Canvas.Handle,0);

8、如何得到字符的ASCII值
得到字符的ASCII值,可以用如下语句:
var: a:integer;
string1:string;
begin
string1:='ABC'
a:=byte(string[1]); {此时就得到'A'的ASCII值}
end; 

9、动态更新DBGrid的颜色
例如,如果一个城市的人口大于200万,我们就让它显示为蓝色。使用的控件事件为DBGrid.OnDrawColumeCell: 
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect:TRect;DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if Table1.FieldByName('Population').AsInteger > 20000000 then
DBGrid1.Canvas.Font.Color := clBlue;
DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end; 

10、获得命令行参数
1. 取得命令列参数的个数: ParamCount 函数
2. 呼叫 ParamStr(0), 传回执行档的档名(含路径) 
3. 呼叫ParamStr(n), 传回第n个参数的内容
例子:
procedure TForm1.FormCreate(Sender: TObject);
var
 ix: integer;
begin
 Memo1.Lines.Clear;
 if ParamCount = 0 then
  Memo1.Lines.Add('没有参数')
 else
 begin
  Memo1.Lines.Add('档名:' + ParamStr(0));
  for ix := 1 to ParamCount do 
  Memo1.Lines.Add(ParamStr(ix));
 end;
end;
11、取得路径中的文件名:
ExtractFileName(Address.Text);
12、复制文件:
CopyFile(pchar(strSourcePath),pchar(strSavePath),false);
false表示如存在同名文件是否覆盖。
13、复制文件夹:
//下面两个函数为文件拷贝函数:
//1.1拷贝目录的递归辅助函数:DoCopyDir
function DoCopyDir(sDirName:String;sToDirName:String):Boolean;
var
  F: TSearchRec;
  //hFindFile:Cardinal;
  t,tfile:String;
  sCurDir:String[255];
  FindFileData:WIN32_FIND_DATA;
begin
//先保存当前目录
    sCurDir:=GetCurrentDir;
    ChDir(sDirName);
    F.FindHandle:=FindFirstFile('*.*',FindFileData);
    if F.FindHandle<>INVALID_HANDLE_VALUE then
    begin
      if not DirectoryExists(sToDirName) then    //如果目录不存在
         ForceDirectories(sToDirName);           //强迫建立目录
        repeat
            tfile:=FindFileData.cFileName;
            if (tfile='.') or (tfile='..') then
               Continue;                          //跳过两个目录标识
            if FindFileData.dwFileAttributes=FILE_ATTRIBUTE_DIRECTORY then
            begin
                t:=sToDirName+'/'+tfile;
                if not DirectoryExists(t) then
                   ForceDirectories(t);
                if sDirName[Length(sDirName)]<>'/' then
                   DoCopyDir(sDirName+'/'+tfile,t)
                else
                   DoCopyDir(sDirName+tfile,sToDirName+tfile);
            end
            else
            begin
                t:=sToDirName+'/'+tFile;
                CopyFile(PChar(tfile),PChar(t),false);
            end;
        until FindNextFile(F.FindHandle,FindFileData)=false;
           FindClose(F);
        end
        else
        begin
            ChDir(sCurDir);
            result:=false;
            exit;
        end;
        //回到原来的目录下
        ChDir(sCurDir);
        result:=true;
    end;

//---- 1.2拷贝目录的函数:CopyDir
function CopyDir(sDirName:String;sToDirName:string):Boolean;
begin
   if Length(sDirName)<=0 then
      exit;
        //拷贝...
   Result:=DoCopyDir(sDirName,sToDirName);
end;


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Creating Windows CreateMDIWindow CreateWindow CreateWindowEx RegisterClass RegisterClassEx UnregisterClass Message Processing BroadcastSystemMessage CallNextHookEx CallWindowProc DefFrameProc DefMDIChildProc DefWindowProc DispatchMessage GetMessage GetMessageExtraInfo GetMessagePos GetMessageTime GetQueueStatus InSendMessage PeekMessage PostMessage PostQuitMessage PostThreadMessage RegisterWindowMessage ReplyMessage SendMessage SendMessageCallback SendMessageTimeout SendNotifyMessage SetMessageExtraInfo SetWindowsHookEx TranslateMessage UnhookWindowsHookEx WaitMessage Window Information AnyPopup ChildWindowFromPoint ChildWindowFromPointEx EnableWindow EnumChildWindows EnumPropsEx EnumThreadWindows EnumWindows FindWindow FindWindowEx GetClassInfoEx GetClassLong GetClassName GetClientRect GetDesktopWindow GetFocus GetForegroundWindow GetNextWindow GetParent GetProp GetTopWindow GetWindow GetWindowLong GetWindowRect GetWindowText GetWindowTextLength IsChild IsIconic IsWindow IsWindowEnabled IsWindowUnicode IsWindowVisible IsZoomed RemoveProp SetActiveWindow SetClassLong SetFocus SetForegroundWindow SetParent SetProp SetWindowLong SetWindowText WindowFromPoint Processes and Threads CreateEvent CreateMutex CreateProcess CreateSemaphore CreateThread DeleteCriticalSection DuplicateHandle EnterCriticalSection ExitProcess ExitThread GetCurrentProcess GetCurrentProcessId GetCurrentThread GetCurrentThreadId GetExitCodeProcess GetExitCodeThread GetPriorityClass GetThreadPriority GetWindowThreadProcessId InitializeCriticalSection InterlockedDecrement InterlockedExchange InterlockedIncrement LeaveCriticalSection OpenEvent OpenMutex OpenProcess OpenSemaphore PulseEvent ReleaseMutex ReleaseSemaphore ResetEvent ResumeThread SetEvent SetPr

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值