I'm reading a Richbox of another app from my app, works fine until I try to get each line in a loop, I'm sure my mistake is in the loop because manually editing nbucle without the loop this works perfectly ...
DWORD dwlineas = SendMessage(hListWnd,EM_GETLINECOUNT,0,0); // Get the number of lines of RichTextBox
int nbucle=0;
while(nbucle<=dwlineas){ // lo inicio
DWORD dwCount = SendMessage(hListWnd,EM_LINELENGTH,0,0); // get the length of the line
if(dwCount){ // si todo esta correcto
char* lpBuff = newchar[dwCount+1]; // I think that is a pointer to the length of the line +1
if(!lpBuff){ // if no buffer, error
MessageBox(NULL,"Error with memory!",NULL,MB_OK);
return 1;
}
if(LB_ERR != SendMessage(hListWnd,EM_GETLINE,nbucle,(LPARAM)lpBuff)){ // if no error, I select the line from nbucle
cout<< "Bucle: " << nbucle << endl; // gives me the number of loop
cout << lpBuff << endl; // print line
}
delete [] lpBuff; // I clear the buffer pointer
}
//
nbucle++; // a high loop
Sleep(2000); // wait 2 seconds
}
The code works only with the first line, if I remove the "while" and put other lines work. Anyone can help me?, sorry about my english, thanks.
SendMessage(hListWnd, EM_LINELENGTH, 0, 0);
Quoting MSDN, wParam is:
The character index of a character in the line whose length is to be retrieved. If this parameter is greater than the number of characters in the control, the return value is zero.
This means you need to track the char position too:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
DWORD dwlineas = SendMessage(hListWnd,EM_GETLINECOUNT,0,0);
int nbucle=0;
DWORD dwCharPos = 0;while(nbucle<=dwlineas){ // lo inicio
DWORD dwCount = SendMessage(hListWnd,EM_LINELENGTH,dwCharPos,0);
if(dwCount){ // si todo esta correcto
dwCharPos += dwCount;char* lpBuff = newchar[dwCount+1];
if(!lpBuff){ // if no buffer, error
MessageBox(NULL,"Error with memory!",NULL,MB_OK);
return 1;
}
if(LB_ERR != SendMessage(hListWnd,EM_GETLINE,nbucle,(LPARAM)lpBuff)){
cout<< "Bucle: " << nbucle << endl; // gives me the number of loop
cout << lpBuff << endl; // print line
}
delete [] lpBuff;
}