?
Solved

dbl-clicking on file open my program

Posted on 2003-03-25
13
Medium Priority
?
172 Views
Last Modified: 2010-04-04
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
Comment
Question by:coderdude
[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
  • 5
  • 5
  • 2
  • +1
13 Comments
 
LVL 1

Accepted Solution

by:
Knighty earned 340 total points
ID: 8208446
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
 
LVL 7

Expert Comment

by:Motaz
ID: 8208455
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
 

Author Comment

by:coderdude
ID: 8212908
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.

 
LVL 1

Expert Comment

by:Knighty
ID: 8213493
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
 
LVL 1

Expert Comment

by:Knighty
ID: 8213519
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
 
LVL 1

Expert Comment

by:Knighty
ID: 8213529
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
 
LVL 7

Expert Comment

by:Motaz
ID: 8215524
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
 

Author Comment

by:coderdude
ID: 8222157
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
 

Author Comment

by:coderdude
ID: 8222399
btw, since i'm asking more questions i'll increase the points
0
 
LVL 1

Expert Comment

by:Knighty
ID: 8225020
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
 

Author Comment

by:coderdude
ID: 8227708
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
 
LVL 4

Expert Comment

by:nestorua
ID: 8230001
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
 

Author Comment

by:coderdude
ID: 8247003
didn't work =/

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

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
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 this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
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
Course of the Month12 days, left to enroll

752 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