Solved

Drag/Dropping files onto program

Posted on 2002-03-19
5
621 Views
Last Modified: 2012-06-21
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
Comment
Question by:JStan
  • 3
5 Comments
 
LVL 6

Expert Comment

by:kotan
Comment Utility
You can convert it to short file name by ExtractShortPathName() and then compare it.
0
 

Author Comment

by:JStan
Comment Utility
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
 

Author Comment

by:JStan
Comment Utility
ooops ;-)
0
 
LVL 1

Accepted Solution

by:
mgazza earned 200 total points
Comment Utility
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
 

Author Comment

by:JStan
Comment Utility
this is what i needed.. thanx,
jim
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now