bonjour à tous !
voilà un code en Builder C++ qui permet de connaitre le mot sous le curseur
Mais il ya un petit bug :
Il ya une violation d'adresse quand je ressort de mon RichEdit ou je Rentre depuis le coin haut où le point du curseur vaut 0,0
voila l'ébauche de la fonction
la question est donc : c'est qui beuge et y a t il un solution?
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58 String GetWordUnderCursor(TRichEdit *RichEdit ) { int DebutMot, FinMot, CharIndex, LineIndex, CharOffset; TPoint Pt=TPoint(); String Result = ""; Pt = Mouse->CursorPos; Pt = RichEdit->ScreenToClient(Pt); // Récupère le caractère sous le curseur // (La fonction retourne -1 si elle échoue) CharIndex = SendMessage(RichEdit->Handle, EM_CHARFROMPOS, 0, int(&Pt)); // Form1->Label1->Caption=" char index "+ IntToStr(CharIndex); //ShowMessage(IntToStr(CharIndex)); if (CharIndex !=-1 ) { Form1->Label1->Caption=" char index "+ IntToStr(CharIndex); Form1->Label2->Caption =RichEdit->Text[CharIndex]; // Si le caractère est valide, on teste les caractères adjacents pour // savoir si le mot sous le curseur if (RichEdit->Text[CharIndex]!=' '|| RichEdit->Text[CharIndex]!='\n'){ // Récupère l'index de la ligne LineIndex = RichEdit->Perform(EM_EXLINEFROMCHAR, 0, LPARAM(CharIndex)); //Récupère la position du caractère depuis le début de la ligne CharOffset = CharIndex - RichEdit->Perform(EM_LINEINDEX, WPARAM(LineIndex), 0); Form1->Label3->Caption="Line Index"+IntToStr(LineIndex); Form1->Label4->Caption="Char OffSet "+IntToStr(CharOffset); // Récupère le mot sous le curseur if (((RichEdit->Lines->Strings[LineIndex].Length()) > 0)&&(RichEdit->Lines->Strings[LineIndex].Length()>CharOffset)){ // La partie gauche du mot DebutMot = CharOffset + 1; Form1->Label5->Caption=IntToStr(CharOffset); while (DebutMot > 0){ if (RichEdit->Lines->Strings[LineIndex][DebutMot]!=' ' ) DebutMot = DebutMot - 1 ; else break; } // La partie droite FinMot = CharOffset + 1; while (FinMot < (RichEdit->Lines->Strings[LineIndex].Length()) ){ { if (RichEdit->Lines->Strings[LineIndex][FinMot]!=' ' ) FinMot = FinMot + 1 ; else break; } } // On copie le mot dans Result for (int i=DebutMot;i<=FinMot-1;i++) Result+= RichEdit->Lines->Strings[LineIndex].c_str()[i]; } } } return Result; }
Cordialement
Partager