When the application is installed, it is common to set up the program groups, how are they implemented? The following is an example of using DDE to add a new group of programs.
1. Use MFC New Project Pmgroup (dialog form), add three edit box in the dialog box.
2. Using class Wizard, add member variable m_groupname,m_itemname,m_filename, corresponding to the added three edit Box, which will accommodate the input of three entries (group name, tag name, corresponding filename);
3. In the header file STDAFX.H, add the following code:
#include < ddeml.h >
4. In the InitInstance function of the application class Cpgroupapp, add the following code:
BOOL CPMGROUPApp::InitInstance()
{
//...
//Initialze DDEML
DdeInitialize(&dwDDEInst,NULL,APPCMD_CLIENTONLY,0);
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
if(!AddPMGroup(dlg.m_GroupName,dlg.m_ItemName,dlg.m_FileName))
{
::MessageBox(NULL,"PMGroup:DDE Error","Error
adding group and item.",MB_ICONHAND|MB_OK);
}
}
//...
}
Add a member variable to the Cpgroupapp class Dwddeinst:
DWORD Dwddeinst (with mouse right click on ClassView cpgroupapp, select Add Member Variable in pop-up menu, enter DWORD and Dwddeinst in the dialog respectively)
Because it's a dialog based application, when you click OK, call Addpmgroup to add the new program group. So also to add to the Cpgroupapp class Addpmgroup (with the mouse on the ClassView Cpgroupapp Right-click, in the pop-up menu select the Add Member Function, in the dialogue input int and addpmgroup respectively. Change the resulting int cpmgroupapp::addpmgroup () to int cpmgroupapp::addpmgroup (CString &group,cstring &item,cstring & file) to change int addpmgroup () to int addpmgroup (CString &group,cstring &item,cstring &file) in Cpgroupapp). The contents of the Addpmgroup are:
int CPMGROOPApp::AddPMGroup(CString &group,CString &item,CString &file)
{
HSZ hszService=DdeCreateStringHandle(dwDDEInst,_T("PROGMAN"),CP_WINANSI);
HSZ hszTopic=DdeCreateStringHandle(dwDDEInst,_T("PROGMAN"),CP_WINANSI);
HCONV hConV=DdeConnect(dwDDEInst,hszTopic,hszService,NULL);
DdeFreeStringHandle(dwDDEInst,hszService);
DdeFreeStringHandle(dwDDEInst,hszTopic);
if(!hConV)
{
return FALSE;
}
//主要内容
CString cmd="[CreateGroup("+group+")]";//建立组的命令字
DWORD dwResult;
LPCTSTR data=(LPCTSTR)cmd;
DdeClientTransaction((LPBYTE)data,cmd.GetLength (),
hConV,NULL,CF_TEXT,XTYP_EXECUTE,
1000,&dwResult);
cmd="[AddItem("+file+","+item+")]";
//建立"file"的图标,"item"为标记的命令字
data=(LPCTSTR)cmd;
DdeClientTransaction((LPBYTE)data,cmd.GetLength (),
hConV,NULL,CF_TEXT,XTYP_EXECUTE,
1000,&dwResult);
return TRUE;
}