怀念一下这些经常不记得的Delphi代码


本文整理自网络,侵删。

  1.防止刷新时闪烁的终极解决办法

{ 防止刷新时闪烁的终极解决办法(对付双缓冲无效时) }
Perform($000B, 0, 0); //锁屏幕 防止闪烁
// 做一些会发生严重闪烁的事情..
//解锁屏幕并重画
Perform($000B, 1, 0);
RedrawWindow(Handle, nil, 0, RDW_FRAME + RDW_INVALIDATE + RDW_ALLCHILDREN + RDW_NOINTERNALPAINT);

2.图片上显示透明文字

//图片上显示透明的文字
//直接用.Canvas.Brush.Style:=bsClear;
//然后.Canvas.TextOut(x,y,’文字显示透明’);
procedure TForm1.Button1Click(Sender: TObject);
var
bitBuf:TBitmap;
begin
bitBuf := TBitmap.Create;
try
bitbuf.LoadFromFile(‘测试图片.bmp’);
Self.Canvas.Draw(0,0,bitbuf);
bitbuf.Transparent := True;
bitbuf.TransparentColor := clWhite; //文字显示透明
bitbuf.Canvas.font.color := clBlue; //文字颜色
bitbuf.Canvas.TextOut(10,10,’这样就是透明的字了!’);
Self.Canvas.Draw(0,0,bitbuf);
finally
bitBuf.Free;
end;
end;

3.取得本机IP地址(精简版)

//取得本地IP地址(精简版)
//注:使用函数前需要 WSAStartup($202, wsdata);
function GetLocalIP(): String;
var
HostName: array[0..255] of Char;
HostEnt: PHostEnt;
begin
Result := ”;
if gethostname(HostName, 255) = 0 then
begin
HostEnt := gethostbyname(HostName);
Result := StrPas(inet_ntoa(PInAddr(PInAddr(HostEnt^.h_addr_list)^)^));
end;
end;



4.报告内存泄漏

// 在程序中加上这句,当退出时会报告内存泄漏
ReportMemoryLeaksOnShutdown := True;

5.释放资源文件

// 首先加入.RC文件,写上 MyDLL DAT testDLL.dll
// 然后程序里 ExtractRes(‘DAT’,'MyDLL’,'123DLL.dll’);
procedure ExtractRes(ResType, ResName, ResNewName:String);
var Res:TResourceStream;
begin
Res:=TResourceStream.Create(Hinstance, Resname, Pchar(ResType));
try
Res.SavetoFile(‘.\’+ResNewName); // 释放到当前目录
finally
Res.Free;
end;
end;

6.延时

// 模仿VB里DoEvents的延时
procedure Delay(const uDelay: DWORD);
var
n: DWORD;
begin
n := GetTickCount;
while ( (GetTickCount ?C n) <= uDelay ) do
Application.ProcessMessages;
end;

7.标准C中的 itoa() 函数Delphi版,将Int型变量转化为(radix)进制输出

//二进制 itoa(i, 2);
//八进制 itoa(i, 8);
//十六进制 itoa(i, 16);
function itoa(aData, radix: Integer): String;
var
t: Integer;
begin
Result := ”;
repeat
t := aData mod radix;
if t < 10 then
Result := InttoStr(t)+Result
else
Result := InttoHex(t, 1)+Result;
aData := aData div radix;
until (aData = 0);
end;

8.程序删除自身

// 利用批处理文件构造一个循环,只要在 OnClose() 中调用 DeleteMe() 就可以删除自身
procedure DeleteMe();
var
BatchFile: TextFile;
BatchFileName: string;
ProcessInfo: TProcessInformation;
StartUpInfo: TStartupInfo;
begin
BatchFileName := ExtractFilePath(ParamStr(0)) + ‘_deleteme.bat’;
AssignFile(BatchFile, BatchFileName);
Rewrite(BatchFile);
Writeln(BatchFile, ‘:try’);
Writeln(BatchFile, ‘del "’ + ParamStr(0) + ‘"’);
Writeln(BatchFile,
‘if exist "’ + ParamStr(0) + ‘"’ + ‘ goto try’);
Writeln(BatchFile, ‘del %0′);
CloseFile(BatchFile);
FillChar(StartUpInfo, SizeOf(StartUpInfo), $00);
StartUpInfo.dwFlags := STARTF_USESHOWWINDOW;
StartUpInfo.wShowWindow := SW_HIDE;
if CreateProcess(nil, PChar(BatchFileName), nil, nil, False, IDLE_PRIORITY_CLASS, nil, nil, StartUpInfo, ProcessInfo) then
begin
CloseHandle(ProcessInfo.hThread);
CloseHandle(ProcessInfo.hProcess);
end;
end;

9.安装服务后立即启动

普通编写的服务,安装后必须重启才能启动,这是自动启动服务的方法:

{ Automatically start a service after using /Install or /Uniinstall switch
In the service unit add these statements before the ‘end.’ statement.
To automatically start or stop the service during install or uninstall
Tested only on Win2k and WinXP
Change the ‘myservice’ in both WinExec statements to your own service name.
}
// 在Service的初始化和结束部分加入如下代码:
initialization
if (ParamCount >= 1) and (CompareText(‘/uninstall’,ParamStr(1))=0) then
begin
// 如果是卸载,先停止服务。注意修改 myservice 为你的服务名
WinExec(‘cmd.exe /c net stop myservice‘, sw_hide);
sleep(3000);
end;
finalization
if (ParamCount >= 1) and (CompareText(‘/Install’,ParamStr(1))=0) then
// 用 net start 执行服务
WinExec(‘cmd.exe /c net start myservice‘,sw_hide);
end.

10.给自己编写的服务程序添加描述

没有描述的服务看起来要多可疑有多可疑,仿照微软的写法给你的服务加个描述吧:

procedure TMyService1.ServiceAfterInstall(Sender: TService);
var
MyReg: TRegistry;
begin
MyReg := TRegistry.Create;
try
with MyReg do
begin
RootKey := HKEY_LOCAL_MACHINE;
{ Service1是服务的名字,注意修改成你自己的 }
if Openkey(‘SYSTEM\CurrentControlSet\Services\Service1′, true) then
WriteString(‘Description’, ‘你自己的服务描述…’);
CloseKey;
end;
finally
MyReg.Free;
end;
end;

比如伪装装成这样:Security Debug Manager (仿Security Accounts Manager)
描述:管理系统安全设置和配置,并提供调试信息。如果此服务被终止,此类型安全措施将不可用。如果此服务被禁用,任何依赖它的服务将无法启动。

相关阅读 >>

Delphi与汇编

Delphi tnethttpcleint提交json串

Delphi 删除确认对话框

5种运行程序的方法具体应用实例

Delphi 使用zlib对datasetprovider.data数据包进行压缩传输的测试

Delphi实现win10下Delphi 10.3.1 inline hook 调试器法获取寄存器并修改

Delphi 调用js解决全角转半角

Delphi 中有 iif() 函数

Delphi webbrowser1使用进度条查看浏览器状态

Delphi 在firemonkey应用程序中使用torientationsensor获取设备倾斜和指南针航向

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...