看到了别人,写的挺好,转载一下。顺便自己记录一下。
1.设置edit只读属性
方法一:m_edit1.SetReadOnly(TRUE);
方法二:::SendMessage(m_edit1.m_hWnd, EM_SETREADONLY, TRUE, 0);
2.判断edit中光标状态并得到选中内容(richedit同样适用)
int nStart, nEnd;
CString strTemp;
m_edit1.GetSel(nStart, nEnd);
if(nStart == nEnd)
{
strTemp.Format(_T(" 光标在%d" ), nStart);
AfxMessageBox(strTemp);
}
else
{
//得到edit选中的内容
m_edit1.GetWindowText(strTemp);
strTemp = strTemp.Mid(nStart) - strTemp.Mid(nEnd);
AfxMessageBox(strTemp);
}
注:GetSel后,如果nStart和nEnd,表明光标处于某个位置(直观来看就是光标在闪动);
如果nStart和nEnd不相等,表明用户在edit中选中了一段内容。
3.在edit最后添加字符串
CString str;
m_edit1.SetSel(-1, -1);
m_edit1.ReplaceSel(str);
4.随输入自动滚动到最后一行(richedit同样适用)
方法一:(摘自msdn)
// The pointer to my edit.
extern CEdit* pmyEdit;
int nFirstVisible = pmyEdit-> GetFirstVisibleLine();
// Scroll the edit control so that the first visible line
// is the first line of text.
if (nFirstVisible > 0)
{
pmyEdit-> LineScroll(-nFirstVisible, 0);
}
方法二:m_richedit.PostMessage(WM_VSCROLL, SB_BOTTOM, 0);
5.如何限制edit输入指定字符
可以从CEdit派生一个类,添加WM_CHAR消息映射。下面一个例子实现了限定输入16进制字符的功能。
void CMyHexEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
if ( (nChar > = 0 && nChar < = 9) ||
(nChar > = a & & nChar < = f) ||
(nChar > = A && nChar < = F) ||
nChar == VK_BACK ||
nChar == VK_DELETE) //msdn的virtual key
{
CEdit::OnChar(nChar, nRepCnt, nFlags);
}
}