本文主要是介绍dde的笔记,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
研读下边连接后,产生一个可以运行的代码。vc6+xp
http://blog.csdn.net/jamesxing/article/details/2142266
// mfcddeDlg.cpp : implementation file
//#include "stdafx.h"
#include "mfcdde.h"
#include "mfcddeDlg.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif//#include "ddel.h"
#include "Ddeml.h"#define NITEM 2 //定义ITEM的数量;
const char szApp[]="Server"; //server DDE服务名;
const char szTopic[]="Topic";//Server DDE目录名;
const char *pszItem[NITEM]={"Item1","Item2"};//SERVER ITEM名称字符串数组;
int count=0;//记数,在Static1中显示;
CString ServerData[NITEM];//存放服务器中的数据项内容
HCONV hConv=0; //会话句柄;
DWORD idlnst=0; //DDEML实例句柄;
HWND hWnd; //窗口句柄;
HANDLE hlnst; //实例句柄;
HSZ hszApp=0; //SERVER服务字符串句柄;
HSZ hszTopic=0; //SERVER目录字符串句柄;
HSZ hszItem[NITEM]; //Server ITEM字符串句柄;
BOOL bConnect; // 建立连接标志;//////////////////////////////////////DDE回调函数;
HDDEDATA CALLBACK DdeCallback(UINT wType,UINT wFmt,HCONV hConv,HSZ Topic,HSZ Item,HDDEDATA hData,DWORD lData1,DWORD lData2)
{int I ;char tmp[255];switch(wType){case XTYP_ADVSTART:case XTYP_CONNECT://请求连接;return ((HDDEDATA)TRUE);//允许;case XTYP_ADVDATA: //有数据到来;for(I=0;I<NITEM;I++)if(Item==hszItem[I]){DdeGetData(hData,(PBYTE)tmp,255,0);//取得数据;switch(I){ case 0:SetDlgItemText(hWnd,IDC_STATIC2,tmp);break;case 1:SetDlgItemText(hWnd,IDC_STATIC3,tmp);break;}}return ((HDDEDATA)DDE_FACK);//回执;case XTYP_ADVREQ:case XTYP_REQUEST://数据请求;for(I=0;I<NITEM;I++)if(Item==hszItem[I])return(DdeCreateDataHandle(idlnst,(PBYTE)(LPCTSTR)ServerData[I],ServerData[I].GetLength()+1,0,Item,wFmt,0));}return(0);
}/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App Aboutclass CAboutDlg : public CDialog
{
public:CAboutDlg();// Dialog Data//{{AFX_DATA(CAboutDlg)enum { IDD = IDD_ABOUTBOX };//}}AFX_DATA// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CAboutDlg)
protected:virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support//}}AFX_VIRTUAL// Implementation
protected://{{AFX_MSG(CAboutDlg)//}}AFX_MSGDECLARE_MESSAGE_MAP()
};CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{//{{AFX_DATA_INIT(CAboutDlg)//}}AFX_DATA_INIT
}void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{CDialog::DoDataExchange(pDX);//{{AFX_DATA_MAP(CAboutDlg)//}}AFX_DATA_MAP
}BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CMfcddeDlg dialogCMfcddeDlg::CMfcddeDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMfcddeDlg::IDD, pParent)
{//{{AFX_DATA_INIT(CMfcddeDlg)m_edit = _T("");//}}AFX_DATA_INIT// Note that LoadIcon does not require a subsequent DestroyIcon in Win32m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}void CMfcddeDlg::DoDataExchange(CDataExchange* pDX)
{CDialog::DoDataExchange(pDX);//{{AFX_DATA_MAP(CMfcddeDlg)DDX_Text(pDX, IDC_EDIT, m_edit);//}}AFX_DATA_MAP
}BEGIN_MESSAGE_MAP(CMfcddeDlg, CDialog)
//{{AFX_MSG_MAP(CMfcddeDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_DESTROY()
ON_WM_TIMER()
ON_EN_CHANGE(IDC_EDIT, OnChangeEdit)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CMfcddeDlg message handlersBOOL CMfcddeDlg::OnInitDialog()
{CDialog::OnInitDialog();// Add "About..." menu item to system menu.// IDM_ABOUTBOX must be in the system command range.ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);ASSERT(IDM_ABOUTBOX < 0xF000);CMenu* pSysMenu = GetSystemMenu(FALSE);if (pSysMenu != NULL){CString strAboutMenu;strAboutMenu.LoadString(IDS_ABOUTBOX);if (!strAboutMenu.IsEmpty()){pSysMenu->AppendMenu(MF_SEPARATOR);pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);}}// Set the icon for this dialog. The framework does this automatically// when the application's main window is not a dialogSetIcon(m_hIcon, TRUE); // Set big iconSetIcon(m_hIcon, FALSE); // Set small icon// TODO: Add extra initialization herehWnd=m_hWnd;if (DdeInitialize(&idlnst,(PFNCALLBACK)DdeCallback,APPCMD_FILTERINITS|CBF_FAIL_EXECUTES|CBF_SKIP_CONNECT_CONFIRMS|CBF_FAIL_SELFCONNECTIONS|CBF_FAIL_POKES,0)){MessageBox("DDE SERVER初始化失败!");return FALSE;}hlnst=AfxGetApp()->m_hInstance;//创建DDE stringhszApp=DdeCreateStringHandle(idlnst,szApp,0);hszTopic=DdeCreateStringHandle(idlnst,szTopic,0);for(int I=0;I<NITEM;I++)hszItem[I]=DdeCreateStringHandle(idlnst,pszItem[I],0);//注册服务;DdeNameService(idlnst,hszApp,0,DNS_REGISTER);bConnect=FALSE;SetTimer(1,1000,NULL);//开始定时;return TRUE; // return TRUE unless you set the focus to a control
}void CMfcddeDlg::OnSysCommand(UINT nID, LPARAM lParam)
{if ((nID & 0xFFF0) == IDM_ABOUTBOX){CAboutDlg dlgAbout;dlgAbout.DoModal();}else{CDialog::OnSysCommand(nID, lParam);}
}// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.void CMfcddeDlg::OnPaint()
{if (IsIconic()){CPaintDC dc(this); // device context for paintingSendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);// Center icon in client rectangleint cxIcon = GetSystemMetrics(SM_CXICON);int cyIcon = GetSystemMetrics(SM_CYICON);CRect rect;GetClientRect(&rect);int x = (rect.Width() - cxIcon + 1) / 2;int y = (rect.Height() - cyIcon + 1) / 2;// Draw the icondc.DrawIcon(x, y, m_hIcon);}else{CDialog::OnPaint();}
}// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CMfcddeDlg::OnQueryDragIcon()
{return (HCURSOR) m_hIcon;
}void CMfcddeDlg::OnDestroy()
{CDialog::OnDestroy();KillTimer(1);//销毁定时;DdeNameService(idlnst,0,0,DNS_UNREGISTER);//注销服务;DdeFreeStringHandle(idlnst,hszApp);DdeFreeStringHandle(idlnst,hszTopic);for(int I=0;I<NITEM;I++)DdeFreeStringHandle(idlnst,hszItem[I]);DdeUninitialize(idlnst);// TODO: Add your message handler code here}void CMfcddeDlg::OnTimer(UINT nIDEvent)
{// TODO: Add your message handler code here and/or call defaultcount++;ServerData[1].Format("%d",count);SetDlgItemText(IDC_STATIC1,ServerData[1]);DdePostAdvise(idlnst,hszTopic,hszItem[1]);//通知更新;if(!bConnect)//如果没有建立连接{hConv=DdeConnect(idlnst,hszApp,hszTopic,NULL);//连接服务器端;if(hConv) //如果建立成功{DWORD dwResult;bConnect=TRUE;for(int I=0;I<NITEM;I++)DdeClientTransaction(NULL,0,hConv,hszItem[I],CF_TEXT,XTYP_ADVSTART,TIMEOUT_ASYNC,&dwResult);}}CDialog::OnTimer(nIDEvent);
}void CMfcddeDlg::OnChangeEdit()
{// TODO: If this is a RICHEDIT control, the control will not// send this notification unless you override the CDialog::OnInitDialog()// function and call CRichEditCtrl().SetEventMask()// with the ENM_CHANGE flag ORed into the mask.// TODO: Add your control notification handler code hereUpdateData();ServerData[0]=m_edit;DdePostAdvise(idlnst,hszTopic,hszItem[0]);//通知DDE更新该数据项目;}
这篇关于dde的笔记的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!