Use windows message to change text in CEdit with timeout control

来源:互联网 发布:户外烧烤 知乎 编辑:程序博客网 时间:2024/06/06 20:20
 

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 objects 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 );

}

原创粉丝点击