none
RichTextBox mit Hintergrund? RRS feed

  • Frage

  • Hallo,

    ich würde gerne wissen, wie man einer RichTextBox einen Background verpasst. Ich hoffe, dass das überhaupt funktioniert.

     MFG Der-Fuchs10
    Dienstag, 5. Januar 2010 15:28

Antworten

  • Hallo Der-Fuchs10,

    In VB.NET würde das so gehen:

    1.     Ein neues Windows Forms Applikation Projekt erstellen.

    2.     Eine neue Klasse zum Projekt hinzufügen.

    3.     Klasse speichern und Build

    4.     Die Custom Kontrolle auf die Form bringen.

    Code für die Klasse (erstellt eine anwendungsspezifische RichTextBox Kontrolle:


    Imports System
    Imports System.Windows.Forms
    Imports System.Runtime.InteropServices
    Public Class _RichTextBox
    
        Inherits RichTextBox
    
    #Region "Params"
    
        <DllImport("kernel32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
        Public Shared Function LoadLibrary(ByVal path As String) As IntPtr
        End Function
    
        Private Shared moduleHandle As IntPtr
    
        Protected Overrides ReadOnly Property CreateParams() As  _
        System.Windows.Forms.CreateParams
    
            Get
                If moduleHandle = IntPtr.Zero Then
                    moduleHandle = LoadLibrary("msftedit.dll")
                    If CType(moduleHandle, Integer) < &H20 Then
                        Throw New System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error(), _
                        "Could not load Msftedit.dll")
    
                    End If
                End If
    
                Dim cParams As CreateParams = MyBase.CreateParams
                cParams.ClassName = "RichEdit50W"
    
    
                If Me.Multiline = True Then
                    If ((Me.ScrollBars And RichTextBoxScrollBars.Horizontal) <> _
                        RichTextBoxScrollBars.None) _
                        AndAlso MyBase.WordWrap = False Then
    
                        cParams.Style = cParams.Style Or &H100000
    
                        If (Me.ScrollBars And CType(&H10, RichTextBoxScrollBars)) <> _
                        RichTextBoxScrollBars.None Then
    
                            cParams.Style = cParams.Style Or &H2000
    
                        End If
                    End If
                End If
    
                If (Me.ScrollBars And RichTextBoxScrollBars.Vertical) <> _
                    RichTextBoxScrollBars.None Then
    
                    cParams.Style = cParams.Style Or &H200000
    
                    If (Me.ScrollBars And CType(&H10, RichTextBoxScrollBars)) <> _
                    RichTextBoxScrollBars.None Then
    
                        cParams.Style = cParams.Style Or &H2000
    
                    End If
                End If
    
                If BorderStyle.FixedSingle = MyBase.BorderStyle AndAlso _
                    (cParams.Style And &H800000) <> 0 Then
    
                    cParams.Style = cParams.Style And -8388609
                    cParams.ExStyle = cParams.ExStyle Or &H200
    
                End If
    
    
    
                cParams.ExStyle = cParams.ExStyle Or &H20 '//WS_EX_TRANSPARENT = 0x20
    
                Return cParams
    
            End Get
        End Property
    
        Protected Overrides Sub OnPaintBackground(ByVal pevent As System.Windows.Forms.PaintEventArgs)
            ' MyBase.OnPaintBackground(pevent)
        End Sub
    
    
        Public Sub New()
            Me.SetStyle(ControlStyles.DoubleBuffer, True)
            Me.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
            '//Me.ScrollBars = RichTextBoxScrollBars.None
        End Sub
    #End Region
    
    
    
    End Class





    Die Custom Kontrolle auf die Form bringen.



     

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim mycontrol As New _RichTextBox
            Controls.Add(mycontrol)
    End Sub

     

    Grüße,

    Robert

    Freitag, 8. Januar 2010 20:33
  • Hallo,

    Du kannst nicht einfach Deine Hintergrund Ressource als Argument für die OnPaintBackground Methode übergeben. Diese Methode erwartet einen PaintEventArgs Typ. Wenn Du Deine Hintergrund Ressource zeichnen lassen möchtest, würdest Du das etwa über

    pevent.Graphics.DrawImage

    erledigen. Stell Dir das aber nicht zu einfach vor.


    Thorsten Dörfler
    Microsoft MVP Visual Basic
    Freitag, 8. Januar 2010 22:20

Alle Antworten

  • Hallo,

    Du kannst es mal versuchen mit:
    http://www.codeproject.com/KB/edit/AlphaBlendedTextControls.aspx
    (ist allerdings C#)

    Gruß Elmar
    Mittwoch, 6. Januar 2010 08:03
  • Hallo

    Leider kann ich C# sehr schlecht und bräuchte das ganze in einem VB.net Projekt. Kann man das ganze vielleicht in VB.net übersetzen?

    MFG Der-Fuchs10
    Donnerstag, 7. Januar 2010 15:15
  • Hallo Der-Fuchs10,

    In VB.NET würde das so gehen:

    1.     Ein neues Windows Forms Applikation Projekt erstellen.

    2.     Eine neue Klasse zum Projekt hinzufügen.

    3.     Klasse speichern und Build

    4.     Die Custom Kontrolle auf die Form bringen.

    Code für die Klasse (erstellt eine anwendungsspezifische RichTextBox Kontrolle:


    Imports System
    Imports System.Windows.Forms
    Imports System.Runtime.InteropServices
    Public Class _RichTextBox
    
        Inherits RichTextBox
    
    #Region "Params"
    
        <DllImport("kernel32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
        Public Shared Function LoadLibrary(ByVal path As String) As IntPtr
        End Function
    
        Private Shared moduleHandle As IntPtr
    
        Protected Overrides ReadOnly Property CreateParams() As  _
        System.Windows.Forms.CreateParams
    
            Get
                If moduleHandle = IntPtr.Zero Then
                    moduleHandle = LoadLibrary("msftedit.dll")
                    If CType(moduleHandle, Integer) < &H20 Then
                        Throw New System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error(), _
                        "Could not load Msftedit.dll")
    
                    End If
                End If
    
                Dim cParams As CreateParams = MyBase.CreateParams
                cParams.ClassName = "RichEdit50W"
    
    
                If Me.Multiline = True Then
                    If ((Me.ScrollBars And RichTextBoxScrollBars.Horizontal) <> _
                        RichTextBoxScrollBars.None) _
                        AndAlso MyBase.WordWrap = False Then
    
                        cParams.Style = cParams.Style Or &H100000
    
                        If (Me.ScrollBars And CType(&H10, RichTextBoxScrollBars)) <> _
                        RichTextBoxScrollBars.None Then
    
                            cParams.Style = cParams.Style Or &H2000
    
                        End If
                    End If
                End If
    
                If (Me.ScrollBars And RichTextBoxScrollBars.Vertical) <> _
                    RichTextBoxScrollBars.None Then
    
                    cParams.Style = cParams.Style Or &H200000
    
                    If (Me.ScrollBars And CType(&H10, RichTextBoxScrollBars)) <> _
                    RichTextBoxScrollBars.None Then
    
                        cParams.Style = cParams.Style Or &H2000
    
                    End If
                End If
    
                If BorderStyle.FixedSingle = MyBase.BorderStyle AndAlso _
                    (cParams.Style And &H800000) <> 0 Then
    
                    cParams.Style = cParams.Style And -8388609
                    cParams.ExStyle = cParams.ExStyle Or &H200
    
                End If
    
    
    
                cParams.ExStyle = cParams.ExStyle Or &H20 '//WS_EX_TRANSPARENT = 0x20
    
                Return cParams
    
            End Get
        End Property
    
        Protected Overrides Sub OnPaintBackground(ByVal pevent As System.Windows.Forms.PaintEventArgs)
            ' MyBase.OnPaintBackground(pevent)
        End Sub
    
    
        Public Sub New()
            Me.SetStyle(ControlStyles.DoubleBuffer, True)
            Me.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
            '//Me.ScrollBars = RichTextBoxScrollBars.None
        End Sub
    #End Region
    
    
    
    End Class





    Die Custom Kontrolle auf die Form bringen.



     

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim mycontrol As New _RichTextBox
            Controls.Add(mycontrol)
    End Sub

     

    Grüße,

    Robert

    Freitag, 8. Januar 2010 20:33
  • Hallo,

    erstmal vielen herzlichen Dank für diese schnelle und gute Antwort :)
    Leider mache ich wohl noch einen kleinen Fehler...

    bei:
    Protected Overrides Sub OnPaintBackground(ByVal pevent As System.Windows.Forms.PaintEventArgs)
            MyBase.OnPaintBackground(My.Resources.notiz_background)
        End Sub
    unterstreicht er mir mit blau "My.Resources.notiz_background" obwohl die Resource-Datei existiert.

    PS. Die Resource-Datei ist eine .jpg-Datei.

    MFG Der-Fuchs10
    Freitag, 8. Januar 2010 21:08
  • Hallo,

    Du kannst nicht einfach Deine Hintergrund Ressource als Argument für die OnPaintBackground Methode übergeben. Diese Methode erwartet einen PaintEventArgs Typ. Wenn Du Deine Hintergrund Ressource zeichnen lassen möchtest, würdest Du das etwa über

    pevent.Graphics.DrawImage

    erledigen. Stell Dir das aber nicht zu einfach vor.


    Thorsten Dörfler
    Microsoft MVP Visual Basic
    Freitag, 8. Januar 2010 22:20