# Some useful Delphi functions
To increase the time and color of the Hint when mouse is on an element:
~~~
procedure TMainForm.FormCreate(Sender: TObject);
begin
Application.HintColor := clLime;
//Application.HintPause := 2000;
Application.HintHidePause := 9000;
end;
~~~
# String finding
~~~
Function GetStrBetween(Const s, startMarker, endMarker: string): string;
var pStart,pEnd : Integer; //Returns The first occurence of a string between 2 markers
begin
pStart := Pos(startMarker,s)+length(startMarker);
pEnd := Pos(endMarker,s);
end;
~~~
Usage: Memo1.Text:=GetStrBetween(indexfile,'
','');
# Replace as in STR_Replace:
~~~
function Replace(Dest, SubStr, Str: string): string;
var
Position: Integer;
begin
while Pos(SubStr, Dest)>0 do
begin
Position:=Pos(SubStr, Dest);
Delete(Dest, Position, Length(SubStr));
Insert(Str, Dest, Position);
end;
Result:=Dest;
end;
~~~
# Trimming many chars (all specified) from anywhere in a string:
~~~
function TrimChar(Dest, chars: string): string;
var
Position,i: Integer;
begin
for i:=0 to length(chars) do
begin
Position:=Pos(chars[i], Dest);
Delete(Dest, Position, 1);
end;
Result:=Dest;
end;
~~~
Usage: TrimChar('ABCDEF','BCD') // will give 'AEF'
# Showing about box
ShowMessage('This is a demonstration'+#13#10+'Press OK to close window');
#Changing file creation time/date
~~~
procedure TMainForm.ChangeFileDate1Click(Sender: TObject);
var newDateTime : TDateTime;
FileHandle : Integer;
begin
// newDateTime := StrToDateTime('01/01/2000 12:34:56'); on win10 is with -
newDateTime := (FileDateToDateTime(FileAge(FileListBox1.FileName)));
newDateTime:=StrToDateTime(InputBox('Set Time and Date', 'New Date and time for '+
FileListBox1.FileName, DateTimeToStr(newDateTime)));
FileHandle := FileOpen(FileListBox1.FileName, fmOpenWrite or fmShareDenyNone);
if FileHandle > 0 then
FileSetDate(FileHandle, DateTimeToFileDate(newDateTime))
else
ShowMessage('Cant open file for setting date');
FileListBox1Click(nil);
end;
~~~
# Starting any application/document from windows
ShellExecute(0, nil, 'index.html',PChar(FileListBox1.FileName), nil, SW_SHOWNORMAL);
ShellExecute(0, nil, 'notepad.exe',PChar(FileListBox1.FileName), nil, SW_SHOWNORMAL);
# Deleting a file with confirmation
~~~
procedure TMainForm.DeletefileMenuClick(Sender: TObject);
begin
if messagedlg('Delete file '+#13#10+#13#10+
FileListBox1.FileName+ #13#10+#13#10+'Forever ?',mtConfirmation , mbYesNoCancel , 0)=mrYes then
if deletefile(FileListBox1.FileName)
then StaticText1.Caption:='Deleted';
// FileListBox1.Update;
FileListBox1Enter(nil);
end;
~~~
# Save as dialog:
~~~
procedure TEditorForm.Saveas1Click(Sender: TObject);
var
newfilename : string;
begin
newfilename:= extractfilename (MainForm.FileListBox1.FileName);
repeat
if not InputQuery('Save As', 'New filename',newfilename )then exit;
until (not FileExists(newfilename)) or (MessageDlg('File Exists, Overwrite ?',mtError, mbOKCancel, 0)=1);
RichEdit1.Lines.SaveToFile(newfilename);
ShowMessage('Saved as:'+newfilename);
end;
~~~
# simulate a keypress Like CTRL+Z
~~~
// Simulating a Ctrl+Z keystroke (for undo last typing)
keybd_event(VK_CONTROL,157,0 , 0); // Ctrl Press
keybd_event(VkKeyScan('Z'),158,0 , 0); // ?Z’ Press
keybd_event(VkKeyScan('Z'),158, KEYEVENTF_KEYUP,0); // ?Z’ Release
keybd_event(VK_CONTROL,157,KEYEVENTF_KEYUP,0); // Ctrl Release
~~~