参数是目录名,搜索扩展名(以,或;间隔的串,比如‘jpg,bmp,gif;psd’)是否搜索子目录标志,传出值是一个列表 procedure SearchFileInFolder(const FolderName , SearchAttr : string ; const SubFolderFlag : boolean; out FileList : TStringList); var FindFile: TSearchRec; AttrList: TStringList; s1 , s2: string; count : integer; begin AttrList := TStringList.Create; try //解析扩展名到列表 s1 := SearchAttr; if rightStr(s1,1)<>';' then s1 := s1 + ';'; if (pos(';',s1)>0) or (pos(',',s1)>0) then begin while (pos(';',s1)>0) or (pos(',',s1)>0) do begin count := pos(';',s1); if (pos(',',s1)0) then count := pos(',',s1);
s2 := copy(s1,1,count - 1); if leftStr(s2,1)<>'.' then s2 := '.' + s2; s2 := LowerCase(s2); delete(s1,1,count);
AttrList.Add(s2); end; end else begin s1 := LowerCase(s1); AttrList.Add(s1); end; s1 := '; s2 := '; AttrList.Sort;
//开始查找 s1 := FolderName; if rightStr(s1,1)<>'\' then s1 := s1 + '\'; if DirectoryExists(s1) then begin if FindFirst(s1 + '*.*', faAnyFile, FindFile) = 0 then begin repeat if FindFile.Attr <> faDirectory then begin //比较扩展名 s2 := ExtractFileExt(FindFile.Name); s2 := LowerCase(s2); if AttrList.Find(s2,count) then FileList.Add(s1 + FindFile.Name); end else begin //目录则嵌套自身 if SubFolderFlag and (FindFile.Name <> '.') and (FindFile.Name <> '..') then SearchFileInFolder(s1+FindFile.Name,SearchAttr,SubFolderFlag,FileList); end; until FindNext(FindFile) <> 0; FindCLose(FindFile); end; end; finally AttrList.Free; end; end;
动态建立菜单项目的例子:
procedure TMainForm.AddMenuItemClick(Sender: TObject); var i : integer; s : string; begin if Sender is TMenuItem then begin i := TMenuItem(Sender).Tag - 100; s := GetIni.ReadString(MySection,'OpenedFile' + inttostr(i),'); s := lowerCase(s); if fileExists(s) or (copy(s,1,7)='http://') then begin PlayList.Clear; PlayList.Add(s); PlayIndex := 0; self.Play(PlayList[0]); end; end; end;
一个关于窗体判断对象的例子 :刚进公司的时候学会的第一个东东 procedure TForm1.CheckBox1Click(Sender: TObject); var s:string; ed:Tedit; begin s:=tCheckBox(sender).name; s:=copy(s,length(s),1); if TcheckBox(sender).checked then //TEdit(findComponent('edit'+s)).enabled:=true begin ed:=findComponent('edit'+s) as TEdit; ed.enabled:=true; end else //TEdit(findComponent('edit'+s)).enabled:=false; begin ed:=findComponent('edit'+s) as TEdit; ed.enabled:=false; end
end;
传入外部启动参数的例子:
//外部启动参数 outerParamFlag := false; for i := 0 to ParamCount() do begin if AnsiEndsStr('.swf',ParamStr(i)) then begin if fileExists(ParamStr(i)) then begin PlayList.Add(ParamStr(i)); outerParamFlag := true; end; end else if AnsiEndsStr('.fls',ParamStr(i)) then begin StrReadForFile(ParamStr(i),PlayList); outerParamFlag := true; end; if outerParamFlag then self.Play(PlayList[0]); end;
procedure TtrayForm.WMTrayIcon(var Message:Tmessage); var P:TPoint; begin {message.LParam 判断是否按下鼠标左键、右键、双击} if message.LParam = WM_RBUTTONDOWN then begin //mouse右键 if not(self.Visible) then PMShow.Caption:='显示(&S)' else PMShow.Caption:='隐藏(&H)'; //使主窗体成为当前窗体,否则有时会出现弹出菜单不会消失的情况 SetActiveWindow(Handle); //避免弹出菜单被任务栏挡住的情况 SetForegroundWindow(Handle); // GetCursorPos(P); PopupMenu1.Popup(P.X,P.Y); end; if message.LParam = WM_LBUTTONDOWN then begin //mouse左键
end; if message.LParam = WM_LBUTTONDBLCLK then begin //mouse左键双击
end; end;
procedure TtrayForm.ModifyTrayIcon(dwMessage:DWORD;ico:TIcon;Tip:String); var nid:TnotifyIconData; begin with nid do begin cbSize:=sizeof(TnotifyIconData); //结构大小 Wnd:=Handle; //接收消息的窗口句柄 uID:=0; //自定义tray icon序号 uFlags:=NIF_ICON or NIF_MESSAGE or NIF_TIP; //包含NIF_ICON表示hicon可用 //NIF_MESSAGE表示uCallbackMessage可用 //NIF_TIP表示ssTip可用 uCallbackMessage:=WM_TRAYICON; //消息传递鼠标事件 hIcon:=ico.Handle; //显示在tray的图标 StrPCopy(szTip,Tip); //提示文字,不超过64哥ASCII字符 end; Shell_NotifyIcon(dwMessage,@nid); end;
procedure TtrayForm.FormCreate(Sender: TObject); begin ModifyTrayIcon(NIM_ADD,Application.Icon,'this write title word'); end;
procedure TtrayForm.PMShowClick(Sender: TObject); begin self.Visible:=not self.Visible; end;
procedure TtrayForm.PMExitClick(Sender: TObject); begin self.Close; end;
procedure TtrayForm.MMHideClick(Sender: TObject); begin ModifyTrayIcon(NIM_ADD,Application.Icon,Application.Title); self.Hide; end;
procedure TtrayForm.MMStayOnTopClick(Sender: TObject); begin MMStayOnTop.Checked:=not MMStayOnTop.Checked; if MMStayOnTop.Checked then self.FormStyle:=fsStayOnTop else self.FormStyle:=fsNormal; end;
procedure TtrayForm.FormClose(Sender: TObject; var Action: TCloseAction); begin ModifyTrayIcon(NIM_DELETE,Application.Icon,'this write title word'); Application.Terminate; end;