·  Start ·  Programme ·  Codes ·  Tipps ·  ActiveX ·  Tutorials · 





Many programmers want to realize their own file-extensions, i.e.: *.jkop . It's no problem ! The simple way is to change settings in the registry and register the new fileextension.
And of course you can open existing files (bmp, jpg, jpeg, gif, doc...) with your program - if your tool can read and load this filetypes.
 

Overview:

General
Registry (common)
Registry (particular)
Command ( )
Sampleproject
Collection

 
 General


It is easy, to create your own extensions and register them. It is recommended, to make a backup of the registry before you manipulate it.

You can associate any file with your program, but your tool should be able to handle the associated file.

In the next step i will tell you more about the registry.

 

 Registry (common)

If you have your own Functions to read values and set values and keys in the registry, you can go to the next step.

In the attachment of this tutorial (download), you will find ein sample project with a module, which contains all needed functions to manipulate the registry. You can use this functions in other projects too.

Events in chronologic order:

1) Check, whether the key and value is set or not
2) If the value was deleted by the user or by another program, you must set the key and value again.
3) Use the Command() - Function, to handle the associated file.

 

 Registry (particular)

I will show you now the right keys and values in the registry:

Own Extensions:

Just an sample - we try to register our own extension "xsc". It is a default Setting by Windows, to use only extensions with 3 chars, but you can use more too (i.e.: *.ffghu)
Try to make an extension, where you can be sure, it is an unique extension. Otherwise your program get troubles. If you register your own extension "bmp", and it is a textfile, the user cannot open really Bitmaps with your program.

The registry-key:

The right key for extensions is

        HKEY_CLASSES_ROOT

In this registry-key we set / make a new key, named ".xsc".

       
 

The default value for this new key is "xscfile". You can use any other value.

Note:
Browse through the Key "HKEY_CLASSES_ROOT". You will find here all extensions, which was registered by the system or by any installed programs.

In the next step, we must register our name "xscfile".
We make a new Key, named:

        HKEY_CLASSES_ROOT\xscfile

Subkeys:

Only a few steps more...
We need 3 keys, to finish our work.

We have to make new subkeys for the key "xscfile".
At first, we make the key "shell". For this key we have to make the subkey "open" and at last the subkey "command". Only the key "command" need a value !
The value is the path and name of your program. In example: "C:\Programs\MyApp\First.exe" % 1


       

Don't forget the chars "% 1" at the end of the path. More in the sampleproject...

 


 Command ( )

It is a simple Visual-Basic-Function: Command()
This we need to evaluate the string, in our sample it is the path and name of the file, we want to open.
i.e.: "C:\MyFiles\sample.xsc"

All you need is to write the following lines in the Form_Load-Event:
Dim cmdline
    cmdline = Command()
    RichTextBox1.LoadFile cmdline
How can this work ?

We have registered our own fileextension in the registry - you remember ?
If the user want to open this  file (with extension xsc - i.e.: sample.xsc), he have to click on the file (doubleclick, single click, enter...). Now the system open the associated program (our EXE). If we do not have the command()-function in our Form_Load (or in Sub Main), our tool will be opened, but nothing else. Otherwise the file can be shown in our program, i.e. textbox, richtextbox or picturebox...

Note:

Of course you can realize more than one command-string !
If you want to open frmMain with "A" and frmAbout with "B", just do this:
The calling is "C:\programs\yourtool.exe A"
Sub Main()

    Dim cmdLine As String
    cmdLine = Command()
    Select Case cmdLine
        Case "A"
        frmMain.Show
        Case "B"
        frmabout.Show
        Case Else
        frmMain.Show
        MsgBox "System-Error"
    End Select
  
End Sub
In this sample, we started from Sub Main (in a module)

More Extensions:

Many other programs like "Paint Shop Pro" or "Adobe Photo Shop" etc., can associate more than one file with their program.
Of course you can do that too.

       

 

 Sampleprojekt

Now we have a sample and I want to demonstrate, how it works:

At first register the fileextension. We do that in den Event "Form_Load".
Private Sub Form_Load()
On Error Resume Next
Call Settings
Call RegistryCheck

Dim cmdLine
    cmdLine = Command()
    RichTextBox1.LoadFile cmdLine
    Me.Caption = "X-SCR - " & cmdLine
End Sub
In this event we call "Command()" and we submit the Params to the Richtextbox. If it is not the right filetype (i.e.: *.wav renamed as *.xsc), you get an runtime-error.

Before you call Command, you should check the registry: Is my own fileextension registered or not ?
We do this in an external function (RegistryCheck):
Sub RegistryCheck()
  'Read Registry-value

On Error Resume Next
 
    Dim Datei As String
    Dim Datei2 As String
    Dim Pfad As String
    Pfad = App.Path & "\" & App.Title & ".exe"
    Pfad = Chr(34) & Pfad & Chr(34) & " %1"
    Datei = ".xsc"
    Datei2 = ".xscfile"
    If GetValue(HKEY_CLASSES_ROOT, "xscfile\shell\open\command", _
    "", Pfad) Then
 
        Exit Sub
 
    Else
 
        Dim a&, b&, c&, d&
        Dim XscWert As String
        XscWert = "xscfile"
 
        a = CreateKey(HKEY_CLASSES_ROOT, _
            ".xsc", "")
        b = SetValue(HKEY_CLASSES_ROOT, _
            ".xsc", "", XscWert)
        c = CreateKey(HKEY_CLASSES_ROOT, _
            "xscfile\shell\open\command", "")
        d = SetValue(HKEY_CLASSES_ROOT, _
            "xscfile\shell\open\command", "", Pfad)
 
    End If

End Sub
 

 Collection

This Tutorial is only a base for you. You can check the right path of your program in the registry: If the user move your program into an other folder, your tool will not work right. So you should check the path too. You can download here a sample project with an reusable module.

DownloadSampleprojekt  ( 7,58 KB )
Aktualisiert: 12.02.2008, 20:49 Uhr Copyright © 2001 - 2010 by ST-software Navigation zurück  |  Navigation vorwärts  |  Zum Seitenanfang     
Ihre IP: 38.107.191.82 ·  Seite erstellt in: 0.306 Sekunden ·  Dateigröße:  12977 Bytes