There are lots of code for delphi, but everybody has to start somewhere with the basics.
Some of us are past the Basics and want to know more codes for Delphi. But not just codes to work within Delphi, we want codes that work with Windows, the registry, and files and folders. We may even want to run DOS commands.
Index
- Basic Knowledge
- Open Windows Programs
- Open Files with Programs
- Open Folders
- Run DOS Commands
- Writing to text files
- Reading from text files
- Hiding the Close button
- Hide/Show Application in Taskbar
Basic Knowledge - Always when we work with code which refers to some external command, we have to specify to Delphi what it is. To do this open up the code section, and at the top of the code, you will see 'uses'. Here Delphi is told which your services your program will use.
- // means that a comment is to follow and delphi will not execute it as script. You can type anything here.
Open Windows Programs Under 'uses', put 'ShellApi'
For example, if you click a button, notepad must open. Put this code in the button's procedure:
ShellExecute(Handle, 'open', 'c:\Windows\notepad.exe', nil, nil, SW_SHOWNORMAL) ;
Open Files with Programs uses ShellApi;
ShellExecute(Handle,'open', 'c:\windows\notepad.exe','c:\SomeText.txt', nil, SW_SHOWNORMAL) ;
Open Folders
uses ShellApi;
ShellExecute(Handle,'open', 'c:\DelphiDownload', nil, nil, SW_SHOWNORMAL) ;
Run DOS Commands
uses ShellApi;
Run a DOS command and return close Command Prompt:
ShellExecute(Handle, 'open', PChar('command.com'), PChar('/c copy file1.txt file2.txt'), nil, SW_SHOW);
Run a DOS command and keep the Command Prompt Window Open:
ShellExecute(Handle, 'open', PChar('command.com'), PChar('/k dir'), nil, SW_SHOW);
Writing to Text Files
Suppose you want to type something into an editbox, and then you want to save it as a .txt file when you click on a button.
In the button's code:
var
F: TextFile;
begin
AssignFile(F, 'C:\Test\Data.txt'); //Open a new file
Rewrite(F); //Will be creates if non-existing WriteLn(F, Edit1.Text); //Write text in editbox
WriteLn(F, Edit2.Text); //Write text in second editbox CloseFile(F); //Close the File
end;
Reading from Text Files
procedure TForm1.btnReadClick(Sender: TObject);
var
F: TextFile;
S: string;
begin
if FileExists('C:\Test\Data.txt') then begin
AssignFile(F, 'C:\Test\Data.txt');
Reset(F);
ReadLn(F, S);
Edit1.Text := S;
ReadLn(F, S);
Edit2.Text := S;
CloseFile(F);
end
else
ShowMessage('File C:\Test\Data.txt not found');
end;
Hiding the Close buttonThere is a very easy way to do this without programming it. This is usefull for a splash-screen.
- Click on your form
- Look in the Object Inspector, under 'Border Icons'
- Select False in 'biSystemMenu' dropdown window
- Run your app and you will see that there isn't a close button!
- Press Alt-F4 to exit
Hide/Show Application in Taskbar ShowWindow(Application.Handle, SW_HIDE);
ShowWindow(Application.Handle, SW_SHOW);
The Message Box Sometimes it is inconveniant to have to display something on a label. ShowMessage is a very neat way to do this:
ShowMessage('type your message here');
The inputbox If you want to quickly provide a name or password, but haven't got the space to use a editbox, the inputbox command is perfect. Here's an example:
var
example :string;
procedure TForm1.Button1Click(Sender: TObject);
begin
example:=inputbox('The caption of the inputbox', 'The prompt', 'The Default input');
end;
The Registry
1.Reading from the registry
In this example we'll tell delphi to show us were the windows wallpapers are hidden
uses Registry;
var
reg :TRegistry;
begin
Reg:=TRegistry.Create; //Create an instance of the registry
Reg.RootKey:=HKEY_LOCAL_MACHINE; //Set the root key
Reg.OpenKey('Software\Microsoft\Windows\CurrentVersion', False); //Open the key, the last paramater indicates //that if the key doesn't exist, it should be //created. In this case, false
ShowMessage( Reg.ReadString ('WallPaperDir')); //Show it in messagebox
Reg.Free; //Always free your registry afterwards
end;
2.Writing to the registry
In this example we'll write our name into the registry
uses registry;
var
reg :TRegistry;
begin
Reg:=Tregistry.Create;
Reg.Rootkey:=HKEY_LOCAL_MACHINE;
Reg.Openkey('DelphiCoding\MyInfo', True); //Note that we set it to true, meaning we WANT to create the path
Reg.WriteString ('MyName',InputBox('Write to Reg:','What''s your name?',''));
// ! !
// The key name The Data (whatever you put into the inputbox)
reg.free;
end;