如何使 RichTextBox 具有平面外观?

发布于 2024-07-16 01:23:58 字数 228 浏览 10 评论 0原文

我正在开发一个 WinForms SmartClient 应用程序,该应用程序使用大量 RichTextBox 控件 - 由于各种原因,其中一些控件代替了常规 TextBox。 不幸的是,RichTextBox 绘制了丑陋的 Win95 3D 边框,而不是主题 XP 或 Vista 样式边框。

有谁知道如何将主题边框应用于 RichTextBox? 我不介意为此目的对它们进行子类化。

谢谢!

I'm working on a WinForms SmartClient application, which uses a lot of RichTextBox controls - some in place of the regular TextBox for various reasons. Unfortunately the RichTextBox draws the ugly Win95 3D border instead of the themed XP or Vista style border.

Does anyone know of a way to apply the themed border to the RichTextBox? I don't mind subclassing them for this purpose.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

月下凄凉 2024-07-23 01:23:58

这确实是一种黑客攻击,但您可以做的一件事是将面板控件拖放到页面上。 将它的 BorderStyle 设置为 FixSingle(默认情况下为 None。)

将 RichTextBox 放入面板中,并将 BorderStyle 设置为 none。 然后将 RichTextBox 的 Dock 属性设置为 Fill。

这将为您提供一个具有平坦边框的 RichTextBox。

This is really a hack but one thing you can do is drop a Panel control onto the page. Give it a BorderStyle of FixedSingle (it will be None by default.)

Drop your RichTextBox into the panel and set the BorderStyle to none. Then set the Dock property of the RichTextBox to Fill.

This will give you a RichTextBox with a flat border.

空气里的味道 2024-07-23 01:23:58

以前,我必须使用一个面板来解决这个问题,其中文本框是内部的一个组件,并将 DockPadding 设置为 3 或 4 像素。 然后我将该面板的样式设置为单个像素。

我一直觉得这真的很烦人!

Back in the day I had to solve this with a panel where the text box is a component inside and had DockPadding set to 3 or 4 pixels. I'd then style that panel to a single pixel.

I always found this really annoying!

茶底世界 2024-07-23 01:23:58

这是 VB.NET 代码,它将非客户区域填充为 -1,然后用蓝色填充非客户区域。 可以使用 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

Here is VB.NET code, it inflates the nonclient area with -1 and then fills the nonclient area with caded blue. It can be converted to C# using SharpDevelop 4.4. I derived the code from this article:

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
优雅的叶子 2024-07-23 01:23:58

我想这有点太晚了,但尽管如此。

您始终可以使用 P/Invoke 并使用 UXTheme API 子类化您的 RichEdit,这样您就可以根据需要禁用/启用它。

我认为 CodeProject 有一个使用 UXTheme/Visual Styles API

随着 Windows 8 的推出,视觉样式 UXTheme API 可能会过时或弃用

I guess this is a little too late, but nonetheless.

You can always use P/Invoke and subclass your RichEdit with UXTheme API that way you can disable/enable it as you wish.

I think CodeProject had an richedit control which used UXTheme/Visual Styles API

Visual Styles UXTheme API may become obsolete or deprecated as Windows 8 is rolling out

故笙诉离歌 2024-07-23 01:23:58

[另一个黑客 (VB.NET)] 创建标签并添加代码:

Label backcolor = System.Drawing.Color.Transparent 
Label.BorderStyle = BorderStyle.FixedSingle
myRichTextBox.BringToFront
Label.Width=  myRichTextbox.Width + 4
Label.Height = myRichTextbox.Height + 4 
Label.left = myRichTextBox.left -2
Label.Top = myRichTextboxTop -2

希望有帮助
文森佐

[Another Hack (VB.NET)] Create a Label and Add Code:

Label backcolor = System.Drawing.Color.Transparent 
Label.BorderStyle = BorderStyle.FixedSingle
myRichTextBox.BringToFront
Label.Width=  myRichTextbox.Width + 4
Label.Height = myRichTextbox.Height + 4 
Label.left = myRichTextBox.left -2
Label.Top = myRichTextboxTop -2

Hope it helps
Vincenzo

故事和酒 2024-07-23 01:23:58

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

richTextBox.BorderStyle = BorderStyle.FixedSingle;

FixedSingle-BorderStyle 最接近例如 Button 的 FlatStyle

The easiest way to get rid of the 3D-border is to set another one:

richTextBox.BorderStyle = BorderStyle.FixedSingle;

The FixedSingle-BorderStyle is the closest to the FlatStyle of e.g. Button

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文