• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 181
  • Last Modified:

dbl-clicking on file open my program

my program is just missing this option, here's what i'm after:

my program is a text visualization, but i want to create a button that when clicked will associate .txt files to my program and open the file on it

also, if it isn't too much to ask, when the program starts it would check and display a box if it is the default program to open .txt files
0
coderdude
Asked:
coderdude
  • 5
  • 5
  • 2
  • +1
1 Solution
 
KnightyCommented:
register your own filetyp:

uses
  registry, shlobj;

procedure TForm1.RegisterFileType(prefix: string; exepfad: string);
var
  reg: TRegistry;
begin
  reg := TRegistry.Create;
  try
    reg.RootKey := HKEY_CLASSES_ROOT;
    //create a new key  --> .pci
    reg.OpenKey('.' + prefix, True);
    try
      //create a new value for this key --> pcifile
      reg.Writestring('', prefix + 'file');
    finally
      reg.CloseKey;
    end;
    //create a new key --> pcifile
    reg.CreateKey(prefix + 'file');
    //create a new key pcifile\DefaultIcon
    reg.OpenKey(prefix + 'file\DefaultIcon', True);
    //and create a value where the icon is stored --> c:\project1.exe,0
    try
      reg.Writestring('', exepfad + ',0');
    finally
      reg.CloseKey;
    end;
    reg.OpenKey(prefix + 'file\shell\open\command', True);
    //create value where exefile is stored --> c:\project1.exe "%1"
    try
      reg.Writestring('', exepfad + ' "%1"');
    finally
      reg.CloseKey;
    end;
  finally
    reg.Free;
  end;
  SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, nil, nil);
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
  RegisterFileType('pci', 'c:\project1.exe');
end;


and checking what prog got which file extension:
uses Registry;

procedure TForm1.Button1Click(Sender: TObject);
var
  reg: TRegistry;
  keys: TStringList;
  i: Integer;
  typename, displayname, server: string;
begin
  memo1.Clear;
  reg := TRegistry.Create;
  try
    reg.rootkey := HKEY_CLASSES_ROOT;
    if reg.OpenKey('', False) then
    begin
      keys := TStringList.Create;
      try
        reg.GetKeyNames(keys);
        reg.CloseKey;
        {memo1.lines.addstrings(keys);}
        for i := 0 to keys.Count - 1 do
        begin
          if keys[i][1] = '.' then
          begin
            {this is an extension, get its typename}
            if reg.OpenKey(keys[i], False) then
            begin
              typename := reg.ReadString('');
              reg.CloseKey;
              if typename <> '' then
              begin
                if reg.OpenKey(typename, False) then
                begin
                  displayname := reg.ReadString('');
                  reg.CloseKey;
                end;
                if reg.OpenKey(typename + '\shell\open\command', False) then
                begin
                  server := reg.ReadString('');
                  memo1.Lines.Add(Format('Extension: "%s", Typename: "%s", Displayname:"%s"' +
                                         #13#10'  Server: %s',
                                         [keys[i], typename, displayname, server]));
                  reg.CloseKey;
                end;
              end;
            end;
          end;
        end;
      finally
        keys.Free;
      end;
    end;
  finally
    reg.Free
  end;
end;
0
 
MotazCommented:
Put this code in your project, in any event you want, for example call this procedure on MainForm's OnCreate event

// Add Registry to uses clause, the post this code:

procedure RegisterTxt;
var
 Regs: TRegistry;
begin
 Regs:= TRegistry.Create;
 Regs.RootKey:= HKEY_CLASSES_ROOT;

 Regs.OpenKey('\.txt', True);
 Regs.WriteString('', 'YourProject.Data');
 Regs.OpenKey('\YourProject.Data\Shell\Open\Command', True);
 Regs.WriteString('', ParamStr(0) + ' %1');

 (*** Select an Icon for .txt files ***)
 Regs.OpenKey('\YourProject.Data\DefaultIcon', True);
 Regs.WriteString('', ParamStr(0) + ',0'); // First Icon in exe
 Regs.CloseKey;
 Regs.Free;

end;


then your program should use ParamStr(1) in onCreate to check what is received file (Double clicked) to open it, example:

OpenMyFile(ParamStr(1));

OpenMyFile is your procedure to open that file


Motaz
0
 
coderdudeAuthor Commented:
thanks for the comments

both ways works great

but i get the same problem on both, if the filename (ex: readme.txt) has a space like this: read me.txt, i get a error, it just opens files without space, can you help me on this?
0
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.

 
KnightyCommented:
you getting the filename as a parameter for your program, when there is a space in the filename then its in ParamStr(1) and ParamStr(2)

procedure TForm1.FormCreate(Sender: TObject);
var filename:String; i:integer;
begin
  filename:='';
  if ParamCount > 1 then
  begin
  if i>1 then filename := filename + ' ';
  for i:=1 to ParamCount do filename := filename + ParamStr(i);
  end;
end;

s.th. like this should give you the whole filename, not testet :)
0
 
KnightyCommented:
sry, this is right

procedure TForm1.FormCreate(Sender: TObject);
var filename:String; i:integer;
begin
 filename:='';
 if ParamCount > 0 then
 begin
 if i>1 then filename := filename + ' ';
 for i:=1 to ParamCount do filename := filename + ParamStr(i);
 end
 else
 begin
 //no parameter
 end;
end;
0
 
KnightyCommented:
again a little mistake *g*

procedure TForm1.FormCreate(Sender: TObject);
var filename:String; i:integer;
begin
filename:='';
if ParamCount > 0 then
begin
for i:=1 to ParamCount do
begin
if i>1 then filename := filename + ' ';
filename := filename + ParamStr(i);
end;
end
else
begin
//no parameter
end;
end;
0
 
MotazCommented:
You can put " between any file name that include spaces:

Regs.WriteString('', ParamStr(0) + ' "%1"');

then you can deal it as one parameter:

ParamStr(1) will return Read Me.txt


Motaz
0
 
coderdudeAuthor Commented:
knighty,

i tried your answer and now the program don't show the text of the file open, it opens the program but the file don't show up, what could be the error?

i'm sorry if i'm asking too much questions, but i tried alot before asking and can't fix it
0
 
coderdudeAuthor Commented:
btw, since i'm asking more questions i'll increase the points
0
 
KnightyCommented:
you got the filename in the String and you can open it now with Memo or so.

how do you open the file in your program? can you be more precisely
0
 
coderdudeAuthor Commented:
i'm using richedit

what i meant was, if i double-click a txt file named readme.txt it opens correctly and shows whats writen inside of it normally, but if i double-click a file named read me.txt i get an error because of the space, now if i open the same file (read me.txt) using the openfile dialog inside the program i get no error, is it more clear now?

i'm sorry, english is not my native language
0
 
nestoruaCommented:
HI, coderdude,
Go now to Tools/Folder options/File Types.
Find in "Extensions" .txt.
Go to "Advanced/Open/Edit.." and change the program name from notepad.exe to YourProgrameNameWithFullPath
(don't forget %1 in the end).  
Does it work properly now? I mean the way you want to?
Sincerely,
Nestorua.
0
 
coderdudeAuthor Commented:
didn't work =/

anyway, thanks for all the replys, sorry i can't give points to all of you
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 5
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now