Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 641
  • Last Modified:

Drag/Dropping files onto program

good day all,

i use the following code to open a file that is dropped on my program.
problem is my prog looks to see if file is already open.

if user double clicks on a file.. it is opened normally.
but if user drags/drops file.. the filename is changed to short name (ie: 'program files' becomes 'progra~1').
so now filecompare function sees it as 2 different files.

i would like to either show longfilename as dropped file or alternatively compare short to short.

this code is in a program written in delphi 3

thanx 4 all help..
code follows,
jim

procedure TForm1.WMDropFiles(var Msg: TWMDropFiles);
var
     FileName: array[0..MAX_PATH] of Char;
begin
     try
              if (DragQueryFile(Msg.Drop, 0, FileName, MAX_PATH) > 0) and (FileExists(FileName)) then
               begin
                    if AnsiCompareFileName(somefile,FileName) = 0 then
                         begin
                                            ###file is already open###
                                                       Break;
                                                  end;        
                    Msg.Result := 0;
               end;
     finally
              DragFinish(Msg.Drop);
       end;
end;
0
JStan
Asked:
JStan
  • 3
1 Solution
 
kotanCommented:
You can convert it to short file name by ExtractShortPathName() and then compare it.
0
 
JStanAuthor Commented:
kotan,

i have raised the points to 200..
i could not find ExtractShortPathName() but i did see getshortpathname()..
and it works.. i use the function to ck for dups and all is well.

so here is the new question..
why cant i use getFULLpathname and do the opposite??

i tried but it wouldnt work.
maybe u could ck this code and see what i am doing wrong.
thanks ,
jim

here is the code i used
there is already a string variable that contains the file im comparing to(theOtherFile)
################
procedure TForm1.WMDropFiles(var Msg: TWMDropFiles);
var
  FileName: array[0..MAX_PATH] of Char;
  Buffer: array[0..255] of Char;
  TheFile: pchar;
begin
     try
          if (DragQueryFile(Msg.Drop, 0, FileName, MAX_PATH) > 0) and (FileExists(FileName)) then
          begin
               StrPCopy(Buffer,FileName);
                   GetFullPathName(Buffer,255,Buffer,TheFile);
                   MainFile := Buffer;
               if AnsiCompareFileName(MainFile,theOtherFile) = 0 then
                    IsOpen := True;
               else
                    IsOpen := False;
                   Msg.Result := 0;
              end;
       finally
              DragFinish(Msg.Drop);
       end;
end;
################
0
 
JStanAuthor Commented:
ooops ;-)
0
 
mgazzaCommented:
here is the full program for a drag and drop laurnch pad:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls,buttons, ExtCtrls, Grids, Outline, FileCtrl,ShellAPI;

type
  TForm1 = class(TForm)
    FileListBox1: TFileListBox;
    Panel1: TPanel;
    procedure savenotes(Sender: TObject);
    procedure formcreate(Sender: TObject);
  private
    { Private declarations }
    procedure AddBtn( fname : string );
    procedure BtnClick(Sender: TObject);
    procedure RespondToMessage(var Msg: Tmsg; var Handled: Boolean);
  public
    { Public declarations }
    procedure files;
    function ExecuteFile(const FileName, Dir : string ) : THandle;
end;
const
  BUFFLEN = 255;
type
  CHARARRAY = array[0..BUFFLEN] of char;
var
  Form1: TForm1;

implementation
var
  ButtonList: TStringList;
{$R *.DFM}


procedure tform1.files;
var
i:integer;
begin
if  FileExists('files.txt') then
begin
filelistbox1.visible:=true;
panel1.visible:=true;
filelistbox1.items.LoadFromFile('files.txt');

for i := 0 to (filelistbox1.items.Count-1) do
AddBtn(filelistbox1.items.Strings[i])
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
filelistbox1.items.savetoFile('files.txt');
end;
procedure TForm1.formcreate(Sender: TObject);
begin
DragAcceptFiles(Form1.Handle, true);
ButtonList := TStringList.Create;
Application.OnMessage := RespondToMessage;
end;
function tform1.ExecuteFile(const FileName, Dir : string ) : THandle;

var
  ntFileName, ntDir : CHARARRAY;
begin
  Result := ShellExecute(Application.MainForm.Handle,
  nil,
  StrPCopy(ntFileName, FileName),
  nil,
  StrPCopy(ntDir,Dir),
  SW_SHOW);
end;
procedure TForm1.AddBtn( fname : string );
{ Create a new application launching button, complete with glyph
  and label. Add it to the Form }
var
  lplicon  : word;         {!! required argument to ExtractAssociatedIcon }
  buffer   : CHARARRAY;    { PChar version of the filename fname          }
begin
  lplicon := 0;
  StrPCopy(buffer,fname);
  begin  { if FileExists }
             {!! get icon for EXE file or of application associated with file }
     Icon.Handle := ExtractAssociatedIcon(HInstance, buffer, lplicon);
     ButtonList.AddObject(fname, TBitBtn.Create(Self));
     with TBitBtn(ButtonList.Objects[ButtonList.Count-1]) do
     begin
        with Glyph do
        begin
          width := 32;
          height := 32;
          if Icon.Handle <> 0 then
             Canvas.Draw(0,0,Icon);
        end;
              { set the size, font etc. of the BitBtn itself  }
        width := 60;
        layout := blGlyphTop;
        font.name := 'Arial';
        font.size := 7;
        caption := ExtractFileName(fname);
        Align := alleft;
        Parent := Form1.Panel1;          { put the BitBtn onto the form       }
        OnClick := BtnClick;      { set BitBtn's OnClick event-handler }
     end;
  end;
  end;
procedure TForm1.BtnClick(Sender: TObject);
var
   filename: string;
   retval   : word;
begin
  filename := ButtonList.Strings[ButtonList.IndexOfObject(Sender)];
  retval := ExecuteFile(filename, ExtractFilePath(filename));
end;

procedure TForm1.RespondToMessage(var Msg: Tmsg; var Handled: Boolean);
{ Iterate through all file names if a multi-file selection was dropped }
const
  FileIndex : Cardinal = Cardinal(-1);   { return a count of dropped files }
var                                      { $FFFF 16-bit;  $FFFFFFFF 32-bit }
  buffer : CHARARRAY;
  fname : string;
  fnum  : word;
begin
   if Msg.Message = WM_DROPFILES then
   begin
      for fnum := 0 to DragQueryFile(Msg.WParam, FileIndex, NIL, BUFFLEN)-1 do
      begin
         DragQueryFile(Msg.WParam, fnum, buffer, BUFFLEN);
         fname  := StrPas(buffer);
         AddBtn(fname);
         filelistbox1.Items.Add(fname);
      end;
    DragFinish(Msg.WParam);
    Handled := True;
   end;
end;



end.
0
 
JStanAuthor Commented:
this is what i needed.. thanx,
jim
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now