发点源代码:<br>{*******************************************************}<br>{ }<br>{ 解析XML文件单元 }<br>{ }<br>{*******************************************************}<br><br><br>unit XMLReader;<br><br>interface<br><br>uses SysUtils, Variants, Classes, Dialogs, Forms,xmldom, XMLIntf, msxmldom, XMLDoc,Types,Graphics,SyncObjs;<br><br>const DefBkColor=clyellow;<br> DefFontColor=ClRed;<br> DefFontSize=8;<br><br>type<br> TUrlCollection=class;<br> TUrlItem=class;<br><br> IUpdateFileXmlReader=Interface<br> procedure ChannelFiles(PlayType,Channel:string;var List,ListTime,ListURL:TStrings;IsLocal:Boolean=True);<br> procedure ChannelUpdateFiles(PlayType,Channel:string;var List,ListTime,ListURL:TStrings);<br> procedure AddFile(PlayType,Channel:string;FileName:string;URLFileName:string;LastModifyTime:string);<br> procedure FileUrls(PlayType,Channel,FileName:string;var URLs:TUrlCollection);<br> end;<br><br> ITopicFileXmlReader=Interface<br> procedure AllPlayTypes(List:TStrings);<br> procedure ChannelsOfPlayType(PlayTypeName:String;List:TStrings);<br> end;<br><br> TBaseXmlReader=class(TInterfacedObject)<br> private<br> FXmlDoc:TXMLDocument;<br> FPath:string;<br> procedure SetDocText(Text:string);<br> function GetDocText:string;<br> public<br> procedure SaveToFile;overload;<br> procedure EnumChildNodes(Node:IXMLNode;var Lst:TStrings); //枚举一个节点下的所有子节点。<br> procedure EnumChildNodesAttr(Node:IXMLNode;AttrName:string;var Lst:TStrings);//枚举一个节点下的所有子节点的某一属性的值<br><br> function FindNode(ParentNode:IXMLNode;tagName:string):IXMLNode;overload; //查找一个节点下的所有子节点中<br> function FindNode(ParentNode:IXMLNode;tagName:string;AttributeName,AttributeValue:WideString):IXMLNode;overload;<br> function FindNode(ParentNode:IXMLNode;tagName:string;<br> AttributeName1,AttributeValue1:WideString;<br> AttributeName2,AttributeValue2:WideString):IXMLNode;overload;<br> function FindNodeNext(Sibling:IXMLNode;tagName:string):IXMLNode;overload;<br> function FindNodeNext(Sibling:IXMLNode;tagName:string;AttributeName,AttributeValue:WideString):IXMLNode;overload;<br> function FindNodeNext(Sibling:IXMLNode;tagName:string;<br> AttributeName1,AttributeValue1:WideString;<br> AttributeName2,AttributeValue2:WideString):IXMLNode;overload;<br> public<br> constructor Create(APath:string);overload;<br> constructor Create;overload;<br> destructor Destroy;override;<br><br> procedure WriteToXML(APath:string; AStr:String); //将字符串写入到xml文件。原文件将被覆盖。<br><br> function FirstNode:IXMLNode;<br> function Root:IXMLNode;<br><br> function AddChild(Node:IXMLNode;ChildNodeName:WideString):IXMLNode; // 添加一个子节点<br> procedure AddAttribute(Node:IXMLNode;AttributeName,AttributeValue:WideString); // 给节点添加一个属性<br> procedure SetText(Node:IXMLNode;AText:WideString); //给节点设置一个文本值。<br><br> function GetNodeAttributeValue(Node:IXMLNode;AttributeName:WideString):WideString;<br> function GetNodeText(Node:IXMLNode):WideString;<br><br> procedure SaveToFile(APath:string);overload; //XmlDoc的内容保存到文件<br><br> property DocText:string read GetDocText write SetDocText;<br> end;<br><br> TUpdateFileXmlReader=class(TInterfacedObject,IUpdateFileXmlReader)<br> private<br> FUpdateFile,FLocalFile:TBaseXmlReader;<br> FUpdateFilePath,FLocalFilePath:string;<br> public<br> constructor Create(AUpdateFilePath,ALocalFilePath:String);<br> destructor Destroy;override;<br><br> procedure ChannelFiles(PlayType,Channel:string;var List,ListTime,ListURL:TStrings;IsLocal:Boolean=True);<br> procedure ChannelUpdateFiles(PlayType,Channel:string;var List,ListTime,ListURL:TStrings);<br> procedure AddFile(PlayType,Channel:string;FileName:string;URLFileName:string;LastModifyTime:string);<br> procedure FileUrls(PlayType,Channel,FileName:string;var URLs:TUrlCollection);<br> //procedure Reload;<br> end;<br><br> TTopicFileXmlReader=class(TBaseXmlReader,ITopicFileXmlReader)<br> public <br> procedure AllPlayTypes(List:TStrings);<br> procedure ChannelsOfPlayType(PlayTypeName:String;List:TStrings);<br> end;<br><br>//-------------------------------------------------------------<br> TUrlItem=class(TCollectionItem)<br> protected<br> procedure AssignTo(Dest: TPersistent);override;<br> public<br> Text,Src:String;<br> Position:TRect;<br> Font:TFont;<br> BkColor:TColor;<br> constructor Create(Collection: TCollection);override;<br> destructor Destroy;override;<br> end;<br> TUrlItemClass=class of TUrlItem;<br><br> TUrlCollection=class(TCollection)<br> protected<br> procedure ParseUrl(APath:string);virtual;<br> public<br> constructor Create(APath:string);reintroduce;<br> end;<br><br> //---------------------------------------------<br> function ParserColor(C:String):TColor;<br> function PaserToHtml(AText:String):string; //替换掉一些字符,使之成为html格式。<br><br>implementation<br><br>uses StrUtils;<br><br>var<br> FCS:TCriticalSection;<br><br> function PaserToHtml(AText:String):string; //替换掉一些字符,使之成为html格式。<br> begin<br> AText:= StringReplace(AText,'(','<',[rfReplaceAll , rfIgnoreCase]);<br> Result:= StringReplace(AText,')','>',[rfReplaceAll , rfIgnoreCase]);<br> end;<br> // #AAFFCC---->$AAFFCC<br> function ParserColor(C:String):TColor;<br> begin<br> if LeftStr(C,1)='#' then<br> AnsiReplaceStr(C,'#','$');<br> Result:=StrToIntDef(C,clWhite);<br> end;<br><br>{ TBaseXmlReader }<br><br>procedure TBaseXmlReader.AddAttribute(Node: IXMLNode; AttributeName,<br> AttributeValue: WideString);<br>begin<br> FXMLDoc.Active:=True;<br> Node.Attributes[AttributeName]:=AttributeValue;<br>end;<br><br>function TBaseXmlReader.AddChild(Node: IXMLNode;<br> ChildNodeName: WideString): IXMLNode;<br>begin<br> FXMLDoc.Active:=True;<br> Result:=Node.AddChild(ChildNodeName);<br>end;<br><br>procedure TBaseXmlReader.SetText(Node: IXMLNode; AText: WideString);<br>begin<br> FXMLDoc.Active:=True;<br> Node.Text:=AText;<br>end;<br><br>constructor TBaseXmlReader.Create(APath:string);<br>begin<br> FXMLDoc:=TXMLDocument.Create(Application);<br> FPath:=APath;<br> FXmlDoc.LoadFromFile(APath);<br> try<br> FXMLDoc.Active:=True;<br> except<br> //;<br> end;<br> FXMLDoc.Encoding:='GB2312';<br> FXMLDoc.Options:=[doNodeAutoCreate,doNodeAutoIndent ,doAutoSave,doAttrNull];<br> FXmlDoc.ParseOptions:=FXmlDoc.ParseOptions+[poAsyncLoad];<br> if FXMLDoc.IsEmptyDoc then<br> begin<br> FXMLDoc.Active:=False;<br> FXMLDoc.XML.Text:='<?xml version="1.0" encoding="GB2312"?> <Root></Root> ';<br> FXMLDoc.Active:=True;<br> end;<br>end;<br><br>constructor TBaseXmlReader.Create;<br>begin<br> FXMLDoc:=TXMLDocument.Create(Application);<br><br> if FXMLDoc.IsEmptyDoc then<br> begin<br> FXMLDoc.Active:=False;<br> FXMLDoc.XML.Text:='<?xml version="1.0" encoding="GB2312"?><Root></Root> ';<br> FXMLDoc.Active:=True;<br> end;<br> FXMLDoc.Encoding:='GB2312';<br> FXMLDoc.Options:=[doNodeAutoCreate,doNodeAutoIndent ,doAutoSave];<br>end;<br><br>destructor TBaseXmlReader.Destroy;<br>begin<br> FXMLDoc.Active:=False;<br> FXMLDoc:=nil;<br> inherited;<br>end;<br><br>procedure TBaseXmlReader.WriteToXML(APath, AStr: String);<br>begin<br> SetDocText(AStr);<br> SaveToFile(APath);<br> FXmlDoc.LoadFromFile(APath);<br>end;<br><br>procedure TBaseXmlReader.SetDocText(Text: string);<br>begin<br> try<br> FXMLDoc.Active:=False;<br> FXMLDoc.XML.Text:=Text;<br> FXMLDoc.Active:=True;<br> Except<br> MessageDlg('错误的XML文件格式!', mtError,[mbYes],0);<br> Abort;<br> end;<br>end;<br><br>procedure TBaseXmlReader.SaveToFile(APath: string);<br>begin //Encoding NOT Write!!!<br> FXMLDoc.NodeIndentStr:=#32#32;<br> FXMLDoc.Active:=True;<br> FXmlDoc.SaveToFile(APath);<br>end;<br><br>function TBaseXmlReader.GetNodeAttributeValue(Node: IXMLNode;<br> AttributeName:WideString): WideString;<br>begin<br> Result:=Node.Attributes[AttributeName]; //if AttributeName NOT Exist, Return Empty str .<br>end;<br><br>function TBaseXmlReader.GetNodeText(Node: IXMLNode): WideString;<br>begin //if TextNodes NOT Found, Error Occur .<br> Result:=Node.Text;<br>end;<br><br>function TBaseXmlReader.GetDocText: string;<br>begin<br> FXMLDoc.Active:=True;<br> Result:=FXMLDoc.XML.Text;<br>end;<br><br>function TBaseXmlReader.FirstNode: IXMLNode;<br>begin<br> Result:=nil;<br> if root.HasChildNodes then<br> Result:=root.ChildNodes[0];<br>end;<br><br>function TBaseXmlReader.Root: IXMLNode;<br>begin<br> try<br> Result :=FXMLDoc.ChildNodes.Get(1);<br> except<br> Result:=nil;<br> end; //if FirstNode Not Exsit , An Error occur.<br>end;<br><br>procedure TBaseXmlReader.EnumChildNodes(Node: IXMLNode; var Lst: TStrings);<br>var<br> I:Integer;<br>begin<br> for I:=0 to Node.ChildNodes.Count-1 do<br> Lst.Add(Node.ChildNodes.NodeName)<br>end;<br><br>function TBaseXmlReader.FindNode(ParentNode: IXMLNode;<br> tagName: string): IXMLNode;<br>begin<br> if ParentNode=nil then Exit;<br> Result:=ParentNode.ChildNodes.FindNode(tagName);<br>end;<br><br>function TBaseXmlReader.FindNode(ParentNode: IXMLNode; tagName: string;<br> AttributeName1, AttributeValue1, AttributeName2,<br> AttributeValue2: WideString): IXMLNode;<br>var<br> N:IXMLNode;<br>begin<br> Result:=Nil;<br> N:=FindNode(ParentNode,tagName);<br> if N=nil then Exit;<br><br> while N<>nil do<br> begin<br> if (N.NodeName=tagName) AND(N.Attributes[AttributeName1]=AttributeValue1) and (N.Attributes[AttributeName2]=AttributeValue2) then<br> begin<br> Result:=N;<br> Exit;;<br> end;<br><br> N:=N.NextSibling;<br> end;<br>end;<br><br>function TBaseXmlReader.FindNode(ParentNode: IXMLNode; tagName: string;<br> AttributeName, AttributeValue: WideString): IXMLNode;<br>var<br> N:IXMLNode;<br>begin<br> Result:=Nil;<br> N:=FindNode(ParentNode,tagName);<br> if N=nil then Exit;<br><br> while N<>nil do<br> begin<br> //ShowMessage((WideCharToString(PWideChar(WideUpperCase(N.NodeName)))));<br> //ShowMessage(WideUpperCase(N.Attributes[AttributeName]));<br> if ((WideCharToString(PWideChar(WideUpperCase(N.NodeName))))=UpperCase(tagName)) AND (WideUpperCase(N.Attributes[AttributeName])=WideUpperCase(AttributeValue)) then<br> begin<br> Result:=N;<br> Exit;;<br> end;<br><br> //while (N<>nil) and (N.NodeName<>tagName)do<br> N:=N.NextSibling;<br> end;<br>end;<br><br>function TBaseXmlReader.FindNodeNext(Sibling: IXMLNode;<br> tagName: string): IXMLNode;<br>var<br> N:IXMLNode;<br>begin<br> Result:=Nil;<br> N:=Sibling.NextSibling;<br> if N=nil then Exit;<br><br> while N<>nil do<br> begin<br> if UpperCase(N.NodeName)=UpperCase(tagName) then<br> begin<br> Result:=N;<br> Exit;;<br> end;<br> N:=N.NextSibling;<br> end;<br>end;<br><br>function TBaseXmlReader.FindNodeNext(Sibling: IXMLNode; tagName: string;<br> AttributeName1, AttributeValue1, AttributeName2,<br> AttributeValue2: WideString): IXMLNode;<br>var<br> N:IXMLNode;<br>begin<br> Result:=Nil;<br> N:=FindNode(Sibling.ParentNode,tagName);<br> if N=nil then Exit;<br><br> N:=N.NextSibling;<br> while (N<>nil) or (N.NodeName<>tagName) or (N.Attributes[AttributeName1]<>AttributeValue1) or (N.Attributes[AttributeName2]<>AttributeValue2) do<br> N:=N.NextSibling;<br> Result:=N;<br>end;<br><br>function TBaseXmlReader.FindNodeNext(Sibling: IXMLNode; tagName: string;<br> AttributeName, AttributeValue: WideString): IXMLNode;<br>var<br> N:IXMLNode;<br>begin<br> Result:=Nil;<br> N:=FindNode(Sibling.ParentNode,tagName);<br> if N=nil then Exit;<br><br> N:=N.NextSibling;<br> while (N<>nil) or (N.NodeName<>tagName) or (N.Attributes[AttributeName]<>AttributeValue) do<br> N:=N.NextSibling;<br> Result:=N;<br>end;<br><br><br>procedure TBaseXmlReader.EnumChildNodesAttr(Node: IXMLNode;AttrName:string;<br> var Lst: TStrings);<br>var<br> I:Integer;<br>begin<br> for I:=0 to Node.ChildNodes.Count-1 do<br> Lst.Add(WideCharToString(PWideChar(VarToWideStr(Node.ChildNodes.Attributes[AttrName]))));<br>end;<br><br><br>procedure TBaseXmlReader.SaveToFile;<br>begin<br> FXmlDoc.SaveToFile(Self.FPath);<br>end;<br><br><br>{ TTopicFileXmlReader }<br><br>procedure TTopicFileXmlReader.AllPlayTypes(List: TStrings);<br>var<br> N:IXMLNode;<br>begin<br> Assert(Assigned(List),Format('类%S的方法%S的参数%S未赋值。',[Self.ClassName,'AllPlayTypes','List']));<br> List.Clear;<br> N:=Self.FirstNode;<br> if N=nil then Exit;<br> while N<>nil do<br> begin<br> if N.HasAttribute('TypeName') then<br> List.Add(N.Attributes['TypeName']);<br> N:=Self.FindNodeNext(N,'PlayType');<br> end;<br>end;<br><br>procedure TTopicFileXmlReader.ChannelsOfPlayType(PlayTypeName: String;<br> List: TStrings);<br>var<br> N,C:IXMLNode;<br>begin<br> Assert(Assigned(List),Format('类%S的方法%S的参数%S未赋值。',[Self.ClassName,'ChannelsOfPlayType','List']));<br> List.Clear;<br> N:=Self.FirstNode;<br> if N=nil then Exit;<br><br> while N<>nil do<br> begin<br> if N.HasAttribute('TypeName') and (UpperCase(N.Attributes['TypeName'])=UpperCase(PlayTypeName)) then<br> begin<br> C:=nil;<br> if N.HasChildNodes then<br> C:=N.ChildNodes[0];<br><br> while C<>nil do<br> begin<br> if C.HasAttribute('id') then<br> List.Add(C.Attributes['id']);<br> C:=Self.FindNodeNext(C,'txt');<br> end;<br> end;<br><br> N:=Self.FindNodeNext(N,'PlayType');<br> end;<br>end;<br><br>{ TUrlItem }<br><br>procedure TUrlItem.AssignTo(Dest: TPersistent);<br>begin<br> if Dest is TUrlItem then<br> begin<br> TUrlItem(Dest).Text:=Self.Text;<br> TUrlItem(Dest).Src:=Self.Src;<br> TUrlItem(Dest).Position:=Self.Position;<br> TUrlItem(Dest).Font.Assign(Self.Font);<br> end<br> else<br> inherited;<br>end;<br><br>constructor TUrlItem.Create(Collection: TCollection);<br>begin<br> inherited;<br> Font:=TFont.Create;<br> Font.Color:=clRed;<br><br> BkColor:=clWhite;<br><br> Text:='';<br> Position.Left:=0;<br> Position.Top:=0;<br> Position.Right:=0;<br> Position.Bottom:=0;<br><br> Src:='';<br>end;<br><br>destructor TUrlItem.Destroy;<br>begin<br> if Font<>nil then Font.Free;<br> inherited;<br>end;<br><br>{ TUrlCollection }<br><br>constructor TUrlCollection.Create(APath:string);<br>begin<br> inherited Create(TUrlItem);<br> //解析url<br> try<br> ParseUrl(APath);<br> except<br> end; <br>end;<br><br>procedure TUrlCollection.ParseUrl(APath:string);<br>var<br> FXmlReader:TBaseXmlReader;<br> First,Sec:IXMLNode;<br> Item:TUrlItem;<br> //Font:TFont;<br>begin<br> FXmlReader:=TBaseXmlReader.Create(APath);<br> try<br> First:=FXmlReader.FirstNode; //urlinfo Node<br> if First=nil then Exit;<br><br> while First<>nil do<br> begin<br> Item:=TUrlItem(Self.Add);<br> Sec:=FXmlReader.FindNode(First,'Position');<br> if Sec<>nil then<br> begin<br> if Sec.HasAttribute('Left') then<br> Item.Position.Left:=Sec.Attributes['Left'];<br> if Sec.HasAttribute('Top') then<br> Item.Position.Top:=Sec.Attributes['Top'];<br> if Sec.HasAttribute('Width') then<br> Item.Position.Right:=Sec.Attributes['Width'];<br> if Sec.HasAttribute('Height') then<br> Item.Position.Bottom:=Sec.Attributes['Height'];<br> end;<br><br> Sec:=FXmlReader.FindNode(First,'font');<br> if Sec<>nil then<br> begin<br> Item.Font.Size:=DefFontSize;<br> if Sec.HasAttribute('bkcolor') then<br> if ParserColor(Sec.Attributes['bkcolor'])=clwhite then<br> Item.BkColor:=DefBkColor<br> else<br> Item.BkColor:=ParserColor(Sec.Attributes['bkcolor']);<br> if Sec.HasAttribute('Fontcolor') then<br> if (ParserColor(Sec.Attributes['Fontcolor'])=clwhite) or (ParserColor(Sec.Attributes['Fontcolor'])=Item.BkColor) then<br> Item.Font.Color:=DefFontColor<br> else<br> Item.Font.Color:=ParserColor(Sec.Attributes['Fontcolor']);<br> end;<br><br> Sec:=FXmlReader.FindNode(First,'text');<br> if Sec<>nil then<br> if Sec.HasAttribute('data') then<br> Item.Text:=Sec.Attributes['data'];<br><br> Sec:=FXmlReader.FindNode(First,'link');<br> if Sec<>nil then<br> if Sec.HasAttribute('Src') then<br> Item.Src:=Sec.Attributes['Src'];<br><br> First:=FXmlReader.FindNodeNext(First,'URLInfo');<br> end;<br> finally<br> FXmlReader.Free;<br> end;<br>end;<br><br>{ TUpdateFileXmlReader }<br><br>procedure TUpdateFileXmlReader.AddFile(PlayType, Channel, FileName,<br> URLFileName, LastModifyTime: string);<br>var<br> nPlayType,nChannel,nFile:IXMLNode;<br> //I:Integer;<br>begin<br> if Trim(PlayType)='' then Exit;<br> nPlayType:=FLocalFile.FindNode(FLocalFile.Root,'PlayType','TypeName',PlayType);<br> if nPlayType=nil then<br> begin<br> nPlayType:=FLocalFile.Root.AddChild('PlayType');<br> nPlayType.Attributes['TypeName']:=PlayType;<br> end;<br><br> if Trim(Channel)='' then Exit;<br> nChannel:=FLocalFile.FindNode(nPlayType,'Channel','ChannelName',Channel);<br> if nChannel=nil then<br> begin<br> nChannel:=nPlayType.AddChild('Channel');<br> nChannel.Attributes['ChannelName']:=Channel;<br> end;<br><br> if Trim(FileName)='' then Exit;<br> nFile:=FLocalFile.FindNode(nChannel,'File','FileName',FileName);<br> if nFile =nil then nFile:=nChannel.AddChild('File');<br><br> nFile.Attributes['FileName']:=FileName;<br> nFile.Attributes['LastModifyTime']:=LastModifyTime;<br> nFile.Attributes['url']:=URLFileName;<br><br> //FLocalFile.SaveToFile;<br>end;<br><br><br><br>procedure TUpdateFileXmlReader.ChannelFiles(PlayType, Channel: string;<br> var List,ListTime,ListURL: TStrings; IsLocal: Boolean);<br>var<br> xml:TBaseXmlReader;<br> nPlayType,nChannel{,nFile}:IXMLNode;<br> I:Integer;<br>begin<br> if IsLocal then<br> xml:=FLocalFile<br> else<br> Xml:=FUpdateFile;<br><br> List.Clear; ListTime.Clear;ListURL.Clear;<br><br> nPlayType:=xml.FindNode(xml.Root,'PlayType','TypeName',PlayType);<br> if nPlayType=nil then Exit;<br><br> nChannel:=xml.FindNode(nPlayType,'Channel','ChannelName',Channel);<br> if nChannel=nil then Exit;<br><br> for I:=0 to nChannel.ChildNodes.Count-1 do<br> begin<br> if nChannel.ChildNodes.HasAttribute('FileName') then<br> begin<br> List.Add(UpperCase(nChannel.ChildNodes.Attributes['FileName']));<br><br> if nChannel.ChildNodes.HasAttribute('LastModifyTime') then<br> ListTime.Add(UpperCase(nChannel.ChildNodes.Attributes['LastModifyTime']))<br> else<br> ListTime.Add('');<br><br> if nChannel.ChildNodes.HasAttribute('url') then<br> ListURL.Add(UpperCase(nChannel.ChildNodes.Attributes['url']))<br> else<br> ListURL.Add('');<br> end;<br> end;<br>end;<br><br>procedure TUpdateFileXmlReader.ChannelUpdateFiles(PlayType,<br> Channel: string; var List,ListTime,ListURL: TStrings);<br>var<br> oList,oListTime,oListURL,nList,nListTime,nListURL:TStrings;<br> I,index:Integer;<br> oTime,nTime:TDateTime;<br><br> procedure FreeLists;<br> begin<br> FreeAndNil(nListURL);<br> FreeAndNil(nListTime);<br> FreeAndNil(nList);<br> FreeAndNil(oListURL);<br> FreeAndNil(oListTime);<br> FreeAndNil(oList);<br> end;<br>begin<br> oList:=TStringList.Create;<br> oListTime:=TStringList.Create;<br> oListURL:=TStringList.Create;<br> nList:=TStringList.Create;<br> nListTime:=TStringList.Create;<br> nListURL:=TStringList.Create;<br><br> List.Clear;ListTime.Clear;ListURL.Clear;<br> try<br> Self.ChannelFiles(PlayType,Channel,oList,oListTime,oListURL,True);<br> //ShowMessage(oList.Text);<br> Self.ChannelFiles(PlayType,Channel,nList,nListTime,nListURL,False);<br> //ShowMessage(nList.Text);<br> if oList.Count=0 then //not such PlayType OR not such Channel.<br> begin<br> List.Assign(nList);<br> ListTime.Assign(nListTime);<br> ListURL.Assign(nListURL);<br> FreeLists;<br> Exit;<br> end;<br><br> for I:=0 to nList.Count-1 do<br> begin<br> index:=oList.IndexOf(nList.Strings);<br> if index=-1 then //not found.<br> begin<br> List.Add(nList);<br> ListTime.Add(nListTime);<br> ListURL.Add(nListURL);<br> end<br> else //Found ,but Time is New .<br> begin<br> oTime:=StrToDateTimeDef(oListTime.Strings[index],StrToDateTime('2008-1-1 00:00:00')) ;<br> nTime:=StrToDateTimeDef(nListTime.Strings,StrToDateTime('2008-1-1 00:00:00')) ;<br> if oTime <nTime then<br> begin<br> List.Add(nList);<br> ListTime.Add(nListTime);<br> ListURL.Add(nListURL);<br> end;<br> end;<br> end;<br><br> finally<br> FreeLists;<br> end;<br>end;<br><br>constructor TUpdateFileXmlReader.Create(AUpdateFilePath,<br> ALocalFilePath: String);<br>begin<br> FCS.Enter;<br> inherited Create;<br> FUpdateFilePath:=AUpdateFilePath;<br> FLocalFilePath:=ALocalFilePath;<br> FUpdateFile:=TBaseXmlReader.Create(AUpdateFilePath);<br> FLocalFile:=TBaseXmlReader.Create(ALocalFilePath);<br>end;<br><br>destructor TUpdateFileXmlReader.Destroy;<br>begin<br> FreeAndNil(FLocalFile);<br> FreeAndNil(FUpdateFile);<br> FCS.Release;<br> inherited;<br>end;<br><br>procedure TUpdateFileXmlReader.FileUrls(PlayType, Channel,<br> FileName: string; var URLs: TUrlCollection);<br>begin<br><br>end;<br><br>//procedure TUpdateFileXmlReader.Reload;<br>//begin<br>// try<br>// FUpdateFile.FXmlDoc.LoadFromFile(FUpdateFilePath);<br>// FLocalFile.FXmlDoc.LoadFromFile(FLocalFilePath);<br>// except<br>// end;<br>//end;<br><br>initialization<br> FCS:=TCriticalSection.Create;<br>finalization<br> FCS.Free;<br><br>end.<br> <br><br><br>unit WallPaper;<br><br>interface<br><br>uses<br> Classes,Registry, WinProcs, SysUtils,CommCtrl,Graphics,Types,Forms,Dialogs,jpeg;<br><br>type<br> TBeforePlayFile =procedure (Sender:TObject; CurFileName:string) of object;<br> TAfterPlayFile =procedure (Sender:TObject; CurFileName:string) of object;<br><br> TTurnWallPaper=class(TThread)<br> private<br> FPicList:TStrings;<br> FCurPicIndex:Integer;<br> FIsPaused:Boolean;<br> FIsFirst,FIsLast:Boolean;<br> FNext:Boolean;<br> FDelay:Integer;<br> FPath:string;<br> FBeforePlayFile:TBeforePlayFile;<br> FAfterPlayFile:TAfterPlayFile;<br><br> procedure DoPlayPic;<br> procedure DoBeforePlayFile;<br> procedure DoAfterPlayFile;<br> function CheckIndex:Boolean;<br> procedure SetWallpaper(path:string);<br> procedure SetDelay(const Value: Integer);<br> public<br> constructor Create;<br> destructor Destroy;override;<br><br> procedure Play;<br> procedure Pause;<br> procedure PlayNext;<br> procedure PlayPrior;<br><br> procedure Execute;override;<br> procedure AddPic(Path:string);<br> procedure PicList(List:TStrings);<br><br> property IsFirst:Boolean read FIsFirst;<br> property IsLast:Boolean read FIsLast;<br> property IsPause:Boolean read FIsPaused;<br> property Delay:Integer read FDelay write SetDelay;<br><br> property BeforePlayFile:TBeforePlayFile read FBeforePlayFile write FBeforePlayFile;<br> property AfterPlayFile:TAfterPlayFile read FAfterPlayFile write FAfterPlayFile;<br> end;<br><br>function GetDesktopHand: THandle;<br>procedure HideDeskTopIcon;<br>procedure ShowDesktopIcon;<br>procedure JPEG2BMP(JPEGFileName:String);<br><br>type<br> TWallPaperStyle=(wpsTile = 0,wpsCenter,wpsSizeToFit ,wpsXY );<br> <br>procedure SetWallpaperExt(sWallpaperBMPPath : string;nStyle:TWallPaperStyle;nX, nY : integer );<br><br>implementation<br><br><br>procedure JPEG2BMP(JPEGFileName:String);<br>var<br> JpegImage1:TJpegImage;<br> Bit:TBitmap;<br>begin<br> JpegImage1 := TJpegImage.Create;<br> try<br> JpegImage1.LoadFromFile(JPEGFileName);<br> Bit:=TBitmap.Create;<br> try<br> Bit.Width := JpegImage1.Width ;<br> Bit.Height := JpegImage1.Height ;<br> Bit.Canvas.StretchDraw(Rect(0,0,Bit.Width,Bit.Height),JpegImage1);<br> Bit.SaveToFile(ChangeFileExt(JPEGFileName,'.BMP'));<br> finally<br> Bit.Free;<br> end;<br> finally<br> JpegImage1.Free;<br> end;<br>end;<br><br>procedure SetWallpaperExt(sWallpaperBMPPath : string;nStyle:TWallPaperStyle;nX, nY : integer );<br>var<br> reg : TRegIniFile;<br> s1 : string;<br> X, Y : integer;<br>begin<br> reg := TRegIniFile.Create('Control Panel/Desktop' );<br><br> with reg do<br> begin<br> s1 := '0';<br> X := 0;<br> Y := 0;<br> case nStyle of<br> wpsTile : s1 := '1';<br> wpsCenter: nStyle := wpsTile;<br> wpsXY :<br> begin<br> nStyle := wpsTile;<br> X := nX;<br> Y := nY;<br> end;<br> end;<br> WriteString( '','Wallpaper',sWallpaperBMPPath );<br> WriteString( '','TileWallpaper',s1 );<br> WriteString( '','WallpaperStyle',IntToStr( Integer(nStyle) ) );<br> WriteString( '','WallpaperOriginX',IntToStr( X ) );<br> WriteString( '','WallpaperOriginY',IntToStr( Y ) );<br> end;<br> reg.Free;<br> SystemParametersInfo(SPI_SETDESKWALLPAPER,0,Nil,SPIF_SENDWININICHANGE );<br>end;<br><br>procedure HideDeskTopIcon;<br>var<br> hdesktop:thandle;<br>begin<br> hDesktop := FindWindow('ProgMan',nil) ;<br> showwindow(hdesktop,SW_HIDE)//显示桌面。<br>end;<br><br>procedure ShowDesktopIcon;<br>var<br> hdesktop:thandle;<br>begin<br> hDesktop := FindWindow('ProgMan',nil) ;<br> showwindow(hdesktop,SW_SHOW)//显示桌面。<br>end;<br><br>function GetDesktopHand: THandle;<br>begin<br> Result:=FindWindow('progman',nil);<br> Result:=GetWindow(Result,GW_Child);<br> Result:=GetWindow(Result,GW_Child);<br>end;<br><br>{ TTurnWallPaper }<br><br>procedure TTurnWallPaper.AddPic(Path:string);<br>var<br> temp:Boolean;<br>begin<br> temp:=FIsPaused;<br> if not FIsPaused then Pause;<br> FPicList.Add(Path);<br> FCurPicIndex:=FPicList.Count-1;<br> FNext:=True;<br> DoPlayPic;<br> if not temp then Play;<br>end;<br><br>function TTurnWallPaper.CheckIndex:Boolean;<br>begin<br> if FPicList.Count=0 then<br> begin<br> FCurPicIndex:=-1;<br> Result:=False;<br> Exit;<br> end;<br><br> if FNext then<br> begin<br> if FCurPicIndex>=FPicList.Count then<br> FCurPicIndex:=0;<br> {else if FCurPicIndex<=-1 then<br> FCurPicIndex:=FPicList.Count-1; }<br> end<br> else<br> begin<br> if FCurPicIndex<=-1 then<br> FCurPicIndex:=FPicList.Count-1;<br> end;<br> Result:=True;<br>end;<br><br>constructor TTurnWallPaper.Create;<br>begin<br> inherited Create(True);<br> FPicList:=TStringList.Create;<br> FIsPaused:=True;<br> FIsFirst:=True;<br> FIsLast:=True;<br> FCurPicIndex:=-1;<br> Self.Priority:=tpLower;<br> Self.FreeOnTerminate:=False;<br> FNext:=True;<br> FDelay:=15000;<br>end;<br><br>destructor TTurnWallPaper.Destroy;<br>begin<br> FPicList.Free;<br> inherited;<br>end;<br><br>procedure TTurnWallPaper.DoAfterPlayFile;<br>begin<br> if Assigned(FAfterPlayFile) then FAfterPlayFile(Self,FPath);<br>end;<br><br>procedure TTurnWallPaper.DoBeforePlayFile;<br>begin<br> if Assigned(FBeforePlayFile) then FBeforePlayFile(Self,FPath);<br>end;<br><br>procedure TTurnWallPaper.DoPlayPic;<br>begin<br> if CheckIndex then<br> begin<br> FPath:=FPicList[FCurPicIndex];<br> if not FileExists(FPath) then Exit;<br> if ((UpperCase(ExtractFileExt(FPicList[FCurPicIndex])))='.JPG') or (UpperCase(ExtractFileExt(FPicList[FCurPicIndex]))='.JPEG') then<br> begin<br> FPath:=ChangeFileExt(FPicList[FCurPicIndex],'.BMP');<br> JPEG2BMP(FPicList[FCurPicIndex]);<br> end;<br><br> Synchronize(DoBeforePlayFile);<br> SetWallpaper(FPath);<br> Synchronize(DoAfterPlayFile);<br> end;<br>end;<br><br>procedure TTurnWallPaper.Execute;<br>var<br> I:Integer;<br>begin<br> while not Self.Terminated do<br> begin<br> while not Self.Suspended do<br> begin<br> if Self.Terminated then Exit;<br> FNext:=True;<br> Inc(FCurPicIndex);<br> DoPlayPic;<br> if Self.Terminated then Exit;<br> if Self.Suspended then Break;<br> Application.ProcessMessages;<br><br> for I:=0 to FDelay div 500 do<br> begin<br> if (not Self.Suspended) and (not Self.Terminated) then Sleep(500);<br> if Self.Terminated then Exit;<br> //if not Self.Suspended then Break;<br> Application.ProcessMessages;<br> end;<br> if Self.Terminated then Exit;<br> //if not Self.Suspended then Break;<br> Application.ProcessMessages;<br> end;<br> Sleep(300);<br> Application.ProcessMessages;<br> end;<br>end;<br><br>procedure TTurnWallPaper.Pause;<br>begin<br> Self.Suspend;<br> FIsPaused:=True;<br>end;<br><br>procedure TTurnWallPaper.PicList(List: TStrings);<br>var<br> temp:Boolean;<br>begin<br> temp:=FIsPaused;<br> Pause;<br> FPicList.Assign(List);<br> FCurPicIndex:=-1;<br> if List.Count>0 then PlayNext;<br><br> if not temp then Play;<br>end;<br><br>procedure TTurnWallPaper.Play;<br>begin<br> Self.Resume;<br> FIsPaused:=False;<br>end;<br><br>procedure TTurnWallPaper.PlayNext;<br>begin<br> FNext:=True;<br> Inc(FCurPicIndex);<br> DoPlayPic;<br>end;<br><br>procedure TTurnWallPaper.PlayPrior;<br>begin<br> FNext:=False;<br> Dec(FCurPicIndex);<br> DoPlayPic;<br>end;<br><br>procedure TTurnWallPaper.SetDelay(const Value: Integer);<br>begin<br> if Value<=6 then<br> FDelay:=6<br> else<br> FDelay := Value;<br>end;<br><br>procedure TTurnWallPaper.SetWallpaper(path: string);<br>begin<br> SetWallpaperExt(path,wpsSizeToFit,0,0);<br>end;<br><br>end.