Search results for 'Delphi'. 123 post(s) found.

  1. 2009/09/09 Is there directory selection VCL component in Delphi ?
  2. 2009/09/09 How to launch application ?
  3. 2009/09/09 How to print external document ?
  4. 2009/09/03 Delphi API to get windows temporary directory
  5. 2009/09/03 Delphi API to get the current working directory
  6. 2009/09/03 How to resize PNG file in Delphi ?
  7. 2009/08/22 Get the length of arrays and a string
  8. 2009/08/22 Delphi Pointer Types
  9. 2009/08/21 How to read/wrtie INI file in Delphi ?
  10. 2009/08/19 How to get file created time, modified time, and last accessed time ?
2010/04/14 13:28

Thread Application Implementation in TThread


Delphi provides class for thread application as TThread.

Following is the simple example for thread application.

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    GroupBox1: TGroupBox;
    seTimeToWork: TSpinEdit;
    Label1: TLabel;
    btnCreate: TButton;
    btnTerminate: TButton;
    procedure FormShow(Sender: TObject);
    procedure btnCreateClick(Sender: TObject);
    procedure btnTerminateClick(Sender: TObject);

  private
    FThread:TThread;
    procedure EnableButtons;
    procedure OnTerminate(Sender:TObject);
end;

TMyThread=class(TThread)
    private
      FTimeToWork:integer;
    protected
      procedure Execute;override;
    public
      constructor Create(TimeToWork:integer);
end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

// TMyThread

constructor TMyThread.Create(TimeToWork: integer);
begin
  FTimeToWork:=TimeToWork;
  inherited Create(True);
end;

procedure TMyThread.Execute;
var
  T:Integer;
begin
  t:=FTimeToWork;
  Form1.Memo1.Lines.Add('Begin execution');
  while not Terminated and (t>0) do
  begin
    Form1.Memo1.Lines.Add(format('Remaining %5.2f%%',[t/FTimeToWork*100]));
    Sleep(500);
    dec(t,500);
  end;

  if Terminated then
    Form1.Memo1.Lines.Add('Terminated by user');

  Form1.Memo1.Lines.Add('Finish execution');
end;

// TForm1
procedure TForm1.EnableButtons;
begin
  btnCreate.Enabled:=not Assigned(FThread);
  btnTerminate.Enabled:= Assigned(FThread);
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  EnableButtons;
end;

procedure TForm1.btnCreateClick(Sender: TObject);
begin
  FThread:=TMyThread.Create(seTimeToWork.Value);
  FThread.OnTerminate:=OnTerminate;
  EnableButtons;
  FThread.Resume;
end;

procedure TForm1.btnTerminateClick(Sender: TObject);
begin
  FThread.Terminate;
end;

procedure TForm1.OnTerminate(Sender: TObject);
begin
  FThread:=nil;
  EnableButtons;
end;

end.




Trackback 0 Comment 0

Trackback : Cannot send a trackbact to this post.

2009/09/09 08:03

Is there directory selection VCL component in Delphi ?


There are FileOpenDialog and FileSaveDialog components on Delphi VCL.
Sometimes you may need Directory Selection dialog, but no components are found on VCL.
Do you know Delphi provides the solution as SelectDirectory().

Here are two examples for Directory Selection.

Following shows directory structure under d:\delphi
procedure TForm1.Button1Click(Sender: TObject);
var
  DirSelected: string;
begin
  if SelectDirectory('Select a folder:', 'D:\Delphi', DirSelected) then
    ShowMessage('You selected ' + DirSelected)
  else
    ShowMessage('You did not select a folder');
end;


Following shows driver names and files as well as directory structure.
procedure TForm1.Button2Click(Sender: TObject);
var
  Dir: string;
begin
  Dir := 'D:\Delphi';
  if SelectDirectory(Dir, [sdAllowCreate, sdPerformCreate, sdPrompt], 0) then
    ShowMessage('You selected ' + Dir)
  else
    ShowMessage('You did not select a folder');
end;

Trackback 0 Comment 0

Trackback : Cannot send a trackbact to this post.

2009/09/09 07:56

How to launch application ?


You can launch application on windows by ShellExecute function. Following is the simple example to launch application.

use ShellAPI;

.
.
.

ShellExecute(Handle, 'open', PChar('c:\test\app.exe'), nil, nil, SW_SHOW);


In case of running DOS command application such as batch file and you want not to display user, put SW_HIDE in the function option as follow

ShellExecute(Handle, 'open', PChar('c:\test\kurapa_backup.bat'), nil, nil, SW_HIDE);

Trackback 0 Comment 0

Trackback : Cannot send a trackbact to this post.

2009/09/09 07:53

How to print external document ?


Without complex programming, you can Print document by ShellExecute function.

use ShellAPI;

.
.
.

ShellExecute(Handle, 'Print', PChar('c:\test\test.doc'), nil, nil, SW_SHOW);
Trackback 0 Comment 0

Trackback : Cannot send a trackbact to this post.

2009/09/03 16:07

Delphi API to get windows temporary directory


When you need to access windows temp directory, you can get from environment variables.
Following is the example to Get Temporary Directory.

var
  m_tempdir: string

.
.
.

  m_tempdir := GetEnvironmentVariable('TEMP');


Trackback 0 Comment 0

Trackback : Cannot send a trackbact to this post.