Use windows message to change text in CEdit with timeout control

 

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

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值