//发送端
void MainWindow::on_pushButton_clicked()
{
HWND hWnd = ::FindWindowW(NULL, L"ProgressBar");
if (hWnd != NULL) {
setWindowTitle("Send");
QString str=ui->lineEdit->text();
QByteArray st = str.toLocal8Bit();
COPYDATASTRUCT cpd;
cpd.dwData = 0;
// cpd.cbData = str.size()+1;
cpd.cbData = st.length()+1;
cpd.lpData = st.data();
::SendMessageW(hWnd, WM_COPYDATA, NULL, (LPARAM)&cpd);
// qDebug()<<"cpd.cbdata:%s"<<cpd.cbData;
ui->m_label->setText("Message has been sent."+QString(str.toLocal8Bit().data()));
} else {
setWindowTitle("receive");
ui->m_label->setText("Ready...");
}
}
//接受端
bool MainWindow::nativeEvent(const QByteArray &eventType, void *message, long *result)
{
MSG* msg = reinterpret_cast<MSG*>(message);
int msgType = msg->message;
if (msg->message == WM_COPYDATA)
{
COPYDATASTRUCT * p = reinterpret_cast<COPYDATASTRUCT *>(msg->lParam);
HWND hWnd = ::FindWindowW(NULL, L"Send");
MessageBox(hWnd,(LPCTSTR)(p->lpData),NULL,msgType);
char *t = static_cast<char *>(p->lpData);
const QByteArray bb=QByteArray(static_cast<char *>(p->lpData));
QString recv=QString::fromLocal8Bit(bb);
ui->textEdit->append(recv);
qDebug()<<":%d"<<recv.toInt();
ui->textEdit->append(QString::number(recv.toInt()));
// ui->label->setText(QString::fromLocal8Bit(bb));
ui->label_2->setNum((int)strlen(t));
}
return false;
}