Delphi小知识点整理

Delphi
placeholder image
admin 发布于:2017-11-17 14:37:25
阅读:loading

基本

'字符串以单引号囊括,逻辑等号判断用=,不等用<> ,逻辑并用and,逻辑或用or,赋值用:=


代码注释

//单行注释

{}区域注释 


类型转换

inttostr(xxx);

strtoint(xxx);

booltoint(xxx);

等等...


弹出框(alert)

showmessage('xxx');

Application.MessageBox(PChar('内容' ) , '标题' , MB_OK);


延迟响应(单位毫秒)


Sleep(1000);


鼠标坐标

SetCursorPos(100 , 100);//设置鼠标坐标

---------------------------------

R: TRect;

GetWindowRect(元素 , R);//获取某个元素的坐标

 

格式化当前时间

FormatDateTime('yyyymmddhhnn' , now);


鼠标左键按下

mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);

mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);


鼠标右键按下

mouse_event(MOUSEEVENTF_RIGHTDOWN,0,0,0,0);

mouse_event(MOUSEEVENTF_RIGHTUP,0,0,0,0);


打开一个软件

WinExec(Pchar('路径) , sw_normal);

ShellExecute(Application.Handle , 'open' , Pchar('路径') , nil , nil , SW_HIDE);


字符串处理

UpperCase('Java');//字母转换为大写

LowerCase('Java');//字符转换为小写

copy('java' , 2 , 2);//字符串截取:从第2个位置开始,截取2个长度

length('Java');//字符串长度

trim(' Java ');//去除空格

RightStr('Java' , 2);//向右截取字符串

Pos('abc' , '1abc1');//字符串出现的位置


数组处理

与Java等其他语言一样,用下标的方式进行获取,如array[0],array[1]...等

array.count为数组的长度个数


变量定义

var index:Integer;


if判断

if 1 =1 then 

     showmessage('1=1');

//------------------------------最简单的if


if 1=1 then

begin

      showmessage('ok');

end;

//------------------------------看着比较爽的if 

if 1=2 then begin

      showmessage('1=2');

end

else if 1=3 then begin

      showmessage('1=3');

end;

//------------------------------if-else if

if 1=2 then begin

     showmessage('1=2');

end

else if 1=3 then begin

      showmessage('1=3');

end

else

begin

      showmessage('over');

end; 

//------------------------------if-else if-else


while循环

while 1=1 do begin 

     //TOOD

end;


for循环

for vIndex :=1 to 100 do

begin 

      //TODO

end;


function语法

function 函数名称(参数名:参数类型):返回值类型

//多个参数;分割,参考如下:


function FindProcess(exeFileName: String): String;

begin 

     Result := 'true';

end; 


procedure过程语法

与function基本一致,定义改成procedure,无需返回值,区别与数据库的存储过程与自定义函数的区别


return返回值

使用Result赋值返回,返回字符串如FindProcess函数;

返回数组SetLength(result , 3);

result[0] := 'A';

result[1] := 'B';

result[2] := 'C';


关于句柄类

根据标题获取一个窗口句柄

FindWindow(nil , '窗口名称');

根据一个句柄获取子级元素句柄

FindWindowEx(上级句柄 , 0 , '类型名称' , nil);或FindWindowEx(上级句柄 , 0 , '类型名称' , '文本值');

获取同级句柄的第一个

getWindow(某句柄 , GW_HWNDFIRST);

获取同级句柄的上一个

getWindow(某句柄 , GW_HWNDPREV);

获取同级句柄的下一个

getWindow(某句柄 , GW_HWNDNEXT);

获取同级句柄的最后一个

getWindow(某句柄 , GW_HWNDLAST);


给某句柄发送鼠标按下事件

SendMessage(某句柄 , WM_LBUTTONDOWN , 0 , 0);

SendMessage(某句柄 , WM_LBUTTONUP , 0 , 0);

获取某句柄的文本值

SendMessage(某句柄 , WM_GETTEXT , 256 , integer(@p));

SendMessage(某句柄 , WM_SETTEXT , 256 , integer(pchar(文本值));


给句柄发送按键(回车键、数字键等)可触发类似密码长度校验的规则

PostMessage(某句柄 , WM_KEYDOWN , VK_RETURN , 0);


常用工具函数

判断进程是否存在

function FindProcess(exeFileName: String): String;

var

    hSnapshot: THandle;

    lppe: TProcessEntry32;

    Found: Boolean;

begin

    Result := 'false';

    hSnapshot := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS , 0);

    lppe.dwSize := SizeOf(TProcessEntry32);

    Found := Process32First(hSnapshot , lppe);

    while Found do

    begin

        if((UpperCase(ExtractFileName(lppe.szExeFile)) = UpperCase(exeFileName)) or (UpperCase(lppe.szExeFile) = UpperCase(exeFileName))) then

        begin

            Result := 'true';

            break;

        end;

        Found := Process32Next(hSnapshot , lppe);

    end;

end;


结束一个进程

function EndProcess(exeFileName:String):String;

var

    hSnapshot: THandle;

    lppe: TProcessEntry32;

    Found: Boolean;

    val:integer;

    findResult :String;

begin

    findResult := FindProcess(exeFileName);

    if findResult = 'false' then

    begin

        Result := findResult;

        exit;

    end;

    val := 0;

    hSnapshot := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS , 0);

    lppe.dwSize := SizeOf(TProcessEntry32);

    Found := Process32First(hSnapshot , lppe);

    while Integer(Found) <> 0 do

    begin

        if((UpperCase(ExtractFileName(lppe.szExeFile)) = UpperCase(exeFileName)) or (UpperCase(lppe.szExeFile) = UpperCase(exeFileName))) then

        val := Integer(TerminateProcess(OpenProcess(PROCESS_TERMINATE, BOOL(0) , lppe.th32ProcessID) , 0));

        Found := Process32Next(hSnapshot , lppe);

    end;

    if val = 0 then

    begin

        Result := 'false';

    end

    else begin

        Result := 'true';

        CloseHandle(hSnapshot);

    end;

end;


切换应用程序(类似Alt+Shift)

procedure SwitchToWindow(hWnd:Thandle; fAltTab:boolean);stdcall;external 'User32.dll';

字符串分割

function SplitString(Source:String ; Deli: string):TStringList;stdcall;

var

  EndOfCurrentString: byte;

  StringList:TStringList;

begin

  StringList:=TStringList.Create;

  while Pos(Deli, Source)>0 do

  begin

    EndOfCurrentString := Pos(Deli, Source);

    StringList.add(Copy(Source, 1, EndOfCurrentString - 1));

    Source := Copy(Source, EndOfCurrentString + length(Deli), length(Source) - EndOfCurrentString);

end; 

    Result := StringList;

    StringList.Add(source);

end;

上述许多内容已经过时和过期了,留存本篇文章仅为方便个人查看,原始文章的信息参考:

原始链接:https://www.chendd.cn/information/viewInformation/other/207.a

最后更新:2017-11-17 14:37:25

访问次数:218

评论次数:0

点赞个数:0

 点赞


 发表评论

当前回复:作者

 评论列表


留言区