利用类向导添加对edit控件的消息EN_SETFOCUS
void CSetselDlg::OnSetfocusEdit3()
{
// TODO: Add your control notification handler code here
m_Edit.SetSel(0, -1);
}
重载PreTranslateMessage,加上下列代码(m_Edit是控件变量):
BOOL CSetselDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
if (pMsg->message == WM_LBUTTONDOWN)
{
if (pMsg->hwnd == m_Edit.m_hWnd && GetFocus() != (CWnd*)&m_Edit)
{
m_Edit.SetFocus();
m_Edit.SetSel(0, -1);
return TRUE;
}
}
return CDialog::PreTranslateMessage(pMsg);
}
CEdit中的SetSel函数
主要解决问题: 使文本框的滚动条自动卷到文本最后.
代码背景: 在一个对话框中任意添加了两个文本框和一个按扭, 文本框的multiple line属性设置为true.
将source框中的串添加到destination框的末尾(由button1激活):
void CAboutDlg::OnButton1()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
//m_destinationo为destination框所绑定的串变量, m_source为source框所绑定的串变量
m_destination += m_source;//注意: 如果要添加换行的话, 必须用/r/n的组合
UpdateData(FALSE);
//m_select为destination框所绑定的控制变量
m_select.SetSel(m_destination.GetLength(),m_destination.GetLength(), FALSE);
}
所得:
1. 使文本框的滚动条自动卷到文本最后;
2. 换行使用/r/n;
3. SetSel()如果不和SetFocus()配合使用的话, 一般是不会看到文本被选择的, 因为一般焦点不在文本框内;
m_select.SetFocus();
//m_select为destination框所绑定的控制变量
m_select.SetSel(0,-1, FALSE);
4. SetSel()必须放在UpdateData()之后, 因为后者会将光标重置.
让CEdit控件SetSel后选中其中内容
最新推荐文章于 2020-04-05 12:38:12 发布