Delphi常用技巧总结1

Delphi常用技巧 1

:如何让del+CTRL+ALT看不见程序运行?
:为了让程序用ALT+DEL+CTRL看不见,implementation后添加声明:
function RegisterServiceProcess(dwProcessID, dwType: Integer): Integer; stdcall; external 'KERNEL32.DLL';
再在上面的窗口Create事件加上一句:RegisterServiceProcess(GetCurrentProcessID, 1);//隐藏
也可以使用下面的函数:
function My_SelfHide: Boolean;
type
TRegisterServiceProcess = function(dwProcessID, dwType: DWord): DWORD; stdcall;
var
hNdl: THandle;
RegisterServiceProcess: TRegisterServiceProcess;
begin
Result := False;
if Win32Platform <> VER_PLATFORM_WIN32_NT then //
不是NT
begin
hNdl := LoadLibrary('KERNEL32.DLL');
RegisterServiceProcess := GetProcAddress(hNdl, 'RegisterServiceProcess');
RegisterServiceProcess(GetCurrentProcessID, 1);
FreeLibrary(hNdl);
Result := True;
end
else
Exit;
end;


:自我拷贝法怎么样使用?
:这种方法的原理是程序运行时先查看自己是不是在特定目录下,如果是就继续运行,如果不是就把自己拷贝到特定目录下,然后运行新程序,再退出旧程序.
打开Delphi,新建一个工程,在窗口的Create事件中写代码:
procedure TForm1.FormCreate(Sender: TObject);
var myname: string;
begin
myname := ExtractFilename(Application.Exename); //
获得文件名
if application.Exename <> GetWindir + myname then //
如果文件不是在Windows\System\那么..
begin
copyfile(pchar(application.Exename), pchar(GetWindir + myname), False);{
将自己拷贝到Windows\System\}
Winexec(pchar(GetWindir + myname), sw_hide);//
运行Windows\System\下的新文件
application.Terminate;//
退出
end;
end;
其中GetWinDir是自定义函数,起功能是找出Windows\System\的路径.
function GetWinDir: String;
var
Buf: array[0..MAX_PATH] of char;
begin
GetSystemDirectory(Buf, MAX_PATH);
Result := Buf;
if Result[Length(Result)]<>'\' then Result := Result + '\';
end;


:如何避免同时运行多个相同程序?
:为了避免同时运行多个程序的副本(节约系统资源也),程序一般会弄成每次只能运行一个.这又有几种方法.
一种方法是程序运行时先查找有没有相同的运行了,如果有,就立刻退出程序.
修改dpr项目文件,修改beginend之间的代码如下:
begin
Application.Initialize;
if FindWindow('TForm1','Form1')=0 then begin
//
当没有找到Form1时执行下面代码
Application.ShowMainForm:=False; //
不显示主窗口
Application.CreateForm(TForm1, Form1);
Application.Run;
end;
end.
另一种方法是启动时会先通过窗口名来确定是否已经在运行,如果是则关闭原先的再启动。冰河就是用这种方法的。
这样做的好处在于方便升级.它会自动用新版本覆盖旧版本.
方法如下:修改dpr项目文件
uses
Forms,windows,messages,
Unit1 in 'Unit1.pas' {Form1};


:如何能使程序能在windows启动时自动启动?
:为了程序能在Windows每次启动时自动运行,可以通过六种途径来实现.“冰河用注册表的方式。
加入Registry单元,改写上面的窗口Create事件,改写后的程序如下:
procedure TForm1.FormCreate(Sender: TObject);
const K = '\Software\Microsoft\Windows\CurrentVersion\RunServices';
var myname: string;
begin
{Write by Lovejingtao,http://Lovejingtao.126.com,Lovejingtao@21cn.com}
myname := ExtractFilename(Application.Exename); //
获得文件名
if application.Exename <> GetWindir + myname then //
如果文件不是在Windows\System\那么..
begin
copyfile(pchar(application.Exename), pchar(GetWindir + myname), False);{//
将自己拷贝到Windows\System\}
Winexec(pchar(GetWindir + myname), sw_hide);//
运行Windows\System\下的新文件
application.Terminate;//
退出
end;
with TRegistry.Create do
try
RootKey := HKEY_LOCAL_MACHINE;
OpenKey( K, TRUE );
WriteString( 'syspler', application.ExeName );
finally
free;
end;
end;


:怎么才能把自己的程序删除掉?
:很简单,可以写一个BAT文件
例如:a.bat
del %0
这样就把a.bat删除掉了!

放一个例子:
用过DOS的朋友应该还记得批处理文件吧,新建一个批处理文件a.bat,编辑其内容为:del %0,然后运行它,怎么样?a.bat把自己删除掉了!!!好,我们就用它来进行程序的自杀
找一个EXE可执行文件,比如说abc.exe,新建一个批处理文件a.bat,编辑其内容为:
:pp
del abc.exe
if exist abc.exe goto pp
del %0
先运行abc.exe,再运行a.bat,然后将abc.exe退出,你会发现a.exea.bat都没有了!!!按照这个思路,我们可以在程序中根据文件名称写一个批处理,将上面的abc.exe换成自己的EXE文件名就可以了。运行Delphi,新建一个工程,添加一个Button到窗体上,点击Button,写下如下代码:

procedure TForm1.Button1Click(Sender: TObject);
var Selfname,BatFilename,s1,s2:string;
BatchFile: TextFile;
begin
Selfname:=Extractfilename(application.exename);//
EXE文件自己的名称
BatFilename:=ExtractFilePath(Application.ExeName)+ 'a.bat';//
批处理文件名称
S1:='@del '+Selfname;
S2:='if exist '+Selfname+' goto pp';
assignfile(BatchFile,BatFilename);
rewrite(BatchFile);
writeln(BatchFile,':pp');
writeln(BatchFile,S1);
writeln(BatchFile,S2);
writeln(BatchFile,'@del %0');
closefile(BatchFile);
winexec(pchar(BatFilename),sw_hide);//
隐藏窗口运行a.bat
application.Terminate;//
退出程序
end;
那我们的事情是不是就完了?NO!上面的程序原理是对的,但如果你的程序是运行在系统目录下如Windows目录下或者Windows\System等目录下,除非你打开那个目录看着它删除,否则根本没法卸掉的。那怎么办?别急,我们请出一个函数CreateProcess,它的原型为:
BOOL CreateProcess(
LPCTSTR lpApplicationName, // pointer to name of executable module
LPTSTR lpCommandLine, // pointer to command line string
LPSECURITY_ATTRIBUTES lpProcessAttributes, // pointer to process security attributes
LPSECURITY_ATTRIBUTES lpThreadAttributes, // pointer to thread security attributes
BOOL bInheritHandles, // handle inheritance flag
DWORD dwCreationFlags, // creation flags
LPVOID lpEnvironment, // pointer to new environment block
LPCTSTR lpCurrentDirectory, // pointer to current directory name
LPSTARTUPINFO lpStartupInfo, // pointer to STARTUPINFO
LPPROCESS_INFORMATION lpProcessInformation // pointer to PROCESS_INFORMATION
);
这个函数和OpenProcessReadProcessMemoryWriteProcessMemory使用可以用来读取和修改内存数据,常用的游戏修改器就是用它。由于这些不是本文的重点所以这里不作详细介绍,感兴趣的读者可自行翻阅Delphi自带的帮助文件。用CreateProcess函数创建一个进程就可以完美的完成我们的程序自杀了。
运行Delphi,新建一个工程,添加一个Button到窗体上,全部代码如下:

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure My_DeleteMe; //
自定义程序自杀过程
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
My_DeleteMe;
end;
procedure TForm1.My_DeleteMe; //
程序自杀
//-----------------------------------------------------------
function GetShortName(sLongName: string): string; //
转换长文件名
var
sShortName: string;
nShortNameLen: integer;
begin
SetLength(sShortName, MAX_PATH);
nShortNameLen := GetShortPathName(PChar(sLongName),
PChar(sShortName), MAX_PATH - 1);
if (0 = nShortNameLen) then
begin
// handle errors...
end;
SetLength(sShortName, nShortNameLen);
Result := sShortName;
end;
//-------------------------------------------------
var
BatchFile: TextFile;
BatchFileName: string;
ProcessInfo: TProcessInformation;
StartUpInfo: TStartupInfo;
begin
BatchFileName := ExtractFilePath(ParamStr(0)) + '$$a$$.bat';
AssignFile(BatchFile, BatchFileName);
Rewrite(BatchFile);
Writeln(BatchFile, ':try');
Writeln(BatchFile, 'del "' + GetShortName(ParamStr(0)) + '"');
Writeln(BatchFile, 'if exist "' + GetShortName(ParamStr(0)) + '"' + ' goto try');
Writeln(BatchFile, 'del %0');
Writeln(BatchFile, 'cls');
Writeln(BatchFile, 'exit');
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;
Application.Terminate;
end;
end.

补充:1、上面的批处理的 del %0等同于 del a.bat,用del a.bat则批处理文件必须为a.bat,del %0则可以随意。
2
、所有程序在Pwin98+Delphi5Win2000+Delphi5下运行通过。
本文的标题为《安装与卸载之卸载篇》,下次将介绍如何用Delphi制作自己的安装程序。记得有一位著名的黑客说过:我从来不去找什么工具软件,需要的话就自己写一个。如果我们也持这种态度,则编程水平一定会越来越高。


:如何得到*******中的密码?
:这里有一个例子:
program password_dos;
{$apptype console} //
设置程序为非图形界面

uses
windows,
messages;

const s:boolean=true;//
置循环标志

var

pass_edit_hwnd:hwnd;//
密码窗口句柄
p:tpoint; //
鼠标指针

begin

writeln;
writeln('**************************************************************************');
writeln;
writeln;
writeln('
星号*密码破解器' );
writeln('
使用方法:将鼠标移动到密码框,密码就会自动现形!' );
writeln('
Ctrl+C 退出程序。 ' );
writeln(' \\\|/// ' );
writeln(' \\ - - // ' );
writeln(' ( @ @ ) ' );
writeln(' +----------------------oOOo-(_)-oOOo---------------------+ ');
writeln(' | | ');
writeln(' |
若在使用过程中发现任何问题或有新的想法请及时与我联系: | ');
writeln(' |
主页:http://lovejingtao.126.com | ');
writeln(' | E-MAIL: lovejingtao@21cn.com | ');
writeln(' | | ');
writeln(' | Oooo
陈经韬 2000.07 | ');
writeln(' +---------------------- oooO---( )---------------------+ ');
writeln(' ( ) ) / ' );
writeln(' \ ( (_/ ' );
writeln(' \_) ' );
writeln;
writeln('**************************************************************************');
writeln;
while s<>false do begin
getcursorpos(p); //
查鼠标坐标
pass_edit_hwnd:= WindowFromPoint(p); //
返回句柄
SendMessage(pass_edit_hwnd,EM_SETPASSWORDCHAR,0,0);//
发送消息
SendMessage(pass_edit_hwnd,WM_PAINT,0,0); //
SendMessage(pass_edit_hwnd,WM_KILLFOCUS,0,0); //
刷新窗口
SendMessage(pass_edit_hwnd,WM_SETFOCUS,0,0); //
sleep(1000); //
延时1000毫秒
end;
end.


:如何对注册进行操作
:首先:uses registry;
var
r:TRegistry
r:=Tregistry.Create;
r.RootKey:=HKEY_LOCAL_MACHINE
HKEY_CURRENT_USER 之类
r.OpenKey('Software\microsoft'
之类, true);
然后就可以 r.ReadString r.ReadIntegerr.WriteString r.WriteInteger 之类
r.Free;


:怎么使用ini文件进行一些设置的保存?
:其实很简单,uses中加入INIFiles然后可以在formonCreateonClose两个事件中写东西,onCreate是读出以前写的内容,onClose是写入更改过的内容,下面是一个例子:
放一个CheckBoxEdit

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls,INIFiles;//INIFiles
不要忘了加

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
With TINIFile.Create('a.ini') do//
创建a.ini
begin
WriteBool('MySetting', 'CheckBox1_Checked', CheckBox1.Checked);{
保存到MySetting下面的CheckBox1_Checked子键下,然后把Checkbox1的是否按下状态写进去}
WriteString('MySetting', 'Edit1_Text', Edit1.Text);//
同上
end;
end;

procedure TForm1.FormCreate(Sender: TObject);//
读入a.ini文件中的设置
begin
With TINIFile.Create('a.ini') do//
打开已创建的a.ini
begin
CheckBox1.Checked := ReadBool('MySetting', 'CheckBox1_Checked', False);{
同上面的写入一样,这里是读取ReadBoolWriteBool是两个BOOL值的写入方法.}
Edit1.Text := ReadString('MySetting', 'Edit1_Text', '');//
同上
end;
end;


:如何能使一个正在运行的程序自动最大化?
:这是一个例子:
var
hwndwindow:hwnd;
begin
hwndwindow:=findwindow(nil,'DELPHI
技巧');//DELPHI技艺改成你要最大化的窗口标提.
if hwndwindow<>0 then//
不等于0则是找到了这个窗体
postmessage(hwndwindow,WM_SYSCOMMAND,SC_MAXIMIZE,0);//
postmessage发送一条最大化消息(SC_MAXIMIZE)到这个窗体的句柄
//******************************************************
//
另外postmessage(hwndwindow,wm_close,0,0);为关闭
//
如果需要要自己的程序中使程序动态变最大化则用
form1.windowstate:=wsmaximized; //form1
为你要最大化的窗口名!
//
几个要用到的名词:
1.hwnd
是句柄的意思,只有先得到了窗体的句柄才能控制它
2.findwindow
是找窗体的意思
3.nil
是空指针的意思
4.postmessage
发送一条消息给一个已找到的窗口句柄.

 

posted on 2011-02-13 20:44  为人民服务  阅读(207)  评论(0编辑  收藏  举报