Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Drag/Dropping files onto program

Posted on 2002-03-19
5
Medium Priority
?
638 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 6

Expert Comment

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

Author Comment

by:JStan
ID: 6887401
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
ID: 6887408
ooops ;-)
0
 
LVL 1

Accepted Solution

by:
mgazza earned 800 total points
ID: 6974784
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
ID: 6975048
this is what i needed.. thanx,
jim
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

636 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