上学吧:级考试资料下载:操作符重载的例子unitWinForm;interfaceusesSystem.Drawing,System.Collections,System.ComponentModel,System.Windows.Forms,System.Data;typeTWinForm=class(System.Windows.Forms.Form){$REGION\'DesignerManagedCode\'}strictprivate//////Requireddesignervariable.///Components:System.ComponentModel.Container;Button1:System.Windows.Forms.Button;//////RequiredmethodforDesignersupport-donotmodify///thecontentsofthismethodwiththecodeeditor.///procedureInitializeComponent;procedureButton1_Click(sender:System.Object;e:System.EventArgs);{$ENDREGION}strictprotected//////Cleanupanyresourcesbeingused.///procedureDispose(Disposing:Boolean);override;private{PrivateDeclarations}publicconstructorCreate;end;//写成类也可以,这里我用了记录。由于记录是值类型省去了创建实例的麻烦TClassTest=recordpublicFA:Integer;//重载了“+”操作符classoperatoradd(A,B:TClassTest):TClassTest;end;[assembly:RuntimeRequiredAttribute(TypeOf(TWinForm))]implementation上学吧:级考试资料下载:{$REGION\'WindowsFormDesignergeneratedcode\'}//////RequiredmethodforDesignersupport--donotmodify///thecontentsofthismethodwiththecodeeditor.///procedureTWinForm.InitializeComponent;beginSelf.Button1:=System.Windows.Forms.Button.Create;Self.SuspendLayout;////Button1//Self.Button1.Location:=System.Drawing.Point.Create(96,88);Self.Button1.Name:=\'Button1\';Self.Button1.Size:=System.Drawing.Size.Create(392,112);Self.Button1.TabIndex:=0;Self.Button1.Text:=\'Button1\';Include(Self.Button1.Click,Self.Button1_Click);////TWinForm//Self.AutoScaleBaseSize:=System.Drawing.Size.Create(6,14);Self.ClientSize:=System.Drawing.Size.Create(560,357);Self.Controls.Add(Self.Button1);Self.Name:=\'TWinForm\';Self.Text:=\'WinForm\';Self.ResumeLayout(False);end;{$ENDREGION}procedureTWinForm.Dispose(Disposing:Boolean);beginifDisposingthenbeginifComponentsnilthenComponents.Dispose();end;inheritedDispose(Disposing);end;constructorTWinForm.Create;begininheritedCreate;//上学吧:级考试资料下载::AddanyconstructorcodeafterInitializeComponentcall//end;procedureTWinForm.Button1_Click(sender:System.Object;e:System.EventArgs);varA,B,C:TClassTest;beginA.FA:=1;B.FA:=2;C:=A+B;//两个结构(或者类)用+操作,在DELHI8以前是不可想象的system.Windows.Forms.MessageBox.Show(System.Convert.ToString(C.FA));end;{TClassTest}//重载“+”操作符的实现classoperatorTClassTest.add(A,B:TClassTest):TClassTest;beginResult.FA:=A.FA+B.FA;end;end.delphitree从一个表复制到另一个表由于TREE从源表复制到目标表的ID号与PARENT会变化,故不能单纯的复制数据,还要将ID与PARENT的结构关系复制到目标表。因此我们用递归操作。由于myquery的数据会变动,全国计算机等级考试网,加入收藏我们要实时创建myquery。下面是delphitree从一个表复制到另一个表的递归源码:ProcedureTfrm_SysDepasManager.CopyTree(socParent,desParent:integer);//树递归复制过程Varmyquery:TADOQuery;mysoc,mydes:integer;Beginmyquery:=Tadoquery.Create(Nil);Try上学吧:级考试资料下载::=frmdata.ADOConnection1;myquery.Active:=false;myquery.SQL.Clear;myquery.SQL.Add('select*fromLCConstDepawhereparent='+inttostr(socParent));myquery.Active:=true;//插记录WhileNotmyquery.EofDoBeginmysoc:=myquery.FieldByName('ID').AsInteger;ReDataSet2.Append;ReDataSet2.FieldByName('shlid').AsInteger:=Syspublic.LoginSchoolID;ReDataSet2.FieldByName('name').AsString:=myquery.FieldByName('name').AsString;ReDataSet2.FieldByName('pinyin').AsString:=myquery.FieldByName('pinyin').AsString;ReDataSet2.FieldByName('parent').AsInteger:=desParent;ReDataSet2.Post;mydes:=ReDataSet2.FieldByName('ID').AsInteger;CopyTree(mysoc,mydes);myquery.Next;End;Finallymyquery.Free;End;End;Delphi实现对注册表的监视和扫描Delphi自带的TRegistry类只能实现注册表的基本操作,如果我们要实时监视注册表的变化或者扫描注册表特定项下的所有子项,TRegistry类就无能为力了。我啃了半天SDK,终于实现了Delphi对注册表的监视与扫描,不敢独享,拿来献给广大的Delphi爱好者。监视注册表相关项的改变要用到一个API:RegNotifyChangeKeyValue。LONGRegNotifyChangeKeyValue(HKEYhKey,//要监视的一个项的句柄BOOLbWatchSubtree,//是否监视此项的子键DWORDdwNotifyFilter,//监视哪些变化HANDLEhEvent,//接受注册表变化事件的事件对象句柄BOOLfAsynchronous//注册表变化前报告还是注册表变化后才报告);注意上面的hEvent是接受注册表变化事件的事件对象句柄,我们要用API:CreateEvent来创建一个系统事件对象。HANDLECreateEvent(LPSECURITY_ATTRIBUTESlpEventAttributes,//SECURITY_ATTRIBUTES结构上学吧:级考试资料下载:是否自动重置BOOLbInitialState,//是否设置初始状态LPCTSTRlpName//事件对象的名称);新建一个工程,添加一个ListBox,两个Button。//先写个监视注册表的例子//监视HKEY_CURRENT_USER\\Software项下所有子键procedureTForm1.Button1Click(Sender:TObject);varhNotify:THandle;hKeyx:HKEY;dwRes:DWORD;beginhNotify:=CreateEvent(nil,//不使用SECURITY_ATTRIBUTES结构FALSE,//不自动重置TRUE,//设置初始状态\’RegistryNotify\’//事件对象的名称);ifhNotify=0thenbeginShowmessage(\’CreateEventfailed.\’);exit;end;ifRegOpenKeyEx(HKEY_CURRENT_USER,//跟键\’Software\’,//子键0,//reservedKEY_NOTIFY,//监视用hKeyx//保存句柄)ERROR_SUCCESSthenbeginCloseHandle(hNotify);Showmessage(\’RegOpenKeyExfailed.\’);exit;end;ifRegNotifyChangeKeyValue(hKeyx,//监视子键句柄TRUE,//监视此项的子键REG_NOTIFY_CHANGE_NAMEorREG_NOTIFY_CHANGE_LAST_SET,hNotify,//接受注