2009-03-31 19 views
14

Estoy trabajando en una aplicación WinForms SmartClient, que usa muchos controles RichTextBox, algunos en lugar del TextBox regular por varias razones. Desafortunadamente, el RichTextBox dibuja el feo borde 3D de Win95 en lugar del borde temático de estilo XP o Vista.¿Cómo le doy a RichTextBox un aspecto plano?

¿Alguien sabe de una forma de aplicar el borde temático a RichTextBox? No me importa subclasificarlos para este propósito.

Gracias!

+1

realmente - no hay respuesta para esto? ... que es una mierda –

+0

¡Buena pregunta con una desafortunada respuesta! –

Respuesta

27

Esto es realmente un truco, pero una cosa que puede hacer es soltar un control Panel en la página. Dale un BorderStyle de FixedSingle (no será None por defecto).

Suelta tu RichTextBox en el panel y configura BorderStyle en none. A continuación, establezca la propiedad Dock de RichTextBox en Fill.

Esto le dará un RichTextBox con un borde plano.

+1

Esto debería ser aceptado como la respuesta. No es un método directo, pero funciona. – Isuru

+0

Seguí este consejo, excepto que usé un cuadro de grupo. +1 – contactmatt

2

De regreso en el día tuve que resolver esto con un panel donde el cuadro de texto es un componente interno y tenía DockPadding configurado en 3 o 4 píxeles. Luego, le daría estilo a ese panel en un solo píxel.

¡Siempre encontré esto realmente molesto!

+2

Esto es realmente molesto y tiene que haber una mejor manera, ya que esto no da el verdadero tema de cuadro de texto. – grover

0

Aquí es código VB.NET, se infla el área no cliente con -1 y luego llena el área no cliente con caded azul. Se puede convertir a C# utilizando SharpDevelop 4.4. Derivé el código de este artículo:

http://www.codeproject.com/Articles/13723/Themed-RichTextBox-A-RichTextBox-with-XP-styled-bo

Imports System.Runtime.InteropServices 
Imports System.Windows.Forms 
Imports System.Windows.Forms.VisualStyles 
Imports System.Drawing 
Imports System.Diagnostics 

Public Class FlatRichTextBox 
    Inherits RichTextBox 

    Private BorderRect As RECT 

    Sub New() 
     If VisualStyleInformation.IsEnabledByUser Then 
      BorderStyle = BorderStyle.None 
     End If 
    End Sub 

    Protected Overrides Sub WndProc(ByRef m As Message) 
     Const WM_NCPAINT = &H85 
     Const WM_NCCALCSIZE = &H83 
     Const WM_THEMECHANGED = &H31A 

     Select Case m.Msg 
      Case WM_NCPAINT 
       WmNcpaint(m) 
      Case WM_NCCALCSIZE 
       WmNccalcsize(m) 
      Case WM_THEMECHANGED 
       UpdateStyles() 
      Case Else 
       MyBase.WndProc(m) 
     End Select 
    End Sub 

    Private Sub WmNccalcsize(ByRef m As Message) 
     MyBase.WndProc(m) 

     If Not VisualStyleInformation.IsEnabledByUser Then Return 

     Dim par As New NCCALCSIZE_PARAMS() 
     Dim windowRect As RECT 

     If m.WParam <> IntPtr.Zero Then 
      par = CType(Marshal.PtrToStructure(m.LParam, GetType(NCCALCSIZE_PARAMS)), NCCALCSIZE_PARAMS) 
      windowRect = par.rgrc0 
     End If 

     Dim clientRect = windowRect 

     clientRect.Left += 1 
     clientRect.Top += 1 
     clientRect.Right -= 1 
     clientRect.Bottom -= 1 

     BorderRect = New RECT(clientRect.Left - windowRect.Left, 
           clientRect.Top - windowRect.Top, 
           windowRect.Right - clientRect.Right, 
           windowRect.Bottom - clientRect.Bottom) 

     If m.WParam = IntPtr.Zero Then 
      Marshal.StructureToPtr(clientRect, m.LParam, False) 
     Else 
      par.rgrc0 = clientRect 
      Marshal.StructureToPtr(par, m.LParam, False) 
     End If 

     Const WVR_HREDRAW = &H100 
     Const WVR_VREDRAW = &H200 
     Const WVR_REDRAW = (WVR_HREDRAW Or WVR_VREDRAW) 

     m.Result = New IntPtr(WVR_REDRAW) 
    End Sub 

    Private Sub WmNcpaint(ByRef m As Message) 
     MyBase.WndProc(m) 

     If Not VisualStyleInformation.IsEnabledByUser Then Return 

     Dim r As RECT 
     GetWindowRect(Handle, r) 

     r.Right -= r.Left 
     r.Bottom -= r.Top 
     r.Top = 0 
     r.Left = 0 

     r.Left += BorderRect.Left 
     r.Top += BorderRect.Top 
     r.Right -= BorderRect.Right 
     r.Bottom -= BorderRect.Bottom 

     Dim hDC = GetWindowDC(Handle) 
     ExcludeClipRect(hDC, r.Left, r.Top, r.Right, r.Bottom) 

     Using g = Graphics.FromHdc(hDC) 
      g.Clear(Color.CadetBlue) 
     End Using 

     ReleaseDC(Handle, hDC) 
     m.Result = IntPtr.Zero 
    End Sub 

    <DllImport("user32.dll")> 
    Public Shared Function GetWindowRect(hWnd As IntPtr, ByRef lpRect As RECT) As Boolean 
    End Function 

    <DllImport("user32.dll")> 
    Public Shared Function GetWindowDC(hWnd As IntPtr) As IntPtr 
    End Function 

    <DllImport("user32.dll")> 
    Public Shared Function ReleaseDC(hWnd As IntPtr, hDC As IntPtr) As Integer 
    End Function 

    <DllImport("gdi32.dll")> 
    Public Shared Function ExcludeClipRect(hdc As IntPtr, nLeftRect As Integer, nTopRect As Integer, nRightRect As Integer, nBottomRect As Integer) As Integer 
    End Function 

    <StructLayout(LayoutKind.Sequential)> 
    Public Structure NCCALCSIZE_PARAMS 
     Public rgrc0, rgrc1, rgrc2 As RECT 
     Public lppos As IntPtr 
    End Structure 

    <StructLayout(LayoutKind.Sequential)> 
    Public Structure RECT 
     Public Left As Integer 
     Public Top As Integer 
     Public Right As Integer 
     Public Bottom As Integer 

     Public Sub New(left As Integer, top As Integer, right As Integer, bottom As Integer) 
      Me.Left = left 
      Me.Top = top 
      Me.Right = right 
      Me.Bottom = bottom 
     End Sub 
    End Structure 
End Class 
0

La forma más fácil de deshacerse de la 3D-frontera es fijar otro:

richTextBox.BorderStyle = BorderStyle.FixedSingle; 

El -BorderStyle FixedSingle es el el más cercano al FlatStyle de por ejemplo Botón

Cuestiones relacionadas