我工作的一个WinForms SmartClient的应用程序,它使用了大量的RichTextBox控件 - 在一些地方出于各种原因经常文本框的。不幸的是,RichTextBox的画丑的Win95 3D边界,而不是主题XP或Vista风格的边框。

有谁知道的方式向边境主题应用到RichTextBox的?我不介意他们继承这个目的。

谢谢!

有帮助吗?

解决方案

这是一个真正的黑客,但有一两件事你可以做的是删除一个Panel控件到页面上。给它FixedSingle的边框(这将是无默认情况下。)

放下你的RichTextBox到所述面板和所述边框设置为无。然后设置在RichTextBox的Dock属性为Fill。

这会给你一个RichTextBox与平面的边缘。

其他提示

早在一天我不得不在文本框是内部的部件用面板来解决这个和不得不DockPadding设置为3或4个像素。然后我风格面板于单个像素。

我总觉得这真的很烦!

我想这是一个有点晚了,但尽管如此,

您可以随时使用的P / Invoke和继承与API的Uxtheme这样你可以禁用/启用它,如你所愿。

您的RichEdit

我认为有_或代码,其使用的RichEdit控件的Uxtheme /视觉样式API

视觉样式的Uxtheme API可能变得过时或弃用视窗8被推出

下面是VB.NET代码,它与充气-1非工作区,然后用填充蓝色caded非工作区。它可以转换为使用的SharpDevelop 4.4 C#。我得到的代码从本文:

的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

摆脱3D边界的最简单的方法是设置另一个问题:

richTextBox.BorderStyle = BorderStyle.FixedSingle;

FixedSingle-边框是最接近的例如所述FlatStyle按钮

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top