Features - License - Installation - TModuleSwitcher - TCommandReceiver - Author
Two components enabling interprocess messaging in a simple an effective way. Define Text commands that you may send from one application to another. The commands are received automatically with 2 lines of code and fire an event in the destination application. You have the option to add parameters to each command, and be able to react appropriatly in the fired event in the destination process. If a destination application isn't launched yet, this is done automatically. If the destination application is already running, your can optionally avoid the start of a second instance. In the destination application, this makes no difference.
The package comes with 2 components:
interface uses Switchmodules ; type TYourForm = class (TForm) CommandReceiver : TCommandReceiver ; private { Private-Deklarationen } { ADD THIS LINE } procedure WMCopyData(var Msg: TWMCopyData); message WM_COPYDATA; ... end ; implementation { ADD THIS PROCEDURE } procedure TYourForm.WMCopyData(var Msg: TWMCopyData); begin CommandReceiver.ForwardMessage (Msg) ; end ; end. |
Two techniques are used for receiving the commands. First is by interpreting the startup paramaters (when the destination application is first launched), of by receiving a windows message (if the application is already running and a second instance shell be avoided.
The components are freeware, means you may also use in commercial applications. But the copyright stays at the author, and you must give a notice in your product saying that you use the components. Freeware means for free, of course, but if you really like the components, you are welcome to pay for it. I've put a lot of time in it, and some money surely would help me ane encouraging me to continue writing components and other things that I may share with you. The contact information you find at the end of the document.
Extract the ZIP to a new folder. Open the package SwitchModulePackage.dpk, compile it and install it. If all works fine, you mighrt find 2 new components in the "Tom" palette. Don't forget to add the installationpath to the browsing path of your library.
Here now comes the detailed description of the two components.
This is the sending part of the package.
Items: TModuleCollection
This is the main part of the component. Add an item and fill its following properties:
Caption | The caption that is displayed in the menuitem and, if you want, on the speedbutton. |
Classname | Each window of every running application in your windows
has a defined classname. Each process may consist of several forms, of
course. To find a window in Windows, you have to search options. The one
is by it's classname. A new delphi application, for example, has a mainform
at startup, and this one is called initially Form1. It's a class, and
the classname is named to TForm1. And thats the name you may search for.
Give it a unique name, to make identification possible, by setting the
Name property of the form in the Objectinspector. |
Command | Each message you send to a destinative application must be identified by this. Therefore, each item needs a command, which is simple text, with no spaces. |
Filename | Thats the full path to the destination application, to enable starting it, if not found by its formclass or -caption |
Formcaption | The second method, for searching a form in windows. See the Classname property for more on this. |
FreeOnClose | When you close your application, you may define by setting this property to false, that the running destination application is also closed. |
Hint | A hint text for the speedbutton and menuitem |
ImageList | Any imagelist on your form, where the application icon is inserted, when you call the Run method of the ModuleSwitcher. Define the width and height properties in this imagelist to have the speedbutton icon in the desired size. |
MenuItem | The linked menuitem. Beware, all settings of this menuitem are overwritten |
MultipleInstances | Allow, or allow not, multiple instances of the destination
program. If you disallow, commands are sent to it by using the Windows messaging
system. There is a slight bug in here: If you allow multiple instances, and choose to close the instances by closing your application (FreeOnClose is set to false), then not all running instances are found and closed. |
SearchMode | Find a window either by using its Classname (smByClassname) or by its formcaption (smByFormCaption) |
ShowCaption | If true, the caption is also shown in the speedbutton. This is not always desired. So switch it to false, if you don't need it |
SpeedButton | The pointer to any available speedbutton |
Values | Predefines parameters that are sent with the command, when clicking or calling the Execute method. |
Each item can call an Execute method, to launch the new item, respectively switch to it, if already running. Normally, you don't need to call this method manually, since the assigned Speedbutton and/or menuitem do this automatically, when being clicked.
More, each item has its own event OnValues, which is fired when you call the Execute method (or by using the speedbutton or menuitem automatically). This gives you the option to fill the parameters of each command during runtime. In the event, fillup the values: TStringlist, which you get as a method variable. Best practice is probably, to use the values property of the TStringList, to assign directly a value to a parameter. (I know, this is rather confusing, so here is a simple example:
procedure TForm1.ModuleSwitcher1Items0Values(Sender: TObject; Values: TStringList); begin Values.values['DBUSERNAME'] := 'yourname' ; Values.values['DBPASSWORD'] := 'password' ; end; |
Back to the properties of the TModuleSwitcher itself:
Messagename
Type any text. You need to set the same in the TCommandReceiver component to
make both modules communicate between each other.
Run
Call this, and all your speedbuttons, menuitems etc. for each item is filled.
Best way to do is call this in the OnShow event of your mainform.
That's it for this part of the package... Here now comes the other side of the moon, to speak in Pink Floyd's words. Sending alone is not enough, you need a receive part in the foreign application... And that's:
And here comes the receiving part, the antenna, if you want.
Messagename
Well, see above five lines above. This must be the same as in the ModuleSwitcher
component of the sending application.
And again, you define the Items in a similar way than in TModuleSwitcher. But this part is even easier because there are not many things to set.
Command | The same that is sent by any sending application. If the command is identified by this item, then the defined event OnCommand is fired. |
OnCommand
When receiving a command, and this command
is identified by the item, so this event is fired. It has as a parameter the
additionally parameters. These can be read (with values.values...
see above for an example).
Now to understand the logic of the component. This does only identify a sent message, and fires the event and feed you with possible parameters. What you do with this, is up to you. This belongs to your program logic, and I'm not responsible for that.
Well, to active the CommandReceiver in your application, it needs 2 things. First is, to call the Run method. Best is to do this in the OnShow Event of your form where you place your component. And second, you need to prepare the receiving of windows messages and forward them to the CommandReceiver. This has to be done, because in Windows, only Forms can receive messages, and not the components on a form. Sorry for this, but thats Windows. I'm not responsible for that... Let's see here a simple example, what you need to do:
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, SwitchModules; type TForm1 = class(TForm) CommandReceiver1: TCommandReceiver; procedure ModuleSwitcher1Items0Values(Sender: TObject; Values: TStringList); procedure FormShow(Sender: TObject); private { Private declarations } procedure WMCopyData(var Msg: TWMCopyData); message WM_COPYDATA; public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.ModuleSwitcher1Items0Values(Sender: TObject; Values: TStringList); begin Values.values['USERNAME'] := 'yourname' ; end; procedure TForm1.FormShow(Sender: TObject); begin CommandReceiver1.Run ; end; procedure TForm1.WMCopyData(var Msg: TWMCopyData); begin CommandReceiver1.ForwardMessage (Msg) ; end; end. |
Oh... what a huge component description... Hope it helps, if not, here come the contact details of the author:
Tom Peiffer... Mail me at tom.peiffer@tp-net.lu. Or contact me via ICQ #2207717. That should be enough.
Have fun.
Tom