Implementation of 7.4 DDE server program
The DDE server program responds to a DDE client's request, which generally contains the data that the client program would like to obtain.
To create a DDE server program, you must add a ddeserveritem part to the form. The Ddeserveritem text or lines property contains the data that you want to join. Generally, Ddeserveritem parts are associated with another text control. Updates the value of the Ddeserveritem text or lines property when the content in the text control changes. The following procedure links ddeserveritem to a list box. This connection is implemented in the OnChange event of the list box.
Procedure Form1.onlistboxchange (Sender:tobject);
Begin
Ddeserveritem1.lines: = Listbox1.items;
End
When you create a DDE server program, you can also add a ddeserverconv part and connect the two parts using the Ddeserveritem serverconv attribute. At this point, the DDE topic becomes the name of the part Ddeserverconv, not the title of the Ddeserveritem part form.
It is necessary to use the Ddeserverconv part in the following situations:
1. The title that owns the Ddeserveritem part form may change at run time or it may have other forms that have the same title. In this case, the DDE join may not be established;
The 2.DDE client program may send a macro command to your server program. In this case, you have only one ddeserverconv part to respond to the Onmacroexecute event and perform the appropriate action.
7.4.1 and DDE Client establish joins
Generally, establishing a DDE join is a task for a client program. However, the server program can copy a join to the clipboard for the client to paste and establish a DDE session. The steps are as follows:
1. Invoke the CopyToClipboard method of the Ddeserveritem part to copy the value of the text (or lines) property and the DDE join information to the Clipboard;
2.DDE Client inserts the joined data. Generally this is done by selecting the appropriate command (e.g. edit| Paste Special or edit| Paste Link) to achieve.
7.4.2 Response to DDE events
Part Ddeserverconv has three events: OnOpen, OnClose, Onexecutemacro. The first two events are triggered when a DDE session is established and terminated. With the introduction of (7.3.7).
The Onexecutemacro event is used in response to a macro directive sent by a client program. The Onexecutemacro Event process has a MSG parameter that saves the sent command string. In this procedure, users can decide how to respond to these macro directives.
The Ddeserveritem part has only one event onpokedata. This event is used in response to data sent by the client program. If the client program is a Delphi program, the client program invokes the Pokedata or Pokedatalines method. During the processing of this event, the user can save the data sent to a suitable place. Generally this should be the text control that Ddeserveritem is connected to.
The following program saves the data sent to the listbox.
Procedure Form1.onddeserveritempokedata (Serder:tobject);
Begin
Listbox1.items: = Ddeserveritem1.lines;
End
7.4.3 DDE Server Application Routines
Below we create a DDE server routine and a corresponding DDE client routine.
The work that a DDE server routine can accomplish is:
1. Copy the DDE connection information to the clipboard for use by other programs;
2. Use of a TMemo component to provide data sources for other programs;
3. Receive the data sent by the client program;
4. According to the client program to send the macro instructions to change their running state.
The key properties for each of these components are as follows:
Ddesrvrform.activecontrol = Memo1
Ddesrvrform.menu = MainMenu1
Bevel1.align = Altop
Memo1.align = Alclient
Ddetestitem.serverconv = Ddetesttopic
By setting the Bevel1, Memo1 align Properties, you can ensure that the window size changes can still have a more beautiful screen display.
Memo1 is the server's data source, and the DDE project part Ddetestitem links Memo1 through Memo1 onchange events.
Procedure Tddesrvrform.doonchange (Sender:tobject);
Begin
If not Finpoke then
Ddetestitem.lines: = Memo1.lines;
End
Where Finpoke is a boolean-type private data member that is used to flag whether a program is sending data in a client program. When the data is sent by the customer to the data source, there is no need to pass the data to the DDE project part.
Copy the join information to the Clipboard, simply call the Ddetestitem CopyToClipboard method.
Procedure Tddesrvrform.copyclick (Sender:tobject);
Begin
Ddetestitem.copytoclipboard;
End
This is through the menu item edit| Copy to invoke.
The data sent by the receiving client is in the process of Ddetestitem onpokedata event processing. Changes the value of the Finpoke during the receive process to prevent invalid data from being sent back.
Procedure Tddesrvrform.doonpoke (Sender:tobject);
Begin
Finpoke: = True;
Memo1.lines: = Ddetestitem.lines;
Finpoke: = False;
End
Processes the macro directives sent by the customer during the Onexecutemacro event processing of the DDE session part Ddetesttopic. We have defined five kinds of macro directives that can be responded to: Copydde, clear, Ws_normal, ws_minimized, ws_maximized, respectively, for copying the join information, clearing the contents of the Memo1, and changing the display state of the window.
Procedure Tddesrvrform.domacro (Sender:tobject; Msg:tstrings);
Var
cmd:string;
I:integer;
Begin
CMD: = ';
If Msg.count = 0 then Exit;
For I: = 0 to Msg.count-1 do
Begin
CMD: = Msg.strings[i];
If uppercase (CMD) = ' Copydde ' Then
Ddetestitem.copytoclipboard
else if uppercase (CMD) = ' Clear ' Then
Memo1.text: = '
else if uppercase (CMD) = ' Ws_normal ' Then
WindowState: = Wsnormal
else if uppercase (CMD) = ' ws_minimized ' Then
WindowState: = wsminimized
else if uppercase (CMD) = ' ws_maximized ' Then
WindowState: = wsmaximized
Else
Messagedlg (' Invalid Command ', mtwarning,[mbok],0);
End
End
The following DDE client is designed primarily to validate the DDE server program above, but it also provides a complete example of a DDE client program.
The program saves the DDE data received in a TMemo class part ddedat, and the data and macro instructions that you want to send to the server are entered in another TMemo class part Pokedat. Two buttons pokebtn, executebtn for sending data and macro directives. Two menu Items file| New Link and edit| Paste link is used to create a new join based on user input and to paste a DDE join from the Clipboard.