Home > Blockchain >  Meaning of the parameters for EM_EXLINEFROMCHAR
Meaning of the parameters for EM_EXLINEFROMCHAR

Time:02-04

The parameter for EM_EXLINEFROMCHAR and EM_LINEFROMCHAR are different.

While I can find the example of the latter the former does not produce any useful links.

What I wonder is - if I pass -1 just like in EM_LINEFROMCHAR to EM_EXLINEFROMCHAR for the current line where the caret is located will it work properly. MSDN unfortunately doesn't explain it.

TIA!!

CodePudding user response:

Unfortunately, the documentation for EM_EXLINEFROMCHAR says only the following about the input parameters:

wParam
This parameter is not used; it must be zero

lParam
Zero-based index of the character.

There is no mention of whether -1 is allowed or not, so there is no guaranteed whether it will work or not. Best not to take any chances. Simply don't pass in -1, pass in the actual caret position, which is very trivial to get using EM_GETSEL or EM_EXGETSEL, eg:

DWORD pos = 0;
SendMessage(hwnd, EM_GETSEL, (WPARAM)&pos, 0);
int lineIndex = SendMessage(hwnd, EM_EXLINEFROMCHAR, 0, pos);
CHARRANGE rng = {};
SendMessage(hwnd, EM_EXGETSEL, 0, (LPARAM)&rng);
int lineIndex = SendMessage(hwnd, EM_EXLINEFROMCHAR, 0, rng.cpMin);

Whereas EM_LINEFROMCHAR is explicitly documented, and thus contractually obligated, to accept -1 as input:

wParam
The character index of the character contained in the line whose number is to be retrieved. If this parameter is -1, EM_LINEFROMCHAR retrieves either the line number of the current line (the line containing the caret) or, if there is a selection, the line number of the line containing the beginning of the selection.

lParam
This parameter is not used.

int lineIndex = SendMessage(hwnd, EM_LINEFROMCHAR, (WPARAM)-1, 0);
  •  Tags:  
  • Related