#pragma pack(1)
struct Student {
char ID[10];
TCHAR Name[20];
UINT Age;
UINT Grade;
char Room[5];
char Tel[12];
};
#pragma pack()
//**********************************************************
因為需要在接收方的OnCopyData()函式中區分發送的兩種不同型別資料。所以就定義了以下兩個常量:
#define STRING 1
#define STUDENT 2
傳送方:
void CSendDataDlg::OnBtSend() //實現CString型別資料的傳送
{
UpdateData(TRUE);
if (m_szData.IsEmpty()) {
m_szData = _T("Hello");
UpdateData(FALSE);
}
// m_szData += "\0";
HWND hWndRcv = ::FindWindow(NULL,"Receiver");
if (hWndRcv == NULL) {
AfxMessageBox(_T("找不到接收視窗,傳送不成功"));
return ;
COPYDATASTRUCT cpd;
cpd.dwData = STRING; //標誌為CString型別
cpd.cbData = m_szData.GetLength() + 1;
//GetLength()只是取得實際字元的長度,沒有包括"\0".
cpd.lpData = (void*)m_szData.GetBuffer(cpd.cbData);
::SendMessage(hWndRcv,WM_COPYDATA,(WPARAM)this->m_hWnd,(LPARAM)&cpd);
m_szData.ReleaseBuffer();
AfxMessageBox(_T("傳送成功"));
void CSendDataDlg::OnBtStu() //實現Student型別資料的傳送
UpdateData();
m_szID += "\0";
m_szName += "\0";
m_szRoom += "\0";
m_szTel += "\0";
m_pStu = new Student();
strcpy(m_pStu->ID,m_szID.GetBuffer(m_szID.GetLength()));
_tcscpy(m_pStu->Name,m_szName.GetBuffer(m_szName.GetLength()));
strcpy(m_pStu->Room,m_szRoom.GetBuffer(m_szRoom.GetLength()));
strcpy(m_pStu->Tel,m_szTel.GetBuffer(m_szTel.GetLength()));
m_szID.ReleaseBuffer();m_szName.ReleaseBuffer();
m_szRoom.ReleaseBuffer();m_szTel.ReleaseBuffer();
m_pStu->Age = m_nAge;
m_pStu->Grade = m_nGrade;
cpd.dwData = STUDENT; // 標誌為Student型別
cpd.cbData = sizeof(Student);
cpd.lpData = (PVOID)m_pStu;
delete m_pStu;
接收方:
在OnInitDialog方法中:
//***************************************************************
//初始化ListCtrl控制元件
LVCOLUMN column;
column.mask = LVCF_TEXT | LVCF_SUBITEM | LVCF_WIDTH;
column.cx = 80;
column.iSubItem = 0;
column.pszText = _T("ID");
m_ListCtl.InsertColumn(0,&column);
column.pszText = _T("Name");
column.iSubItem = 1;
m_ListCtl.InsertColumn(1,&column);
column.cx = 55;
column.pszText = _T("Age");
column.iSubItem = 2;
m_ListCtl.InsertColumn(2,&column);
column.pszText = _T("Grade");
column.iSubItem = 3;
m_ListCtl.InsertColumn(3,&column);
column.pszText = _T("Room");
column.iSubItem = 4;
m_ListCtl.InsertColumn(4,&column);
column.pszText = _T("Tel");
column.iSubItem = 5;
m_ListCtl.InsertColumn(5,&column);
BOOL CReceiverDlg::OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct)
switch (pCopyDataStruct->dwData) { // 接收到的是CString型別
case STRING:
m_szData += (LPCSTR)(pCopyDataStruct->lpData);
break;
case STUDENT: // 接收到的是Student型別
CString id,name,room,tel;
UINT age,grade;
CString str;
Student* pStu = (Student*)(pCopyDataStruct->lpData);
id = pStu->ID;
name = pStu->Name;
room = pStu->Room;
tel = pStu->Tel;
age = pStu->Age;
grade = pStu->Grade;
LVITEM item;
// 把接收到的資料顯示到ListCtrl控制元件上
item.mask = LVIF_TEXT;
int n = m_ListCtl.GetItemCount();
item.iItem = n;
item.iSubItem = 0;
item.pszText = id.GetBuffer(id.GetLength());
id.ReleaseBuffer();
m_ListCtl.InsertItem(&item);
m_ListCtl.SetItemText(n,1,name);
str.Format("%d",age);
m_ListCtl.SetItemText(n,2,str);
str.Format("%d",grade);
m_ListCtl.SetItemText(n,3,str);
m_ListCtl.SetItemText(n,4,room);
m_ListCtl.SetItemText(n,5,tel);
//delete pStu;
// return CDialog::OnCopyData(pWnd, pCopyDataStruct);
return TRUE;
#pragma pack(1)
struct Student {
char ID[10];
TCHAR Name[20];
UINT Age;
UINT Grade;
char Room[5];
char Tel[12];
};
#pragma pack()
//**********************************************************
因為需要在接收方的OnCopyData()函式中區分發送的兩種不同型別資料。所以就定義了以下兩個常量:
#define STRING 1
#define STUDENT 2
傳送方:
void CSendDataDlg::OnBtSend() //實現CString型別資料的傳送
{
UpdateData(TRUE);
if (m_szData.IsEmpty()) {
m_szData = _T("Hello");
UpdateData(FALSE);
}
// m_szData += "\0";
HWND hWndRcv = ::FindWindow(NULL,"Receiver");
if (hWndRcv == NULL) {
AfxMessageBox(_T("找不到接收視窗,傳送不成功"));
return ;
}
COPYDATASTRUCT cpd;
cpd.dwData = STRING; //標誌為CString型別
cpd.cbData = m_szData.GetLength() + 1;
//GetLength()只是取得實際字元的長度,沒有包括"\0".
cpd.lpData = (void*)m_szData.GetBuffer(cpd.cbData);
::SendMessage(hWndRcv,WM_COPYDATA,(WPARAM)this->m_hWnd,(LPARAM)&cpd);
m_szData.ReleaseBuffer();
AfxMessageBox(_T("傳送成功"));
}
void CSendDataDlg::OnBtStu() //實現Student型別資料的傳送
{
UpdateData();
m_szID += "\0";
m_szName += "\0";
m_szRoom += "\0";
m_szTel += "\0";
m_pStu = new Student();
strcpy(m_pStu->ID,m_szID.GetBuffer(m_szID.GetLength()));
_tcscpy(m_pStu->Name,m_szName.GetBuffer(m_szName.GetLength()));
strcpy(m_pStu->Room,m_szRoom.GetBuffer(m_szRoom.GetLength()));
strcpy(m_pStu->Tel,m_szTel.GetBuffer(m_szTel.GetLength()));
m_szID.ReleaseBuffer();m_szName.ReleaseBuffer();
m_szRoom.ReleaseBuffer();m_szTel.ReleaseBuffer();
m_pStu->Age = m_nAge;
m_pStu->Grade = m_nGrade;
HWND hWndRcv = ::FindWindow(NULL,"Receiver");
if (hWndRcv == NULL) {
AfxMessageBox(_T("找不到接收視窗,傳送不成功"));
return ;
}
COPYDATASTRUCT cpd;
cpd.dwData = STUDENT; // 標誌為Student型別
cpd.cbData = sizeof(Student);
cpd.lpData = (PVOID)m_pStu;
::SendMessage(hWndRcv,WM_COPYDATA,(WPARAM)this->m_hWnd,(LPARAM)&cpd);
delete m_pStu;
AfxMessageBox(_T("傳送成功"));
}
接收方:
在OnInitDialog方法中:
//***************************************************************
//初始化ListCtrl控制元件
LVCOLUMN column;
column.mask = LVCF_TEXT | LVCF_SUBITEM | LVCF_WIDTH;
column.cx = 80;
column.iSubItem = 0;
column.pszText = _T("ID");
m_ListCtl.InsertColumn(0,&column);
column.cx = 80;
column.pszText = _T("Name");
column.iSubItem = 1;
m_ListCtl.InsertColumn(1,&column);
column.cx = 55;
column.pszText = _T("Age");
column.iSubItem = 2;
m_ListCtl.InsertColumn(2,&column);
column.cx = 55;
column.pszText = _T("Grade");
column.iSubItem = 3;
m_ListCtl.InsertColumn(3,&column);
column.cx = 55;
column.pszText = _T("Room");
column.iSubItem = 4;
m_ListCtl.InsertColumn(4,&column);
column.cx = 80;
column.pszText = _T("Tel");
column.iSubItem = 5;
m_ListCtl.InsertColumn(5,&column);
BOOL CReceiverDlg::OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct)
{
switch (pCopyDataStruct->dwData) { // 接收到的是CString型別
case STRING:
m_szData += (LPCSTR)(pCopyDataStruct->lpData);
UpdateData(FALSE);
break;
case STUDENT: // 接收到的是Student型別
CString id,name,room,tel;
UINT age,grade;
CString str;
Student* pStu = (Student*)(pCopyDataStruct->lpData);
id = pStu->ID;
name = pStu->Name;
room = pStu->Room;
tel = pStu->Tel;
age = pStu->Age;
grade = pStu->Grade;
LVITEM item;
// 把接收到的資料顯示到ListCtrl控制元件上
item.mask = LVIF_TEXT;
int n = m_ListCtl.GetItemCount();
item.iItem = n;
item.iSubItem = 0;
item.pszText = id.GetBuffer(id.GetLength());
id.ReleaseBuffer();
m_ListCtl.InsertItem(&item);
m_ListCtl.SetItemText(n,1,name);
str.Format("%d",age);
m_ListCtl.SetItemText(n,2,str);
str.Format("%d",grade);
m_ListCtl.SetItemText(n,3,str);
m_ListCtl.SetItemText(n,4,room);
m_ListCtl.SetItemText(n,5,tel);
UpdateData(FALSE);
//delete pStu;
break;
}
// return CDialog::OnCopyData(pWnd, pCopyDataStruct);
return TRUE;