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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
| Option Explicit
'--------------------- Trouvé sur DVP ---------------------
Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" ( _
ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByRef lParam As Any) As Long
Private Const WM_PASTE As Long = &H302
'-----------------------------------------------------------
Dim R As Byte, G As Byte, B As Byte
Dim T As Long, U As Long, V As Long
Dim MotRechercher As String
Private Sub Frame1_Click()
If Frame1.Height = 255 Then
Frame2.Height = 255: Frame2.Width = 2505
Frame1.Height = 540: Frame1.Width = 5385: Frame1.ZOrder
Else
Frame1.Height = 255: Frame1.Width = 2505
End If
End Sub
Private Sub Frame2_Click()
If Frame2.Height = 255 Then
Frame1.Height = 255: Frame1.Width = 2505
Frame2.Height = 540: Frame2.Width = 5385: Frame2.ZOrder
Else
Frame2.Height = 255: Frame2.Width = 2505
End If
End Sub
Private Sub LabCoul_Click(Index As Integer)
'choix de la couleur pour le surlignage ou de l'encre
Select Case Index
Case 0, 23: LabCoul(Index).BackColor = &HFFFFFF: Exit Sub
Case Is < 23
LabCoul(0).BackColor = LabCoul(Index).BackColor
Frame1.Height = 255: Frame1.Width = 2505
Case Is > 22
LabCoul(23).BackColor = LabCoul(Index).BackColor
Frame2.Height = 255: Frame2.Width = 2505
End Select
End Sub
Private Sub MenuPolice_Click()
Dim NomPolice As String
Dim SiezePolice As Integer
Dim BoldPolice As Boolean
Dim ItalicPolice As Boolean
Dim StrikeThruPolice As Boolean
Dim UnderlinePolice As Boolean
If RichTextBox1.SelLength <> 0 Then
If IsNull(RichTextBox1.SelFontName) Then
NomPolice = RichTextBox1.Font.Name
SiezePolice = RichTextBox1.Font.Size
BoldPolice = RichTextBox1.Font.Bold
ItalicPolice = RichTextBox1.Font.Italic
StrikeThruPolice = RichTextBox1.Font.Strikethrough
UnderlinePolice = RichTextBox1.Font.Underline
Else
NomPolice = RichTextBox1.SelFontName
SiezePolice = RichTextBox1.SelFontSize
If Not IsNull(RichTextBox1.SelBold) Then
BoldPolice = RichTextBox1.SelBold
ItalicPolice = RichTextBox1.SelItalic
StrikeThruPolice = RichTextBox1.SelStrikeThru
UnderlinePolice = RichTextBox1.SelUnderline
Else
BoldPolice = RichTextBox1.Font.Bold
ItalicPolice = RichTextBox1.Font.Italic
StrikeThruPolice = RichTextBox1.Font.Strikethrough
UnderlinePolice = RichTextBox1.Font.Underline
End If
End If
Else
NomPolice = RichTextBox1.Font.Name
SiezePolice = RichTextBox1.Font.Size
BoldPolice = RichTextBox1.Font.Bold
ItalicPolice = RichTextBox1.Font.Italic
StrikeThruPolice = RichTextBox1.Font.Strikethrough
UnderlinePolice = RichTextBox1.Font.Underline
End If
CommonDialog1.Flags = cdlCFBoth Or cdlCFEffects
CommonDialog1.FontName = NomPolice
CommonDialog1.FontSize = SiezePolice
CommonDialog1.FontBold = BoldPolice
CommonDialog1.FontItalic = ItalicPolice
CommonDialog1.FontStrikethru = StrikeThruPolice
CommonDialog1.FontUnderline = UnderlinePolice
'CommonDialog1.Color = ColorPolice
CommonDialog1.CancelError = True
On Error Resume Next
CommonDialog1.ShowFont
If Err Then Exit Sub
If RichTextBox1.SelLength <> 0 Then
RichTextBox1.SelFontName = CommonDialog1.FontName
RichTextBox1.SelFontSize = CommonDialog1.FontSize
RichTextBox1.SelBold = CommonDialog1.FontBold
RichTextBox1.SelItalic = CommonDialog1.FontItalic
RichTextBox1.SelStrikeThru = CommonDialog1.FontStrikethru
RichTextBox1.SelUnderline = CommonDialog1.FontUnderline
Else
RichTextBox1.Font.Name = CommonDialog1.FontName
RichTextBox1.Font.Size = CommonDialog1.FontSize
RichTextBox1.Font.Bold = CommonDialog1.FontBold
RichTextBox1.Font.Italic = CommonDialog1.FontItalic
RichTextBox1.Font.Strikethrough = CommonDialog1.FontStrikethru
RichTextBox1.Font.Underline = CommonDialog1.FontUnderline
End If
End Sub
Private Sub RichTextBox1_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
If Button = 1 And Shift = 2 Then 'Bt. Gauche + Ctrl
SurligneSelection RichTextBox1
RichTextBox1.SelLength = 0
End If
End Sub
Private Sub RichTextBox1_SelChange()
If RichTextBox1.SelLength = 0 Then
SmenuSurLigne(0).Enabled = False: SmenuColorer(0).Enabled = False
SmenuSelrtF(0).Enabled = False
Else
SmenuSurLigne(0).Enabled = True: SmenuColorer(0).Enabled = True
SmenuSelrtF(0).Enabled = True
End If
End Sub
Private Sub Smenufichier_Click(Index As Integer)
On Error GoTo GestErr
CommonDialog1.Filter = "Texte Formaté rtf (*.RTF)|*.RTF|Texte brut(*.txt)|*.txt"
CommonDialog1.Filter = CommonDialog1.Filter & "|Images (BMP,CUR,ICO,RLE,EMF,WMF,GIF,JPG)|*.BMP;*.CUR;*.ICO;*.RLE;*.EMF;*.WMF;*.GIF;*.JPG"
CommonDialog1.Filter = CommonDialog1.Filter & "|Tous fichiers(*.*)|*.*"
CommonDialog1.FilterIndex = 1
CommonDialog1.CancelError = True
Select Case Index
Case 0 'ouvrir
CommonDialog1.DialogTitle = "Ouvrir"
CommonDialog1.ShowOpen
Select Case Right(UCase(CommonDialog1.FileTitle), 3)
Case "RTF"
RichTextBox1.LoadFile CommonDialog1.FileName, rtfRTF
Case "BMP", "CUR", "ICO", "RLE", "WMF", "EMF", "GIF", "JPG"
RichTextBox1.TextRTF = ""
Clipboard.Clear: Clipboard.SetData LoadPicture(CommonDialog1.FileName)
SendMessage RichTextBox1.hwnd, WM_PASTE, 0, 0
Case Else '"TXT"
RichTextBox1.LoadFile CommonDialog1.FileName, rtfText
End Select
Case 1 'Insérer
CommonDialog1.DialogTitle = "Insérer"
CommonDialog1.ShowOpen
Select Case Right(UCase(CommonDialog1.FileTitle), 3)
Case "RTF"
Dim MemoRTF As String, NewRTF As String
MemoRTF = RichTextBox1.TextRTF
OldSelStart = RichTextBox1.SelStart
RichTextBox1.LoadFile CommonDialog1.FileName, rtfRTF
NewRTF = RichTextBox1.TextRTF
RichTextBox1.TextRTF = MemoRTF
RichTextBox1.SelStart = OldSelStart
RichTextBox1.SelRTF = NewRTF
Case "BMP", "CUR", "ICO", "RLE", "WMF", "EMF", "GIF", "JPG"
Clipboard.Clear: Clipboard.SetData LoadPicture(CommonDialog1.FileName)
SendMessage RichTextBox1.hwnd, WM_PASTE, 0, 0
Case Else '"TXT"
Dim NumFich As Integer
NumFich = FreeFile
Open CommonDialog1.FileName For Input As #NumFich
MemoRTF = Input(FileLen(CommonDialog1.FileName), NumFich)
Close #NumFich
RichTextBox1.SelText = MemoRTF
End Select
Case 2 'enregistrer
CommonDialog1.DialogTitle = "Enregistrer"
CommonDialog1.ShowSave
If Right(UCase(CommonDialog1.FileTitle), 3) = "RTF" Then
RichTextBox1.SaveFile CommonDialog1.FileName, rtfRTF
Else
RichTextBox1.SaveFile CommonDialog1.FileName, rtfText
End If
Case 3 'imprimer
CommonDialog1.DialogTitle = "Imprimer"
CommonDialog1.ShowPrinter
Case 5: Unload Me 'quitter
End Select
Exit Sub
GestErr:
End Sub
Private Sub SmenuRechercher_Click(Index As Integer)
Select Case Index
Case 0 'Rechercher pas à pas
Case 1 'Suivant
Case 2 'Dans tout le document
MotRechercher = InputBox("Mot à rechercher ?", "Rechercher dans tout le document")
If Trim(MotRechercher) = "" Then Exit Sub
Rechercher
Case 3 'Supprimer le sur-lignage
If Trim(MotRechercher) = "" Then Exit Sub
Dim OlcoulSurlign As OLE_COLOR, OldCoulEncre As OLE_COLOR
'memorisation des couleur pour retablir par la suite
OlcoulSurlign = LabCoul(0).BackColor: OldCoulEncre = LabCoul(23).BackColor
LabCoul(0).BackColor = RichTextBox1.BackColor: LabCoul(23).BackColor = RichTextBox1.SelColor
Rechercher
'retablissment des choix de couleur
LabCoul(0).BackColor = OlcoulSurlign: LabCoul(23).BackColor = OldCoulEncre
End Select
End Sub
Public Sub Rechercher()
Dim Fin As Long, PosTrouver As Long
RichTextBox1.SelStart = 0: RichTextBox1.SelLength = 64000
Fin = RichTextBox1.SelLength: RichTextBox1.SelLength = 0
PosTrouver = 1
Do While PosTrouver <= Fin
PosTrouver = RichTextBox1.Find(MotRechercher, PosTrouver, , rtfWholeWord)
If PosTrouver <> -1 Then
SurligneSelection RichTextBox1
PosTrouver = PosTrouver + Len(MotRechercher)
Else
Exit Do
End If
DoEvents
Loop
End Sub
Private Sub SmenuSelrtF_Click(Index As Integer)
If Index = 0 Then 'copier
Clipboard.Clear: DoEvents: Clipboard.SetText RichTextBox1.SelRTF
Else 'coller
RichTextBox1.SelRTF = Clipboard.GetText
End If
End Sub
Private Sub SmenuColorer_Click(Index As Integer)
If Index = 0 Then RichTextBox1.SelColor = LabCoul(23).BackColor
End Sub
Private Sub SmenuSurLigne_Click(Index As Integer)
Select Case Index
Case 0 ' la sélection
OldSelStart = RichTextBox1.SelStart: OldLen = RichTextBox1.SelLength
SurligneSelection RichTextBox1
RichTextBox1.SelStart = OldSelStart: RichTextBox1.SelLength = OldLen: RichTextBox1.SetFocus
Case 1 ' la ligne
'dans un premier temps, positionner le curseur sur la ligne a surligner
SurligneLaLigne RichTextBox1, RichTextBox1.GetLineFromChar(RichTextBox1.SelStart)
Case 2 ' A la souris (bt.G + CTRL)
End Select
End Sub
Private Sub Form_Resize()
If Me.WindowState <> vbMinimized Then
On Error Resume Next
RichTextBox1.Move 30, 255, Me.Width - 165, Me.Height - 960
If Err.Number Then Exit Sub
End If
End Sub
Private Sub Form_Load()
'placement, dimensionnement des composants
RichTextBox1.Move 30, 915, 8205, 4515: RichTextBox1.AutoVerbMenu = True
Frame1.Move 45, 0, 5385, 255
Frame1.FontBold = True
Frame1.Caption = "Couleur de surlignage ->"
LabCoul(0).Caption = "": LabCoul(0).Move 2220, 45, 225, 135
LabCoul(0).ToolTipText = "Un click pour surligner blanc"
LabCoul(1).Caption = "": LabCoul(1).Move 60, 270, 225, 225
For T = 2 To 22
LabCoul(T).Move LabCoul(T - 1).Left + 240, 270, 225, 225
LabCoul(T).Caption = ""
Next T
Frame2.Move 2610, 0, 5385, 255
Frame2.FontBold = True
Frame2.Caption = "Couleur pour le texte ->"
LabCoul(23).Caption = "": LabCoul(23).Move 2220, 45, 225, 135
LabCoul(23).ToolTipText = "Un click pour encre blanc"
LabCoul(24).Caption = "": LabCoul(24).Move 60, 270, 225, 225
For T = 25 To LabCoul.Count - 1
LabCoul(T).Move LabCoul(T - 1).Left + 240, 270, 225, 225
LabCoul(T).Caption = ""
Next T
Me.Width = 8415: Me.Height = 5520
'initialisation
LabCoul_Click 5
LabCoul_Click 26
RichTextBox1.TextRTF = ""
'3 exemples de texte non surligné
''pas de surlignage ni de couleur pour l'encre
'RichTextBox1.SelText = "Une phrase sens couleur" & vbCrLf
'
'Dim Msg$
''2 phrases avec couleur encre et polices différentes .....
'Msg$ = "{\rtf1\ansi\ansicpg1252\deff0\deflang1036{\fonttbl{\f0\fnil\fcharset0 Arial;}}"
'Msg$ = Msg$ & "{\colortbl ;\red255\green0\blue0;\red0\green192\blue0;\red0\green0\blue255;}"
'Msg$ = Msg$ & "\uc1\pard\cf1\b\fs20 ROUGE \cf2 VERT \cf3 BLEU}"
'RichTextBox1.SelRTF = Msg$
'
'RichTextBox1.SelText = vbCrLf
'Msg$ = "{\rtf1\ansi\ansicpg1252\deff0\deflang1036{\fonttbl{\f0\fnil\fcharset0 Arial;}{\f1\fnil\fcharset2 Webdings;}}"
'Msg$ = Msg$ & "{\colortbl ;\red0\green192\blue0;\red0\green0\blue192;\red192\green0\blue192;}"
'Msg$ = Msg$ & "\uc1\pard\f0\fs17 Texte noir, \b EN GRAS\b0 , \cf1 VERT\cf0 , \cf2\b\f1\fs28 autre fonte\b0 ,\cf0\f0\fs17 \b\i EN GRAS, \cf3\fs32 italic}"
'RichTextBox1.SelRTF = Msg$
End Sub
Public Sub SurligneLaLigne(TextRichBox As RichTextBox, NumLigne As Integer)
Dim NumCaractDeb As Long, NumCaractFin As Long
OldSelStart = TextRichBox.SelStart: OldLen = TextRichBox.SelLength
'recherche du premier et dernier caractere de la ligne
TextRichBox.SelStart = 0: TextRichBox.SelLength = 640000
NumCaractDeb = -1: NumCaractFin = TextRichBox.SelLength
For T = 0 To NumCaractFin
If TextRichBox.GetLineFromChar(T) = NumLigne Then
If NumCaractDeb = -1 Then NumCaractDeb = T
End If
If NumCaractDeb <> -1 And TextRichBox.GetLineFromChar(T) > NumLigne Then
NumCaractFin = T
Exit For
End If
Next T
TextRichBox.SelStart = NumCaractDeb: TextRichBox.SelLength = NumCaractFin - NumCaractDeb
SurligneSelection RichTextBox1
TextRichBox.SelStart = OldSelStart: TextRichBox.SelLength = OldLen: TextRichBox.SetFocus
End Sub
Public Sub SurligneSelection(TextRichBox As RichTextBox)
If TextRichBox.SelLength = 0 Then Exit Sub
Dim Memo As String 'pour contenir le TextRTF
Dim DebPara As String 'pour contenir la 1° ligne
Dim LignTblCouleur As String 'pour contenir la 2° ligne formatage couleur
Dim FinPara As String 'pour contenir la/les ligne(s) formatage texte
Memo = TextRichBox.SelRTF 'Memo du seltext y compris le formatage RTF
T = InStr(1, Memo, vbCrLf) 'Recherche de la fin de la 1°ligne
DebPara = Left(Memo, T + 1) 'recuperation de la 1° ligne
T = T + 2
If InStr(T, Memo, "{\colortbl") <> 0 Then
'il y a deja un formatage couleur
U = InStr(T, Memo, vbCrLf) 'recherche de la fin de la 2° ligne
LignTblCouleur = Mid$(Memo, InStr(Memo, "{\colortbl"), U - T) 'recuperation de la 2° ligne
'recuperation de(s) ligne(s) de texte formaté
FinPara = Right$(Memo, Len(Memo) - (Len(DebPara) + Len(LignTblCouleur) + 2))
Dim MemoLgnColor As String
MemoLgnColor = LignTblCouleur
'pour obtenir le nombre de couleur deja present
LignTblCouleur = Replace(LignTblCouleur, "{\colortbl ;", "")
LignTblCouleur = Replace(LignTblCouleur, ";}", "")
Dim TableauCouleur() As String
TableauCouleur = Split(LignTblCouleur, ";")
'effacement de tous le(s) surlignage(s) existant(s)
FinPara = Replace(FinPara, "\highlight0 ", "")
FinPara = Replace(FinPara, "\highlight0", "")
For V = LBound(TableauCouleur) To UBound(TableauCouleur)
FinPara = Replace(FinPara, "\highlight" & CStr(V + 1) & " ", "")
FinPara = Replace(FinPara, "\highlight" & CStr(V + 1), "")
Next V
LignTblCouleur = MemoLgnColor 'recuperation du formatage d'entrée
'effacement du caractére fin de formatage ligne couleur
LignTblCouleur = Replace(LignTblCouleur, "}", "")
Else
'pas de couleur ni pour l'encre, ni pour un surlignage
FinPara = Right$(Memo, Len(Memo) - Len(DebPara))
LignTblCouleur = "{\colortbl ;" 'entête ligne formatage couleur
V = 0
End If
'ajout au formatage la couleur de surlignage
R = CStr(LabCoul(0).BackColor And &HFF&) 'recuperation de la composante rouge
G = CStr((LabCoul(0).BackColor And &HFF00&) / 2 ^ 8) '.... vert
B = CStr((LabCoul(0).BackColor And &HFF0000) / 2 ^ 16) '.... bleu
LignTblCouleur = LignTblCouleur & _
"\red" & CStr(R) & "\green" & CStr(G) & "\blue" & CStr(B) & _
";}" & vbCrLf
'ajout du formatage surlignage
FinPara = "\highlight" & CStr(V + 1) & FinPara
'recomposition du SelTextRTF
Memo = DebPara & LignTblCouleur & FinPara
TextRichBox.SelRTF = Memo
End Sub |
Partager