Bonsoir

je souhaiterais modifier la couleur d'arrière-plan d'un RichTextBox sans passer par sa propriété BackColor mais en utilisant SendMessage.
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
    <StructLayout(LayoutKind.Sequential)> _
    Public Structure STRUCT_CHARFORMAT
        Public cbSize As Integer
        Public dwMask As UInt32
        Public dwEffects As UInt32
        Public yHeight As Int32
        Public yOffset As Int32
        Public crTextColor As Int32
        Public bCharSet As Byte
        Public bPitchAndFamily As Byte
        <MarshalAs(UnmanagedType.ByValArray, SizeConst:=32)> _
        Public szFaceName() As Char
        Public wAlignment As Short
        Public wWeight As UInt16
        Public sSpacing As UInt16
        Public crBackColor As Integer
        Public lcid As Integer
        Public dwReserved As Integer
        Public sStyle As UInt16
        Public wKerning As UInt16
        Public bUnderlineType As Byte
        Public bAnimation As Byte
        Public bRevAuthor As Byte
        Public bReserved1 As Byte
    End Structure
 
Private Const EM_SETBKGNDCOLOR As Int32 = 1091
Private Const CFM_BACKCOLOR As Integer = 67108864
 
 Public Function setBackGroundColor(ByVal color As Integer) As Boolean
        Dim cf As New STRUCT_CHARFORMAT()
        cf.cbSize = Marshal.SizeOf(cf)
        cf.dwMask = Convert.ToUInt32(CFM_BACKCOLOR)
        cf.crBackColor = color
 
        Dim lParam As IntPtr
        lParam = Marshal.AllocCoTaskMem(Marshal.SizeOf(cf))
        Marshal.StructureToPtr(cf, lParam, False)
 
        Dim res As Integer
        res = SendMessage(Handle, EM_SETBKGNDCOLOR, 0, lParam)
        If (res = 0) Then
            Return True
        Else
            Return False
        End If
 
    End Function
la fonction setBackGroundColor est appelée ainsi :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
MyRTB.setBackGroundColor(ColorTranslator.ToWin32(ColorDialog.Color))
Pourtant la couleur d'arrière-plan affichée n'est pas celle renvoyée, et de plus cette couleur disparait quand je clique sur le RichTextBox.

Pourriez-vous me dire ce qui ne convient pas dans mon code ?
Merci.