 |
|
 |
 | MFC Tips - Common Controls C++ |  |
Trimis: 19/07/2007 16:26 |
|
|
| Ad_Infinitum |
| Administrator |

 |
 |
| Data inscrierii: 23/Iun/2007 |
| Mesaje: 212 |
|
|
|
 |
 |
 |
|
Cum inlocuim o line intr-un edit multiline?
Cine cauta in (editii mai vechi) de MSDN s-ar putea sa gaseasca acest
Exemplu
| Citat: |
// The pointer to my edit.
extern CEdit* pmyEdit;
// The string for replacing.
extern LPCTSTR lpszmyString;
int nBegin, nEnd;
// Replace the second line, if it exists, of the edit control
// with the text lpszmyString.
if ((nBegin=pmyEdit->LineIndex(1)) != -1)
{
nEnd = nBegin + pmyEdit->LineLength(1);
pmyEdit->SetSel(nBegin, nEnd);
pmyEdit->ReplaceSel(lpszmyString);
}
|
Hai mai intai sa-l "traducem" intr-o forma mai usor de citit de programatorii MFC:
| Citat: |
CString strText = _T("Replaced text");
CEdit* pEdit = (CEdit*)GetDlgItem(IDC_EDIT1);
int nBegin = pEdit->LineIndex(1);
if(-1 != nBegin)
{
int nEnd = nBegin + pEdit->LineLength(1); // gresit!
pEdit->SetSel(nBegin, nEnd);
pEdit->ReplaceSel(strText);
}
|
Problema
Exemplul de mai sus nu functioneaza pentru ca functia CEdit::LineLength trebuie sa primeasca un "character index" (cel intors de functia CEdit::LineIndex), adica indexul primului caracter din linie numarat de la inceputul textului si nu indexul liniei.
Rezolvare
(vezi spre comparatie linia in care se apeleaza LineLength)
| Citat: |
CString strText = _T("Replaced text");
CEdit* pEdit = (CEdit*)GetDlgItem(IDC_EDIT1);
int nBegin = pEdit->LineIndex(1);
if(-1 != nBegin)
{
int nEnd = nBegin + pEdit->LineLength(nBegin); // OK.
pEdit->SetSel(nBegin, nEnd);
pEdit->ReplaceSel(strText);
}
|
NOTA: in editiile mai noi de MSDN aceasta greseala a fost corectata.
Cum adaug text la un edit control?
Problema
O metoda des utilizata este de a scoate textul initial cu GetWindowText pentru a adauga textul, apoi a-l pune inapoi cu SetWindowText.
Exemplu
| Citat: |
CString strText;
edit.GetWindowText(strText);
strText += pszNewText;
edit.SetWindowText(strText);
|
Totusi, mai ales in cazul edit-urilor multiline, daca textul din edit este foarte lung iar adaugarile se fac des, metoda nu este chiar asa grozava.
Rezolvare
Pentru controlul edit setam selectia la sfarsitul textului existent apoi o inlocuim cu textul de adaugat. E mai repede si nici nu e nevoie de un string intermediar.
Exemplu
| Citat: |
// get the text length
const int nLength = edit.GetWindowTextLength();
// set the selection at the end of text
edit.SetSel(nLength, nLength);
// replace selection (append the text).
edit.ReplaceSel(pszNewText);
|
Rich Edit control - rule #0
Problema
Tocmai am pus intr-un dialog un contro tip rich edit. Aplicatia se compileaza bine-mersi, dar cand rulez programul, dialogul "refuza" sa mai apara.
Rezolvare
S-a uitat sau n-am stiut regula #0: controlul trebuie initializat cu AfxInitRichEdit.
Exemplu
| Citat: |
BOOL CMyApp::InitInstance()
{
AfxInitRichEdit();
// ...
}
|
|
|
|
|
|
|
|
Fa-ti si tu propriul forum GRATUIT, pe www.myforum.ro
|
|