7.2.1 DDE principle of Windows?
Windows DDE Mechanism Based on Windows Message Mechanism. Two Windows applications perform DDE sessions by communicating DDE messages to each other, thereby completing the request, response, transfer of data. These two applications are called servers and clients, respectively. The server is the provider of data, and the customer is the request and recipient of the data.
The DDE session is started by the client. The client propagates a message (WM_DDE_ITITIATE) to all Windows programs currently running. This message indicates the general data (applications, topics) required for the client program. DDE servers with this data can respond to this transmitted message. At this point, the DDE session begins.
Since the DDE server can support one or more data items in each subject, the application name, theme name, and project name should be specified when requesting data. Applications, topics, projects are three most basic concepts in DDE.
The DDE message provided with Windows itself and the API programming is a fairly tricky problem. Although using the DDE management library (DDEML.DLL) can reduce the work burden of the developer to some extent, the development DDE program is still not a relaxed thing.
At this time, Delphi appeared! Delphi makes it easy and convenient to develop a DDE application with its own clever design.
7.2.2 DDE implementation mechanism for Delphi?
Delphi does all DDE functions in four parts, they are:
● TDDECLIENTCONV: Used for customer programs to establish and maintain a DDE session
● TDDECLIENTITEM: Used for customer program establishing and maintaining data exchange channels
● TDDeServerConv: Server program response DDE session
● TDDeServerItem: Used for server program maintenance data exchange channels
The first two components are used to generate a DDE client, and the latter two components are used to generate a DDE server program. If an application has these components at the same time, this program can act as a DDE customer or act as a DDE server.
The session part TDDECLIENTCONV, TDDeServerConv is used to establish and maintain a DDE session. DDE sessions include DDE services and DDE topics.
The DDE service is the name of the DDE server, that is, the application name that is in the general Windows DDE mechanism. Generally speaking this name is the DDE server application execution file name removes .exe suffix. For example, the DDE service is WINWORD, such as the Word 6.0 established a session. But not too good. For example, your application is to establish a session with Borland ReportSmith (RPTSMITH.exe), the DDE service is Report Smith. DDE service is how, readers can refer to the related DDE server application documentation.
The DDE theme is a data unit that contains link information. Generally speaking, the DDE theme is a full file name including the extension. For example, a DDE session is established with one file in Excel, the topic may be?
Topic = 'C: /EXCEL/EXAMPLE/SALE.XLS'?
If the server is a Delphi application, the subject matter is the title of the data form that is included in the data form. If the server uses the DDeServerConv part, the name of the part DDeServerConv is required as DDE theme.
Project parts TDDECLIENTITEM, TDDeServerItem are used to establish and maintain the transport channel of DDE data. The DDE project contains actual data. The format of the DDE project depends on the DDE server application. A possible DDE project example is the domain in the spreadsheet and the domain in the database table. If the server is a Delphi application, the project is the name of the connected DDeServerItem part.
Delphi's DDE implementation mechanism is convenient and practical, but there is also an unfortunate defect: only the text data and commands, macros can not transmit image data. At this point, the Visual Basic launched by Microsoft is slightly better. However, the use of current data is still the most widely used, and image transfer can be implemented using clipboard and OLE, and this defect has no great impact. ? 7.3 implementation of DDE client?
The DDE client starts the DDE session to request and receive data from the server. At the same time, you can also send data, commands, macros, and change the status of the server and control the run of the server. ?
7.3.1 Single Pattern (ConnectMode)?
Delphi's DDE provides two joint modes: automatic and manual. This can be set by the ConnectMode property of the DDECLINETCONV component. As shown in the following table.
Table 7.5 DDE Joint Pattern
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Value meaning
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Ddeautomatic is automatically established when the window containing the TDDECLIENTCONV component is running.
DDEMANUAL is only established only when the OpenLink method is called
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Different coupling patterns, DDE client implementations are different.
For automatic mode:
1. Add DDECLIENTCONV and DDECLIENTITEM components to the form and name it;
2. Set the DDECLIENTITEM component to the name of the DDECLIENTCONV component;
If you are designed, select by the object viewer; if you establish a connection at runtime, you will set the value of the property by the following statement:
DDECLIENTITEM1.DDECONV: = 'DDECLIENTCONV1';?
3. Connect to the server to achieve data sharing.
For artificial mode:
1. Add DDECLIENTCONV components to the form;
2. Establish a connection with the server;
3. Call the RequestData method to apply and obtain the data when the data is updated. ?
7.3.2 and DDE Server establish contact?
Connect with the DDE server, can be performed during design or during runtime.
At design, the DDE join can be pasted through a clipboard. Specific steps are as follows:
1. Activate the server program and select the data for your client's program to join;
2. Copy the data and DDE connections to the clipboard. Generally speaking, this only needs to select the EDIT | COPY menu of the server application;
3. Select DDECLIENTCONV components in Delphi IDE design form;
4. Click the DDService property or DDETOPIC property in the Object Inspector, and then click the Ellips button to open the DDE INFO dialog;
5. Select the Paste Link button. At this point the App Edit box and the Topic editing box are automatically populated. If the Paste Link button is awesome, you are ready to use as a server, do not support DDE or DDE information is not successfully copied to the clipboard;
6. Select the OK button. At this point, the DDEService in the Object Inspector contains the correct value of the establishment of a DDE join.
It is not required for artificial mode.
7. Select the DDECLIEntItem part and set the DDECONV attribute in the Object Inspector to the DDECLIENTCONV component name that has been completed;
8. If the DDE connection information on the clipboard is still reserved, select the value of the DdeItem property from the Object Inspector drop-down list box. Otherwise, enter the correct value. When running, call the SetLink method to establish a DDE join.
SetLink has two String type parameters, which are used to accept the value of DDService and DDETopic. The DDeService and DDETOPIC properties of the DDECLIENTCONV component are set after execution. It is to be noted that the value directly sets DDEService and DDETOPICs directly at runtime and cannot establish a DDE join, but must call the setLink method for initialization.
For example, the following statements and Excel's SYSTEM topic establish join:?
DDEClieTnconv. SetLink ('Excel', 'System');
After calling the setLink method, you also need to set the DdeItem property of the DDECLIntItem component.
For example, the following statement is coupled to Excel's Topics project to get the file name of the current active file:
DDECLIETNITEM.DDEITEM: = 'TOPICS'
When the DDE is established, the coupled data is saved in the Text and Lines properties of the DDECLIntItem component, and Text is used to save a string (String), LINES is used to save a string table (TSTRINGS) object.
In order to display the coupling data, the value can be assigned to a visual component in the DDECLIntItem's onchange event.
The following event procedure is displayed in the edit box in real time in real time. ?
Procedure form1.ddeClientItemChange (Sender: TOBJECT);
Begin
Edit1.text: = DDECLIENTITEM1.TEXT;
END;
The DDE connection information can also be pasted on the clipboard or call SetLink to establish DDE sessions. The following example shows the process of dynamically establish a DDE session when the user presses the Paste Link button in the application. ?
Procedure Form1.onPastelink (Sender: TOBJECT);
VAR
Service, Topic, Item: String
Begin
IF getPastelinkInfo (Service, Topic, Item) THEN
Begin
Appname.text: = service;
TopicName.Text: = Topic;
ItemName.Text: = ITEM;
DDECLIENT.SETLINK (Service, Topic);?
DDECLIENTITEM.DDECONV: = DDECLIENT;
DDECLIENTITEM.DDEITEM: = ItemName.Text;
END;
End ;?
GetPastelinkInfo is a process defined in the DDEMAN library unit. If Returns true, the DDE linkage information is saved in three parameters; if false is returned, there is no correct format on the clipboard. ?
7.3.3 Data application?
Although the automatic mode is fast, it is convenient, but there are still some reasons to use DDE artificial patterns:
1. Server programs may not support automatic data transfer, customers must explicitly apply for server to update a specific project;
2. Save communication fees. If there is no real-time transmission, the manual model can greatly reduce the overhead of communication;
3. If the client is only used to control the run of the server, it is often not necessary to use automatic mode.
The data update of the client program needs to be applied in manner. Data application needs to call the RequestData method for the DDECLIENTCONV component. RequestData has a parameter that points to the DDE project to be applied. RequestData returns a non-ended string of PCHAR types that contains the text. Returns the memory occupied by the string must explicitly release before the program terminates. In manual mode, even if there is a DDECLIEntItem component and connected to DDECLIENTCONV, the value of the DDECLIEntItem component will not change. ?
7.3.4 Data transmission?
Data transmission is the opposite of the general DDE data flow, which is to send data from the DDE client application to the DDE server application.
Data Send two methods using DDECLIENTCONV components PoKedata and PokeDataines, their syntax is:?
Function Pokedta (item: string; data: pchar): boolean
Function PokeDataLines (item: string; data: tstrings): boolean ;?
Parameter item is a project that is coupled in the DDE server, and DATA is the data to be sent. If the data is a string, turn it into a PCHAR type and call the PoKedata method; if the data is a string table object, the PoKedatAnes method can be called.
The return value of the method is successful. Because there are some DDE server applications that do not receive the transmitted data.
The following statement sends the content in the edit box to the server:?
Strpcopy (ipt, edit1.text);
DDECLIENTCONV1.POKEDATA (DDEClientItem1.ddeItem, Thetxt);
Process strpcopy copies a string of a PASCAL type to a PCHAR type string that is not ended.
7.3.5 Control the execution of server applications
One aspect of a client control server application is: When necessary, the client can start the server program and load the session topic.
The customer control server application is more important to send a server to the server to perform various operations for the server application. Which macro commands are supported by the server to see the server application documentation.
Send a macro command to use DDECLIENTCONV EXECUTEMACRO and ExecuteMacrolines, their syntax is as follows:
Function ExecuteMacro (CMD: Pchar; Waitflag: Boolean): Boolean;
Function ExecuteMacrolines (CMD: Tstrings; Waitflag: Boolean): boolean;
The CMD is a macro command string or a macro command string list that wants to send. WAITFLAG determines the behavior of the client when the DDE server program executes the macro command. If WaitFlag is set to True, the successful call to ExecuteMacro, Executenes, PokeData, PoKedatalines is not allowed before the server macro command is executed, and they do not send data to the server and return false. If WaitFlag is set to false, the called method is trying to send data to the server before the first macro is executed.
Waitfalg's settings also depend on the server application. Some applications are attempted to send data or commands to it before the first macro is executed, causing the first macro to perform failed or result in unpredictable consequences. The server application documentation can be found in the specific situation.
Function return value indicates whether the command string is successfully transferred. The macro order performs whether the successful customer cannot detect.
7.3.6 Formatting Text
DDECLIENTCONV has a Boolean properties formartchars for deciding whether to format text. The so-called formatting text refers to characters such as BACKSPACE (8), Tab (7), Linefeed (10), Return (13) from the transmitted text data. The brackets are the ASCII code of characters. Many times these characters will cause the DDE customer data display chaos. The default value of Formatchars is False. ?
7.3.7 Response DDE event?
Components DDECLIENTCONV have two event onopen and onclose, which triggered when DDE sessions are established and abort. Component DDECLIEntItem has an onchange event. This event is often used for dump and display of DDE project data, as shown in (7.3.1).
In automatic mode, the ONOpen event is triggered when the window containing the DDECLIENTCONV component is created, or triggered when calling the setLink method, and the OnClose event triggers when the client or server program is turned off.
In artificial mode, Onopen event triggers when calling the OpenLink method, the onclose event is triggered when calling the ColSelink method.
7.3.8 Using the client and Excel exchange data
Below we create a DDE client and use this program to exchange data with a worksheet in Excel. The program design interface is shown.
Figure 7.2 Customer Sample Design Interface?
The interface contains a DDE session part DDECLIENTCONV1 and DDE project part DDECLIEntItem1 for establishing and maintaining DDE coupling; a RadiogRouP control and two radio buttons in it Autoradio, Manualradio, used to set up join mode; a GroupBox control and two of them Buttons RequestBTN and PokeBTN, used to control the application and send, where RequestBTN is shining in automatic mode; a text box MEMO1 is used to save DDE data; a button PastebTn is used to paste the connection information and establish a DDE connection; another button ClosebTN Used to turn off the system.
When designing the DDECLIENTCONV1's FormatChars property is True, so that the display format of the data from the server will be retained; ConnectMode reserves the default setting of Ddeautomatic.
The program defines a private data member Automatic for flag connection mode in class TForm1; three string data members DDSERVICE, DDETOPIC, DDEITEM are used to record connection information.
The initialization of the variable and component state is performed when the window is generated. ?
Procedure TFORM1.FormCreate (Sender: TOBJECT);
Begin
Requestbtn.enabled: = false;
Autoradio.checked: = true;
Automatic: = True;
END;
When the coupling mode changes, the program performs corresponding processing.
Automatic mode is converted to manual mode:
Procedure TFORM1.Manualradioclick (Sender: TOBJECT);
Begin
IF Automatic Then
Begin
Requestbtn.enabled: = manualradio.checked;
DDECLIENTCONV1.CONNECTMODE: = DDEMANUAL
Automatic: = FALSE;
END;
END;
Manual mode is converted to automatic mode:
Procedure TFORM1.AUTORADIOCLICK (Sender: TOBJECT);
Begin
IF not Automatic Then
Begin
Requestbtn.enabled: = manualradio.checked;
IF (DDSERVICE = '') or (DDETOPIC = '') Thenbegin
Messagedlg ('Can Not Set Link.', MTWARNING, [Mbok], 0);
EXIT;
END;
DDECLIENTCONV1.SETLINK (DDService, DDETOPIC);
DDECLIENTITEM1.DDECONV: = DDECLIENTCONV1;
DDECLIENTITEM1.DDEITEM: = DDEITEM;
DDECLIENTCONV1.CONNECTMODE: = DDEAUTOMATIC
Automatic: = True;
END;
END;
When converting from automatic mode to manual mode, you only need to simply modify the corresponding attribute; and switch from manual mode to automatic mode, you need to call SetLink to re-establish the join, otherwise it will often trigger a DDE exception.
The establishment of the connection adopts the way to attach information from the clipboard, which is the most flexible way.
Procedure TFORM1.PASTEBTNCLICK (Sender: TOBJECT);
Begin
IF getPastelinkInfo (DDeService, Ddetopic, DdeItem) THEN
Begin
DDECLIENTCONV1.SETLINK (DDService, DDETOPIC);
IF Automatic Then
Begin
DDECLIENTITEM1.DDECONV: = DDECLIENTCONV1;
DDECLIENTITEM1.DDEITEM: = DDEITEM;
END;
END;
END;
GetPasteinfo is a function defined in the DDEMAN library unit that detects whether or not there is link information on the clipboard and returns the corresponding DDE service, subject matter, and project.
For artificial mode, you must apply to the server to the server by the customer. In this mode, the DDE project part is redundant, and the received DDE connection information is recorded with a string. Here is the implementation code. ?
Procedure TFORM1.REQUESTBTNCLICK (Sender: TOBJECT);
VAR
THEDATA: PCHAR;
Begin
If DdeItem = '' 'THEN
Begin
Messagedlg ('Can Not Request Data', MTWARNING, [Mbok], 0);
EXIT;
END;
THEDATA: = Stralloc (79);
DDECLIENTCONV1.OPENLINK;
THEDATA: = DDECLIENTCONV1.REQUESTDATA (DDEITEM);
DDECLIENTCONV1.CLOSLINK;
If theata <> nil dam
Memo1.text: = strpaas (theDATA);
STRDispose (THEDATA);
End ;?
OpenLink, Closelink method is used to open and close the join. The RequestData method applies for data to the server and returns to a PCHAR string. Strings must explicitly allocate memory and release when exiting.
Data transmission is different in different couplings. For manual mode, the connection to the open and closing operations are added. The list of procedures is as follows.
Procedure TFORM1.POKEBTNCLICK (Sender: TOBJECT);
Begin
If DdeItem = '' 'THEN
Begin
Messagedlg ('can not poke data.', MTWARNING, [Mbok], 0);
EXIT;
END;
IF Automatic Then
DDECLIENTCONV1.POKEDATALINES (DDEITEM, Memo1.Lines) ELSE
Begin
DDECLIENTCONV1.OPENLINK;
DDECLIENTCONV1.POKEDATALINES (DDEITEM, Memo1.Lines);
DDECLIENTCONV1.CLOSLINK;
END;
End ;?
Open Excel in Microsoft Office, load a file, select the associated unit, copy to the clipboard. Then, run the program, press the Paste Link button, the DDE join is established, and the data in the relevant unit is displayed in MEMO1. You can then perform a series of work such as mode conversion, data application, and sending. The running screen is displayed as shown below. ?
Figure 7.3 DDE Customer and Excel exchange data?
7.3.9 Customer Control Program Manager
The following example sends a command to the program manager with a client program to create a program group, program item, and delete program group. ?
The program manager provides the application's DDE interface command string. The application uses these command strings to implement the following features:
1. Create a program group
The command format is:
CreateGroup (program group name [, path where the program group is located))
Create when the program group does not exist; if the program group exists, it is activated according to the specified path.
2. Remove the program group
The command format is:
Deletegroup (program name name)
3. Display program group
Command format is;
Showgroup (program group name, display sign)
The display flag is used to control the program group in the program manager to display it in great, very small or normal.
4. Replace the program group
The command format is:
RELOADGROUP (program group name)
This command makes the program manager first delete, and then reload an existing program group.
5. Add a program item to the program group
The command format is:
AddItem (command line [, description [, icon path [, icon serial number [, icon horizontal coordinate, icon shade [, work area directory [, hotkey [, whether to minimize display markers]]]]]])
The execution of the command line control program item can include paths, parameters, and the like. Other parameters correspond to the parameters and options that need to be set when adding a program item in the program manager. They have default settings and are thus optional.
6. Program items in the replacement program group
The command format is:
ReplaceItem (program item name)
This command deletes a program entry and records the location of the deleted program item, increasing new projects in this recorded location through AddItem.
7. Remove the program item from the program group
The command format is:
DELETEITEM (program item name)
Remove a program item from the current active program group.
8. Close the program manager
The command format is:
EXITPROGRAM (Whether to save the program group information sign)
The method of sending a command string from an application to the program manager is basically consistent. For the sake of simplicity, only a situation in which only one string parameter is included, the reader can easily make further expansion.
The programming interface is shown in the figure, which includes a DDE customer session (DDECLIENTCONV) component and four buttons that complete different features. ?
Figure 7.4 Control the design interface of the program manager application?
DDECLIENTCONV creates a DDE session when designing and program manager, where DDE servers and DDE topics are Progman. The connection mode ConnectMode is set to DDEMANUAL.
We abstract only one of the commands of a string parameter, forming the following SendMacro function. ?
Function TFORM1.SENDMACRO (Name: String; Command: String): Boolean;
VAR
Macro: String;
CMD: array [0..255] of char;
Begin
RESULT: = TRUE;
IF name <> '' THEN
Begin
Macro: = Format ('[' command '(% s)]', [name]) # 13 # 10;
Strpcopy (cmd, macro);
DDECLIENT.OPENLINK;
IF not Ddeclient.executemacro (cmd, false) THEN
Result: = FALSE;
DDECLIENT.CLOSLINK;
END;
END;
The process first uses the Format function to form a macro string:
Macro: = Format ('[' command '(% s)]', [name]) # 13 # 10;
Then, copy the PASCAL type string to a program manager acceptable PCHAR type string.
DDE is coupled to artificial mode. First call the OpenLink method. Then then call the ExecuteMacro method to send a command, if it fails, returns false. Finally, close the join with Closelink.
Three buttons createButton, AddButton, DeleteButton are used to create a program group, add a program item, and delete the program group. Their procedures are similar, as shown below.
Create a program group:?
Procedure TFORM1.CREATEBUTTONCLICK (Sender: TOBJECT);
VAR
Name: String;
Begin
Name: = INPUTBOX ('Input Box', 'Input Group Name', '');
if Name = '' THEN
Messagedlg ('Group name can not be blank.', MTerror, [Mbok], 0)
Else
IF sendmacro (name, 'cretegroup') = false then
Messagedlg ('unable to create group.', Mtinformation, [mbok], 0);
END;
Add a program item:
Procedure TFORM1.AddButtonClick (Sender: TOBJECT);
VAR
Name: String;
Begin
Name: = INPUTBOX ('INPUT BOX', 'Input Application Full_path Name', '');
if Name = '' THEN
Messagedlg ('Application Name Can Not Be Blank.', MTerror, [Mbok], 0)
Else
IF sendmacro (name, 'additem') = false kil
Messagedlg ('unable to add item.', Mtinformation, [mbok], 0);
END;
Delete the program group:
Procedure TFORM1.DELETEBUTTONCLICK (Sender: TOBJECT);
VAR
Name: String;
Begin
Name: = INPUTBOX ('Input Box', 'Input Group name to be deleted ";');
if Name = '' THEN
Messagedlg ('Group Name Can Not Be Blank.', MTerror, [Mbok], 0) Else
IF sendmacro (name, 'deletegroup') = false kil
Messagedlg ('unable to create group.', Mtinformation, [mbok], 0);
END;
7.4 Implementation of DDE Server Programs
The DDE server program responds to the request of the DDE customer, generally contains the data that the client wants to get.
Creating a DDE server program must add a DDeServerItem component to the form. The Text or Lines property of DDeServerItem contains data to be connected. Generally DDeServerItem components are connected to another text control. The value of the DDeServerItem's Text or Lines property is updated when the content in the text control changes. A program below links DDeServerItem and 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 ;?
You can also join a DDeServerConv component when you create a DDE server program, and link the two components with the DDeServerItem's ServerConv property. 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 DDeServerConv components in the following cases:
1. The title of the DDeServerItem part form may change at runtime or may have other forms to have the same title. In this case, the DDE coupling may not be established;
2. DDE client may send a macro command to your server program. In this case, only one DDeServerConv component can respond to the ONMACROEXECUTE event and perform the corresponding action.
7.4.1 and DDE clients establish join?
Generally speaking, establishing a DDE join is the task of a client program. But the server program can copy a join to the clipboard for the client to paste and establish a DDE session. Proceed as follows:
1. Call the CopyToClipboard method for the DDeServerItem component, copy the value of the Text (or Lines) attribute and the DDE link information to the clipboard;
2. DDE The client is inserted into the join data. Generally, this is achieved by selecting the appropriate command such as Edit | Paste Special or Edit | Paste Link. ?
7.4.2 Response DDE event?
There are three events of DDeServerConv: Onopen, Onclose, OnexecuteMacro. The first two events trigger when the DDE session is established and terminated. Introduction to (7.3.7).
OnexecuteMacro event is used to respond to the macro sent by the client. The OnexecuteMacro event handler has an MSG parameter that saves the sent instruction string. Users can determine how to respond to these macros during this process.
The DDeServerItem part has only one event onpokedata. This event is used to respond to the data sent by the client. If the client is a Delphi program, the client calls the PokeData or PokeDataLines method. During the processing of this event, the user can save the transmitted data to a suitable place. Generally speaking, this should be the text control contained in DDeServerItem.
The following program saves the transmitted data into the listbox. ?
Procedure form1.inddeeserveritempokedata (serder: TOBJECT);
Begin
ListBox1.Items: = ddeselRItem1.Lines
End ;?
7.4.3 DDE server application routine?
Below we create a DDE server routine and a corresponding DDE client routine.
The DDE server routine can be completed:
1. Copy the DDE linkage to the clipboard for other programs;
2. Provide data sources for other programs using a TMEMO component;
3. Receive the data sent by the client;
4. Change its own running status based on the macro sent by the client program.
The design interface of the server routine is shown.
Figure 7.5 Server routine design interface
The key properties of 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 a more beautiful screen display when the window size changes.
MEMO1 is the server's data source, DDE project part DdtestItem is connected to MEMO1 through the onchange event of Memo1.
Procedure Tddesrvrm.doonchange (Sender: TOBJECT);
Begin
IF not finpoke kil
DdtestItem.Lines: = MEMO1.LINES;
END;
Where FinPoke is a private data member of a Boolean type, which is used to signal whether the program is sent in the processing of the client. When the data is transmitted from the customer to the data source, it is not necessary to pass the data to the DDE project part.
Copy the join information to the clipboard and simply call the DdtestITEM's COPYTOCLIPBOARD method.
Procedure Tddesrvrrform.copyClick (Sender: TOBJECT);
Begin
DdtestItem.copyToclipboard;
END;
This is called by menu item Edit | Copy.
The data transmitted by the client program is during the DDETESTITEM ONPOKEDATA event processing. Change the value of the FinPoke during the receiving process to prevent invalidity of the data. ?
Procedure TDESRVRFORM.DOONPOKE (Sender: TOBJECT);
Begin
FinPoke: = true;
Memo1.Lines: = DdtestItem.Lines;
FinPoke: = false;
END;
Handle the macro sent by the customer during the ONExecUteMacro event handler of DDE session part DDETESTTTTTOPIC. We define five ways to respond to macros: CopyDDE, CLEAR, WS_NORMAL, WS_MINIMIZED, WS_MAXIMIMIZED, respectively for copying link information, clearing the content in MEMO1, and changing the window display status.
Procedure Tddesrvrform.domacro (Sender: Tobject; Msg: Tstrings);
VAR
CMD: String;
i: integer;
Begin?
CMD: = '';
IF msg.count = 0 dam;
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' tellindowstate: = 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 mainly designed to verify the above DDE server programs, but also provides a complete example of a DDE client design. The program design interface is shown. ?
Figure 7.6 DDE client's design interface?
The program saves the received DDE data in a TMEMO class part DDEDAT, and the data and macro to the server are sent in another TMEMO class PoKedat. Two buttons Pokebtn, ExecuteBTN are used to send data and macros. Two menu items File | New Link and Edit | Paste Link are used to establish a new join according to the user's input and paste the DDE from the clipboard.
The establishment of the DDE connection is implemented by calling the SetLink method.
Establishing a new coupled implementation code is as follows. ?
Procedure TFORMD.DONEWLINK (Sender: TOBJECT);
Begin
DDECLIENT.SETLINK (AppName.Text, TopicName.Text);
DDECLIENTITEM.DDECONV: = DDECLIENT;
DDECLIENTITEM.DDEITEM: = ItemName.Text;
END;
Establish the connection to establish the connection information by paste the coupling information from the clipboard.
Procedure TFORMD.EDIT1Click (Sender: TOBJECT);
VAR
Service, Topic, Item: String
Begin
Pastelink1.enabled: = getPastelinkInfo (Service, Topic, Item);
END;
Procedure TFORMD.DOPASTELINK (Sender: TOBJECT);
VAR
Service, Topic, Item: String
Begin
IF getPastelinkInfo (Service, Topic, Item) THEN
Begin
Appname.text: = service;
TopicName.Text: = Topic;
ItemName.Text: = ITEM;
DDECLIENT.SETLINK (Service, Topic);?
DDECLIENTITEM.DDECONV: = DDECLIENT;
DDECLIENTITEM.DDEITEM: = ItemName.Text;
END;
END;
Save the received event in DDEDAT during the DDECLIntItem's onchange event handler.
Procedure tFormd.ddeClientItemChange (Sender: TOBJECT);
Begin
DDEDAT.LINES: = DDECLIENTITEM.LINES;
END;
The implementation code for data transmission is as follows.
Procedure TFORMD.DOPOKE (Sender: TOBJECT);
VAR
DDECLI: TDDECLIENTCONV;
Begin
DDECLI: = DDECLIENTITEM.DDECONV;
IF ddecli <> nil dam
DDECLI.POKEDATALINES (DDEClientItem.ddeItem, PoKedat.Lines);
The implementation code sent by the macro is as follows.
Procedure TFORMD.DOMACRO (Sender: TOBJECT);
VAR
DDECLI: TDDECLIENTCONV;
CMD: String;
Begin
DDECLI: = DDECLIENTITEM.DDECONV;
IF ddecli <> nil dam
Begin
CMD: = pokedat.text # 13 # 10;
Ddecli.executeMacroLines (pokedat.lines, true);
END;
END;
Run the above two programs to establish a DDE join. The transmission and execution of the test, data transmission, data transmission, and macros have achieved expected requirements. Below is the screen display after the two programs run.
Figure 7.7 Dynamic data exchange examples between servers and customers?
7.4.4 Small knot
The clipboard and DDE are two ways to communicate under Windows. Delphi implements the corresponding function in a simple and friendly manner, providing user programming convenience. In general, the clipboard is used for static data transmission, while DDE is used for dynamic data exchange, controlling other program operations. These content is important for program development in multi-user environments.
=====================
Remarks:
(1) DDE a basic Windows application communication protocol, Dynamic Data Exchange (DDE) provides a screwdriver for Windows applications, enables the client and the server to open a session, a DDE session (Conversation) means two The cooperation application is running from the end of the session, and these two applications must be running, although there must be a certain preparation before running. A DDE dialog must have a topic. An example of a most common topic is the file name, and Microsoft thinks that OLE is very promising, and DDE is not encouraged, but some must use DDE. ?
(2) Now people are using OLE,?
(3) If the two parties of the data communications are doing yourself, don't use DDE, so bother, efficiency is not high, use WM_COPYDATA
The news is much faster, of course, if it is industrial control, some devices are provided by the DDE interface, then they have to use it.