Come faccio a dare il RichTextBox un'occhiata piatta?
-
22-08-2019 - |
Domanda
Sto lavorando su un'applicazione WinForms SmartClient, che utilizza un sacco di controlli RichTextBox - alcuni nel luogo del controllo TextBox regolare per vari motivi. Purtroppo la RichTextBox richiama il brutto confine Win95 3D al posto del confine di stile XP o Vista a tema.
Qualcuno sa di un modo per applicare il bordo a tema al RichTextBox? Non mi importa di loro sottoclassi per questo scopo.
Grazie!
Soluzione
Questo è veramente un hack, ma una cosa che puoi fare è rilasciare un controllo Panel nella pagina. Dategli un BorderStyle di FixedSingle (sarà None per impostazione predefinita.)
Drop la RichTextBox nel pannello e impostare il BorderStyle a nessuno. Quindi impostare la proprietà Dock of the RichTextBox da riempire.
Questo vi darà un RichTextBox con un bordo piatto.
Altri suggerimenti
Indietro nel giorno ho dovuto risolvere questo con un pannello in cui la casella di testo è un componente all'interno e aveva DockPadding impostata su 3 o 4 pixel. Mi piacerebbe poi che lo stile del pannello ad un singolo pixel.
Ho sempre trovato questo davvero fastidioso!
Credo che questo è un po 'troppo tardi, ma comunque.
È sempre possibile utilizzare P / Invoke e sottoclasse tua RichEdit con UXTheme API che modo è possibile attivare / disattivare come si desidera.
Credo CodeProject avuto un controllo RichEdit che ha usato UXTheme / visivo stili API
Visual Styles UXTheme API possono diventare obsoleti o deprecato come Windows 8 sta lanciando
Ecco codice VB.NET, si gonfia la zona nonclient con -1 e quindi riempie l'area nonclient con blu caded. Esso può essere convertito in C # utilizzando SharpDevelop 4.4. Ho ricavato il codice da questo articolo:
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
Il modo più semplice per sbarazzarsi del 3D-confine è quello di impostare un altro:
richTextBox.BorderStyle = BorderStyle.FixedSingle;
Il FixedSingle
-BorderStyle è il più vicino al FlatStyle
di esempio Pulsante