When using socket class, usually you have to send your data received from remote peer to upper CDialog class, the basic ways are:
1, pass the windows handle (this or m_hWnd)to socket class object’s component data in construct function.
2, get the main window handle, use the function AfxGetApp()->m_pMainWnd;
3, use a globe buffer to contain data.
And there is an easy way pass data to upper class, just use the CEdit pointer.
The code is as follows: and we added time control.
HWND hWnd = NULL; //the CEdit component window handle if (NULL == m_pMsgCtrl) return false; if (NULL = (hWnd = m_pMsgCtrl->GetSafeHwnd())//CEdit *m_pMsgCtrl; return false;
DWORD dwResult = 0; // the same as returned value of SendMessage // get the length of text in CEdit // 1000L stands for 1 second, up to 15 seconds if (SendMessageTimeout(hWnd, WM_GETTEXTLENGTH, 0, 0, SMTO_NORMAL, 1000L , &dwResult) != 0) { int nLen = (int) dwResult; // set the place to insert new string, just select the text. if (SendMessageTimeout(hWnd, EM_SETSEL, nLen, nLen, SMTO_NORMAL, 1000L , &dwResult) != 0) { // send new string if (SendMessageTimeout(hWnd, EM_REPLACESEL, FALSE, (LPARAM)strText, SMTO_NORMAL, 1000L , &dwResult) != 0) { } } } |
It works the same as:
if (::IsWindow( m_pMsgCtrl->GetSafeHwnd() )) { int nLen = m_pMsgCtrl->GetWindowTextLength(); m_pMsgCtrl->SetSel(nLen, nLen); m_pMsgCtrl->ReplaceSel( strText ); } |