设置EDIT光标的位置可以使用EM_SETSEL消息实现: void CNumEditCtrl::OnChange() { // TODO: If this is a RICHEDIT control, the control will not // send this notification unless you override the CEdit::OnInitDialog() // function and call CRichEditCtrl().SetEventMask() // with the ENM_CHANGE flag ORed into the mask. CString m_estr,szTemp; GetWindowText(m_estr); int nLen=m_estr.GetLength(); if(!m_estr.IsEmpty()) { szTemp=m_estr.Right(1); BOOL bNum=TRUE; CString strtemp=szTemp.SpanIncluding("0123456789"); if(!strtemp.IsEmpty()) bNum=TRUE; else bNum=FALSE; nPoint=GetStringNum(m_estr);
//move left CEdit *pEdit =(CEdit *) GetDlgItem(IDC_EDIT1); int nStartChar,nEndChar; pEdit->GetSel(nStartChar,nEndChar); pEdit->SetFocus(); nEndChar --; pEdit->SetSel(nEndChar,nEndChar);
void CMyDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // A device context for painting. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center the icon in the client rectangle. int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(&rect); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon represented by handle m_hIcon. dc.DrawIcon(x, y, m_hIcon); } else { CDialog::OnPaint(); } }
void CNumEditCtrl::OnChange()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CEdit::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
CString m_estr,szTemp;
GetWindowText(m_estr);
int nLen=m_estr.GetLength();
if(!m_estr.IsEmpty())
{
szTemp=m_estr.Right(1);
BOOL bNum=TRUE;
CString strtemp=szTemp.SpanIncluding("0123456789");
if(!strtemp.IsEmpty())
bNum=TRUE;
else
bNum=FALSE; nPoint=GetStringNum(m_estr);
if(strcmp(szTemp,".")==0 || bNum)
{
if(nPoint>1)
{
nPoint--;
::AfxMessageBox(".号出现次数超过两次!");
m_estr=m_estr.Left(m_estr.GetLength()-1);
SetWindowText(m_estr);
nLen--;
SendMessage(EM_SETSEL,nLen,nLen);
return;
}
}
else
{
::AfxMessageBox("输入格式错误");
m_estr=m_estr.Left(m_estr.GetLength()-1);
SetWindowText(m_estr);
nLen--;
SendMessage(EM_SETSEL,nLen,nLen);
return;
}
}
// TODO: Add your control notification handler code here
}
CEdit *pEdit =(CEdit *) GetDlgItem(IDC_EDIT1);
int nStartChar,nEndChar;
pEdit->GetSel(nStartChar,nEndChar);
pEdit->SetFocus();
nEndChar --;
pEdit->SetSel(nEndChar,nEndChar);
{
if (IsIconic())
{
CPaintDC dc(this); // A device context for painting. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center the icon in the client rectangle.
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon represented by handle m_hIcon.
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
int nStartChar,nEndChar;
pEdit->GetSel(nStartChar,nEndChar);
pEdit->SetFocus();
nEndChar --;
pEdit->SetSel(nEndChar,nEndChar);
SendMessage(edit.m_hWnd, WM_KEYDOWN, wParam, lParam);
SendMessage(edit.m_hWnd, WM_KEYUP, wParam, lParam);