11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 16. Context Sensitive Help<br />

(Function CEditCtrl::ReplaceSel(…) will always output text at the current caret position). For this<br />

reason, the following messages are handled to bypass the default implementations in the sample:<br />

WM_LBUTTONDOWN, WM_LBUTTONUP, WM_MOSUEMOVE and ON_WM_LBUTTONDBLCLK. The handlers of these<br />

messages are empty. This will prevent the application from responding to the mouse events so that the<br />

cursor position can not be changed under any condition.<br />

If we execute the sample at this point, messages will appear on the client window indicating if the<br />

DDE initialization is successful.<br />

15.2 Connecting to Server<br />

Sample 15.2\DDE\Server is based on sample 15.1\DDE\Server. Also, a new sample 15.2\DDE\Client<br />

is created in this section, which is a dialog-based application.<br />

At the beginning, the client also needs to do DDE initialization in order to support DDE. The client<br />

initialization procedure is almost the same with that of server except that it does not need to register the<br />

service name. The client needs to call functions ::DdeInitialize(…) and ::DdeUninitialize(…) for DDE<br />

initialization and clean up. Besides, it also needs to prepare a static (or global) DWORD variable for storing its<br />

instance identifier, and prepare a callback function that will be used to receive DDE messages.<br />

DDE Connection: Client Side<br />

Before requesting any service from the server, the client needs to make DDE connection. This is<br />

somehow similar to making a phone call: we need to dial number and make the connection first, then we<br />

can request the other side to do anything for us.<br />

The client can call function ::DdeConnect(…) to set up a connection with the server:<br />

HCONV ::DdeConnect(DWORD idInst, HSZ hszService, HSZ hszTopic, PCONVCONTEXT pCC);<br />

We need to provide three parameters in order to make the connection: the client instance ID, the<br />

server’s service name, and the topic name that is supported by the server.<br />

While the service name is the identification that can be used by the client to locate the server in the<br />

system, a topic name indicates the type of service which is provided by the server. A server can provide<br />

more than one service, whose properties and features can be defined by the programmer. For example, we<br />

can implement a DDE server managing images, and a client can request the server to send any image to it.<br />

For this service, we can name the topic name “image” (or whatever).<br />

Like the service name, a string handle must be obtained for the topic name before it is used. When<br />

function ::DdeConnect(…) is called from the client side, this handle must be passed to its hszTopic<br />

parameter.<br />

The server does not need to register the topic name. When the client makes the connection, the server<br />

will receive a XTYP_CONNECT message, and the topic name string handle will be passed as one of the<br />

parameters to the DDE call back function. Upon receiving this message, the handle passed with the<br />

message can be compared with the topic string handles stored on the server side (which represent all the<br />

topics supported by the server). If there is a match, it means the server supports this topic, otherwise the<br />

server should reject the connection.<br />

DDE Connection: Server Side<br />

On the server side, we need to handle XTYP_CONNECT message in the callback function. In the previous<br />

section, NULL is returned for all messages sent to the server. In order to respond to the connection request<br />

from the client, XTYP_CONNECT message must be processed.<br />

As we mentioned before, the callback function has 8 parameters. Parameter uType indicates the type of<br />

message, if the message is a connection request, this message should be XTYP_CONNECT. The meanings of<br />

other 7 parameters are listed as follows:<br />

Parameter<br />

UFMT<br />

Not used<br />

Meaning<br />

461

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!