如何在Edit控件中获取char位置? [英] How to get the char position in Edit control ?

查看:84
本文介绍了如何在Edit控件中获取char位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用单行编辑控件,其中我有一个文本,我需要更改文本中的特定字符,其他字符需要保持原样。什么是最好的方法这样做。有人可以提出一些建议。谢谢你提前!!

I am using a single line Edit control ,in which I got a text in it, I need to change only a particular character in the text and the other characters need to remain as it is.What will be the best approach for doing it..Can somebody please come up with some suggestions .Thanks in Advance !!

推荐答案

我假设你在C中使用Win32级别。



向编辑控件发送EM_SETSEL消息,从而根据您想要更改的字符设置选择。请参阅: http://msdn.microsoft.com/ en-us / library / windows / desktop / bb761661%28v = vs.85%29.aspx [ ^ ]



然后发送一个EM_REPLACESEL消息要做替换。请参阅: http://msdn.microsoft.com/ en-us / library / windows / desktop / bb761633%28v = vs.85%29.aspx [ ^ ]
I assume that you are working Win32 level in C.

Send an EM_SETSEL message to the edit control and set thereby the selection to the character or characters you would like to change. See: http://msdn.microsoft.com/en-us/library/windows/desktop/bb761661%28v=vs.85%29.aspx[^]

Then send an EM_REPLACESEL message to do the replacement. See: http://msdn.microsoft.com/en-us/library/windows/desktop/bb761633%28v=vs.85%29.aspx[^]


很难给出一个对你的问题提出疑问,但你应该查看文档 [ ^ ]查看哪种方法或消息最适合您的目的。
It''s difficult to give an naswer to your question, but you should check the documentation[^] to see which methods or messages would best suit your purpose.


编辑控件不提供操作单个字符的功能。您有两种选择:



1.获取完整文本,修改并存储回来

使用 GetWindowText()检索文本,修改它,然后使用 SetWindowText()将其放回去。使用CString时,单个字符可以被 SetAt()成员函数替换:

Edit controls does not provide functions to manipulate single characters. You have two options:

1. Get the complete text, modify it, and store it back
Use GetWindowText() to retrieve the text, modify it, and put it back using SetWindowText(). When using a CString, a single character can be replaced by the SetAt() member function:
void CMyEdit::SetChar1(int nPos, TCHAR c)
{
    if (nPos >= 0 && nPos < GetWindowTextLength())
    {
        CString s;
        GetWindowText(s);
        s.SetAt(nPos, c);
        SetWindowText(s.GetString());
    }
}



此方法适用于具有少量文本的控件以及应执行多个操作的情况,例如:替换字符串中每个字符的出现次数:


This method is appropiate for controls with small amount of texts and when multiple operations should be performed, e.g. replacing each occurrence of a character in the string:

void CMyEdit::ReplaceChar(TCHAR cOld, TCHAR cNew)
{
    CString s;
    GetWindowText(s);
    if (s.Replace(cOld, cNew))
        SetWindowText(s.GetString());
}





2.使用带编辑控制的字符串替换

使用 SetSel()将要替换的字符的索引作为开始传递,再将另一个字符作为结束点传递,然后调用 ReplaceSel()使用从新字符创建的字符串:



2. Use string replacement with edit control
Use SetSel() passing the index of the character to be replaced as start and one more as end point followed by a call to ReplaceSel() using a string created from the new character:

void CMyEdit::SetChar2(int nPos, TCHAR c)
{
    if (nPos >= 0 && nPos < GetWindowTextLength())
    {
        TCHAR lpszReplace[2] = {c, _T('\0') };
        // Save the selection that might be set by the user
        DWORD dwSaveSel = GetSel();
        SetSel(nPos, nPos+1, 1);
        ReplaceSel(lpszReplace);
        SetSel(dwSaveSel, 1);
    }
}





[更新]

从评论中可以看出你需要一个键盘模拟。这可以通过为编辑控件实现 WM_CHAR 处理程序以及存储最后按下的char代码,事件时间和重复按下次数的一些成员变量来完成。在处理程序内部检查char代码是否与最后一个相同,最后一个事件是否在重复时间范围内。如果是这样,根据重复的按下更改传递给默认处理程序的char代码,并递增重复计数器(在切换所有代码时可以回绕为null):



[UPDATE]
From the comments it becomes clear that you need a keypad emulation. This can be done implementing a WM_CHAR handler for the edit control and some member variables storing the last pressed char code, event time, and number of repeated presses. Inside the handler check if the char code is identical to the last one and the last event was inside the repeat time span. If so, change the char code passed to the default handler by the new one according to the repeated presses and increment the repeat counter (which may wrap back to null when toggling through all codes):

void CMyEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
    UINT nNewChar = nChar;
    DWORD dwTime = ::GetTickCount();
    if (nChar == m_nLastChar && dwTime - m_dwLastTime < TIMEOUT)
    {
        // Set nNewChar here according to nChar and m_nRepeatCount
//      nNewChar = pCharTable[nChar][m_nRepeatCount];
        m_nRepeatCount++;
        // When cycled through all combinations, clear m_nRepeatCount

        // Select last char; so it will be replaced by the new one
        // Without tracking the position, 
        //  just use GetWindowTextLength() - 1 as position
        SetSel(m_nPos, m_nPos+1, 1);
    }
    else
    {
        m_nLastChar = nChar;
        m_nRepeatCount = 0;
    }
    m_dwLastTime = dwTime;
    CEdit::OnChar(nNewChar, nRepCnt, nFlags);
}


这篇关于如何在Edit控件中获取char位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆