Advanced MFC Programming

Advanced MFC Programming Advanced MFC Programming

math.hcmuns.edu.vn
from math.hcmuns.edu.vn More from this publisher
11.04.2014 Views

Chapter 16. Context Sensitive Help ( m_dwInst, (LPBYTE)(LPCSTR)m_szPoke, m_szPoke.GetLength(), 0, hszPokeItem, CF_TEXT, 0 ); ::DdeClientTransaction ( (LPBYTE)hData, 0xFFFFFFFF, m_hConvClient, hszPokeItem, CF_TEXT, XTYP_POKE, TIMEOUT_ASYNC, NULL ); …… UnobtainHsz(hszPokeItem); Synchronous Transaction Asynchronous Transaction Initialize transaction Initialize transaction Yes Has the server responded? No Receive XTYP_XACT_COMPLETE message Yes Has timer timed out? No Go on Transaction finished Transaction finished Figure 15-6. Synchronous & asynchronous transactions The following code fragment shows how the transaction result is processed when the client receives an XTYP_XACT_COMPLETE message: …… case XTYP_XACT_COMPLETE: { HSZ hszPokeItem; hszPokeItem=ObtainHsz(TEXT("Poke")); if ( hConv == m_hConvClient && 478

Chapter 16. Context Sensitive Help …… } ) { hszTopic == m_hszTopicName && hszItem == hszPokeItem && wFormat == CF_TEXT if(hData != NULL) { Printf("Poke executed!\r\n"); } else Printf("Poke failed!\r\n"); } UnobtainHsz(hszPokeItem); There is no change on the server side. All types of transactions can be implemented by either synchronous or asynchronous mode. 15.7 Program Manager: A DDE Server By now, we have introduced some of the most useful DDE transactions. We can use these data transfer protocols to design and build applications that can share data and information. Besides this, we can also write client application to communicate with standard DDE servers contained in the system. Program Manager Under Windows, all types of applications are managed into groups. By clicking on Start | Programs command on the task bar, we will see many groups, such as “Accessories”, “Startup”. Within each group, there may exist some items that are linked directly to executable files, or there may exist some sub-groups. Sometimes we may want to modify this structure by adding a new item or deleting an existing item. Actually this structure is managed by Program Manager, which is a DDE server. We can interact with this server to create group, delete group, add items to the group, delete items from a group through initiating Execute transactions. The client sample from the previous section is modified so that it can be used to communicate only to Program Manager. Here, the service name of the Program Manager is “Progman”. As we can see, the DDE initialization for this client is exactly the same as we did before. The five commands we will ask the server to execute are: creating group; bringing up an existing group; deleting a group; creating an item; deleting an item. All the DDE commands must start and end with square braces (‘[’ and ‘]’). The following table lists the formats of five commands: Function Command format Create a group "[CreateGroup(group name)]" Pop up a group "[ShowGroup(group name, 1)]" Delete a group "[DeleteGroup(group name)]" Create an item "[AddItem(File name, item name)]" Delete an item "[DeleteItem(Item name)]" A combo box for storing the command types is added to sample’s dialog box. Besides this, the dialog box also contains an edit box and a button labeled “Command”. The edit box will be used to let the user input parameters for the command. For example, if we want to create a group with name “Test”, we can select “Create group” from the combo box and input “Test” into the edit box then click “Command” button. The application will initiate an execute transaction to the Program Manager and send “[CreateGroup(Test)]” command to it. We can use this program to create groups and add items to a group. We can also delete unwanted groups or remove items from a group. 479

Chapter 16. Context Sensitive Help<br />

……<br />

}<br />

)<br />

{<br />

hszTopic == m_hszTopicName &&<br />

hszItem == hszPokeItem &&<br />

wFormat == CF_TEXT<br />

if(hData != NULL)<br />

{<br />

Printf("Poke executed!\r\n");<br />

}<br />

else Printf("Poke failed!\r\n");<br />

}<br />

UnobtainHsz(hszPokeItem);<br />

There is no change on the server side.<br />

All types of transactions can be implemented by either synchronous or asynchronous mode.<br />

15.7 Program Manager: A DDE Server<br />

By now, we have introduced some of the most useful DDE transactions. We can use these data transfer<br />

protocols to design and build applications that can share data and information. Besides this, we can also<br />

write client application to communicate with standard DDE servers contained in the system.<br />

Program Manager<br />

Under Windows, all types of applications are managed into groups. By clicking on Start | Programs<br />

command on the task bar, we will see many groups, such as “Accessories”, “Startup”. Within each group,<br />

there may exist some items that are linked directly to executable files, or there may exist some sub-groups.<br />

Sometimes we may want to modify this structure by adding a new item or deleting an existing item.<br />

Actually this structure is managed by Program Manager, which is a DDE server. We can interact with<br />

this server to create group, delete group, add items to the group, delete items from a group through<br />

initiating Execute transactions.<br />

The client sample from the previous section is modified so that it can be used to communicate only to<br />

Program Manager. Here, the service name of the Program Manager is “Progman”. As we can see, the<br />

DDE initialization for this client is exactly the same as we did before.<br />

The five commands we will ask the server to execute are: creating group; bringing up an existing<br />

group; deleting a group; creating an item; deleting an item. All the DDE commands must start and end with<br />

square braces (‘[’ and ‘]’). The following table lists the formats of five commands:<br />

Function<br />

Command format<br />

Create a group "[CreateGroup(group name)]"<br />

Pop up a group "[ShowGroup(group name, 1)]"<br />

Delete a group "[DeleteGroup(group name)]"<br />

Create an item "[AddItem(File name, item name)]"<br />

Delete an item "[DeleteItem(Item name)]"<br />

A combo box for storing the command types is added to sample’s dialog box. Besides this, the dialog<br />

box also contains an edit box and a button labeled “Command”. The edit box will be used to let the user<br />

input parameters for the command. For example, if we want to create a group with name “Test”, we can<br />

select “Create group” from the combo box and input “Test” into the edit box then click “Command” button.<br />

The application will initiate an execute transaction to the Program Manager and send<br />

“[CreateGroup(Test)]” command to it.<br />

We can use this program to create groups and add items to a group. We can also delete unwanted<br />

groups or remove items from a group.<br />

479

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

Saved successfully!

Ooh no, something went wrong!