📄 fileassociation_demo_v2.pas
字号:
{+------------------------------------------------------------
| Unit FileAssociation_Demo_V2
|
| Version: 1.0 Created: 14.03.99
| Last Modified: 21.03.99
| Environment : Delphi 4.02, tested on Win95B
| Author : P. Below
| Project: Sample applications
| Description:
| This is a simple demo application that shows how to register
| an application as server for a file extension (.TED in this
| case) and how to use DDE to open files from Explorer in
| an existing instance of the program.
| A file association requires, at minimum, the following keys
| in the registry under HKEY_CLASSES_ROOT (HKCR):
|
| HKCR\<extension> = <filetype>
| HKCR\<filetype> = <description>
| HKCR\<filetype>\shell\open\command = <application> "%1"
|
| "open" is one of the standard verbs, others that may be used
| are "edit", "print", and "printto". If all verbs are implemented
| by the same application command line switches may be used
| to differentiate the action to take in the command key string.
| See the entry for rtffile in regedit.exe for an example.
|
| If only the three keys above are present Explorer will open
| a new instance of the application for each file. To get it to
| use an existing instance one needs to make the application into
| a DDE server and add some more keys to the registry:
|
| HKCR\<filetype>\shell\open\ddeexec = <macrostring>
| HKCR\<filetype>\shell\open\ddeexec\topic = <topicname>
| HKCR\<filetype>\shell\open\ddeexec\application = <DDE Servername>
|
| If a TDDEServerConv object is used to implement the DDE server
| then <topicname> is the name of the TDDEServerConv component
| and <DDE Servername> is the applications filename, without
| path and extension.
|
| To test this application copy a number of textfiles (e.g. PAS
| files) to extension TED and open them in Explorer. Note that
| you have to run the application once manually to get it to
| register itself.
+------------------------------------------------------------}
Unit fileassociation_demo_V2;
Interface
Uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, DdeMan, ComCtrls;
Type
TForm1 = Class(TForm)
PageControl1: TPageControl;
Procedure FormCreate(Sender: TObject);
Procedure TEDDdeServerExecuteMacro(Sender: TObject; Msg: TStrings);
Private
Procedure AddFileeditor(Const filename: String);
{ Private declarations }
Public
{ Public declarations }
End;
Var
Form1: TForm1;
Implementation
Uses ShlObj, Associations;
{$R *.DFM}
{$R TEDICON.RES}
{+------------------------------------------------------------
| Procedure TForm1.FormCreate
|
| Event : OnCreate
| Used by : the form
| Call method: static
| Visibility : published
| Description:
| On form creation we create editors for any file that
| may have been passed on the commandline and attach
| a macro handler to the DDE request handler the
| Associations unit created for us.
| Note that the commandline processing code needs to be
| changed if command line switches are used to select
| between different actions!
| Error Conditions: none
| Created: 14.03.99 by P. Below
+------------------------------------------------------------}
Procedure TForm1.FormCreate(Sender: TObject);
Var
i: integer;
Begin
AttachExecuteMacrohandler(TEDDdeServerExecuteMacro);
For i:= 1 To ParamCount Do
AddFileeditor( ParamStr( i ));
End; { TForm1.FormCreate }
{+------------------------------------------------------------
| Procedure TForm1.TEDDdeServerExecuteMacro
|
| Event : OnExecuteMacro
| Used by : TEDDdeServer
| Call method: static
| Visibility : published
| Description:
| This method is called when the DDE server receives a macro
| request. In this case the request will always be a single
| line but the code is able to deal with several macros rolled
| into one request, as long as the macros are separated by
| line breaks.
| Error Conditions: none
| Created: 14.03.99 by P. Below
+------------------------------------------------------------}
Procedure TForm1.TEDDdeServerExecuteMacro(Sender: TObject; Msg: TStrings);
Var
filename: String;
i, n: Integer;
Begin
If Msg.Count > 0 Then Begin
For i := 0 To Msg.Count-1 Do Begin
filename := Msg[i];
If Pos('[Open(', filename) = 1 Then Begin
n:= Pos('"', filename );
If n > 0 Then Begin
Delete( filename, 1, n );
n:= Pos('"', filename );
If n > 0 Then
Delete( filename, n, maxint );
AddFileeditor( filename );
End; { if }
End; { If }
End; { For }
End; { if }
End; { TForm1.TEDDdeServerExecuteMacro }
{+------------------------------------------------------------
| Procedure TForm1.AddFileeditor
|
| Parameters :
| filename: full pathname of a file to view
| Call method: static
| Visibility : private
| Description:
| Creates a new tabsheet in the pagecontrol and a TMemo on
| this tabsheet. The file is loaded into the memo and the
| tabsheet is made active.
| Error Conditions:
| Exceptions may result if the file is not found or is not
| a textfile.
| Created: 14.03.99 by P. Below
+------------------------------------------------------------}
Procedure TForm1.AddFileeditor( Const filename: String );
Var
tab: TTabSheet;
memo: TMemo;
Begin
tab := TTabsheet.Create(self);
tab.Pagecontrol := pagecontrol1;
tab.caption := Extractfilename( filename );
memo := TMemo.Create( tab );
memo.Align := alClient;
memo.Parent := tab;
memo.lines.LoadFromFile( filename );
Pagecontrol1.ActivePage := tab;
End; { TForm1.AddFileeditor }
Const
Extension = '.TED';
Filetype = 'TEDFile';
Initialization
If not FiletypeIsRegistered( Extension, Filetype ) Then Begin
RegisterFiletype(
Extension, Filetype, 'TED File', 'open', '"%1"' );
RegisterDDEServer(
Filetype, 'open', '[Open("%1")]' );
RegisterFileIcon(
Filetype, ParamStr(0), 1 );
ShChangeNotify( SHCNE_ASSOCCHANGED, 0, Nil, Nil );
End
Else
ShowMessage('Filetype is registered');
End.
{
object Form1: TForm1
Left = 192
Top = 128
AutoScroll = False
Caption = 'Form1'
ClientHeight = 373
ClientWidth = 632
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -13
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
PixelsPerInch = 120
TextHeight = 16
object PageControl1: TPageControl
Left = 0
Top = 0
Width = 632
Height = 373
Align = alClient
TabOrder = 0
end
end
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -