Board index » delphi » What am I missing here?

What am I missing here?

Hi

The tiny program below is supposed to take
the contents from a textfile dropped upon the
non-activated program, then save a .bak file,
then strip all " from the contents, and finally
save the stripped contents to a .csv file and
close down.

Unfortunately, in real life the form pops up,
bur nothing more happens.

What am I missing???

 best regard
Arne

*** strip.dpr ***

program strip;

uses
  Forms,
  {*word*49}en in '{*word*49}en.pas' {stripForm};

{$R *.res}

begin
  Application.Initialize;
  Application.CreateForm(TstripForm, stripForm);
  Application.Run;
end.

*** {*word*49}en.pas ***

unit {*word*49}en;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TstripForm = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  procedure WMDropFiles(var Msg:TWMDropFiles); message WM_DROPFILES;
  procedure DoTheJob(const AFileName:string);

  public
    { Public declarations }
  end;

var
  stripForm: TstripForm;

implementation
uses ShellAPI;
{$R *.dfm}

procedure TstripForm.Button1Click(Sender: TObject);

var AFilename:string;

begin
  if OpenDialog1.Execute then
    begin
      AFileName := OpenDialog1.Filename;
      DoTheJob(AFileName);
    end;
    Close;
end;

procedure TstripForm.WMDropFiles(var Msg: TWMDropFiles);
var
  FileName: array[0..254] of char;

begin
  try
    if DragQueryFile(Msg.Drop, 0, FileName, MAX_PATH) > 0 then
      begin
        DoTheJob(FileName);
        Msg.Result := 0;
      end;
  finally
    DragFinish(Msg.Drop);
  end;
end;

procedure TstripForm.DoTheJob(const AFileName:string);

var
  sekfil, bakfil, csvfil : string;
  ch : char;
  InFile, OutFile : TextFile;

begin
  sekfil := AFileName;
  bakfil := ChangeFileExt (sekfil, '.bak');
  csvfil := ChangeFileExt (sekfil, '.csv');
  AssignFile(InFile, sekfil);
  Reset(InFile);
  AssignFile(OutFile, bakfil);
  Rewrite(OutFile);
  Repeat
    Read(Infile,ch);
    Write(OutFile, ch);
  Until EOF(InFile);
  CloseFile(InFile);
  CloseFile(OutFile);
  AssignFile(InFile, bakfil);
  Reset(InFile);
  AssignFile(OutFile, csvfil);
  Rewrite(OutFile);
  Repeat
    Read(Infile,ch);
    if ord(ch) <> 34 then Write(OutFile, ch);
  Until EOF(InFile);
  CloseFile(InFile);
  CloseFile(OutFile);
end;

procedure TstripForm.FormCreate(Sender: TObject);
begin
  DragAcceptFiles(Handle, true);
end;

end.

*** {*word*49}en.dfm ***

object stripForm: TstripForm
  Left = 192
  Top = 110
  Width = 235
  Height = 129
  Caption = 'strip'
  Color = clCream
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
end

 

Re:What am I missing here?


Sorry - a minor correction is called for:

procedure TstripForm.Button1Click(Sender: TObject);
actually has no function in the program - the essential
part is the file-dropping.

best regard
Arne

Re:What am I missing here?


Quote
Arne Eckmann wrote:
> Hi

> The tiny program below is supposed to take
> the contents from a textfile dropped upon the
> non-activated program, then save a .bak file,
> then strip all " from the contents, and finally
> save the stripped contents to a .csv file and
> close down.

> Unfortunately, in real life the form pops up,
> bur nothing more happens.

> What am I missing???

>  best regard
> Arne

> *** strip.dpr ***

> program strip;

> uses
>   Forms,
>   {*word*49}en in '{*word*49}en.pas' {stripForm};

> {$R *.res}

> begin
>   Application.Initialize;
>   Application.CreateForm(TstripForm, stripForm);
>   Application.Run;
> end.

> *** {*word*49}en.pas ***

> unit {*word*49}en;

> interface

> uses
>   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
>   Dialogs, StdCtrls;

> type
>   TstripForm = class(TForm)
>     procedure FormCreate(Sender: TObject);
>     procedure Button1Click(Sender: TObject);
>   private
>     { Private declarations }
>   procedure WMDropFiles(var Msg:TWMDropFiles); message WM_DROPFILES;
>   procedure DoTheJob(const AFileName:string);

>   public
>     { Public declarations }
>   end;

> var
>   stripForm: TstripForm;

> implementation
> uses ShellAPI;
> {$R *.dfm}

> procedure TstripForm.Button1Click(Sender: TObject);

> var AFilename:string;

> begin
>   if OpenDialog1.Execute then
>     begin
>       AFileName := OpenDialog1.Filename;
>       DoTheJob(AFileName);
>     end;
>     Close;
> end;

> procedure TstripForm.WMDropFiles(var Msg: TWMDropFiles);
> var
>   FileName: array[0..254] of char;

> begin
>   try
>     if DragQueryFile(Msg.Drop, 0, FileName, MAX_PATH) > 0 then
>       begin
>         DoTheJob(FileName);
>         Msg.Result := 0;
>       end;
>   finally
>     DragFinish(Msg.Drop);
>   end;
> end;

> procedure TstripForm.DoTheJob(const AFileName:string);

> var
>   sekfil, bakfil, csvfil : string;
>   ch : char;
>   InFile, OutFile : TextFile;

> begin
>   sekfil := AFileName;
>   bakfil := ChangeFileExt (sekfil, '.bak');
>   csvfil := ChangeFileExt (sekfil, '.csv');
>   AssignFile(InFile, sekfil);
>   Reset(InFile);
>   AssignFile(OutFile, bakfil);
>   Rewrite(OutFile);
>   Repeat
>     Read(Infile,ch);
>     Write(OutFile, ch);
>   Until EOF(InFile);
>   CloseFile(InFile);
>   CloseFile(OutFile);
>   AssignFile(InFile, bakfil);
>   Reset(InFile);
>   AssignFile(OutFile, csvfil);
>   Rewrite(OutFile);
>   Repeat
>     Read(Infile,ch);
>     if ord(ch) <> 34 then Write(OutFile, ch);
>   Until EOF(InFile);
>   CloseFile(InFile);
>   CloseFile(OutFile);
> end;

> procedure TstripForm.FormCreate(Sender: TObject);
> begin
>   DragAcceptFiles(Handle, true);
> end;

> end.

> *** {*word*49}en.dfm ***

> object stripForm: TstripForm
>   Left = 192
>   Top = 110
>   Width = 235
>   Height = 129
>   Caption = 'strip'
>   Color = clCream
>   Font.Charset = DEFAULT_CHARSET
>   Font.Color = clWindowText
>   Font.Height = -11
>   Font.Name = 'MS Sans Serif'
>   Font.Style = []
>   OldCreateOrder = False
>   PixelsPerInch = 96
>   TextHeight = 13
> end

Your dfm file misses the following line:

   OnCreate = FormCreate

In the code you posted the FormCreate is never fired, so the window is
not registered for accepting dropped files.

(You could have established this by putting a breakpoint on the line
with "DragAcceptFiles(Handle, true);")

hth
Kees de Kooter

Re:What am I missing here?


"Kees de Kooter" <k...@boplicity.nl> wrote:

Quote
> Your dfm file misses the following line:

>    OnCreate = FormCreate

> In the code you posted the FormCreate is never fired, so the window is
> not registered for accepting dropped files.

Thanks a lot!

Now it is working this way:

When a file is dropped on the non-activated
program, it starts up and displays the form,
and if the file is dropped a second time,
(this time on the form) the job is done.

Now, what would it take to make it do the job
right away, when a file is dropped on the
non-activated program?

best regard
Arne

*** {*word*49}en.dfm ***

object stripForm: TstripForm
  Left = 192
  Top = 110
  Width = 235
  Height = 129
  Caption = 'strip'
  Color = clCream
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
end

Re:What am I missing here?


"Kees de Kooter" <k...@boplicity.nl> skrev:

Quote
> Your dfm file misses the following line:

>    OnCreate = FormCreate

> In the code you posted the FormCreate is never fired, so the window is
> not registered for accepting dropped files.

Thanks a lot Kees!

Now it is working this way:

When a file is dropped on the non-activated
program, it starts up and displays the form,
and if the file is dropped a second time,
(this time on the form) the job is done.

Now, what would it take to make it do the job
right away, when a file is dropped on the
non-activated program?

best regard
Arne

*** {*word*49}en.dfm ***

object stripForm: TstripForm
  Left = 192
  Top = 110
  Width = 235
  Height = 129
  Caption = 'strip'
  Color = clCream
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
end

Re:What am I missing here?


That's easy! I guess you mean when you drag a file onto the program's icon
in explorer? Use this is your oncreate:

procedure TstripForm.FormCreate(Sender: TObject);
begin
  if paramcount>0 then dothejob(paramstr(1));
  DragAcceptFiles(Handle, true);
end;

Quote
> Thanks a lot Kees!

> Now it is working this way:

> When a file is dropped on the non-activated
> program, it starts up and displays the form,
> and if the file is dropped a second time,
> (this time on the form) the job is done.

> Now, what would it take to make it do the job
> right away, when a file is dropped on the
> non-activated program?

> best regard
> Arne

> *** {*word*49}en.dfm ***

> object stripForm: TstripForm
>   Left = 192
>   Top = 110
>   Width = 235
>   Height = 129
>   Caption = 'strip'
>   Color = clCream
>   Font.Charset = DEFAULT_CHARSET
>   Font.Color = clWindowText
>   Font.Height = -11
>   Font.Name = 'MS Sans Serif'
>   Font.Style = []
>   OldCreateOrder = False
>   OnCreate = FormCreate
>   PixelsPerInch = 96
>   TextHeight = 13
> end

Re:What am I missing here?


"Nicholas Sherlock" <n_sherl...@hotmail.com> skrev:

Quote
> That's easy! I guess you mean when you drag a file onto the program's icon
> in explorer? ...

Hi Nicholas - yes, you are right!

Quote
> procedure TstripForm.FormCreate(Sender: TObject);
> begin
>   if paramcount>0 then dothejob(paramstr(1));
>   DragAcceptFiles(Handle, true);
> end;

Thank's a lot Nicholas, it works fine :-)

best regard
Arne

Other Threads