C# Winforms 面板与 TabControl 具有相同的样式(阴影)?

发布于 2024-08-17 06:16:14 字数 52 浏览 4 评论 0原文

我希望面板有某种像 TabControl 那样的阴影,这可能吗?

I want a panel to have some sort of dropshadow like the TabControl has, is this possible?

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

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

发布评论

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

评论(2

子栖 2024-08-24 06:16:14

BorderStyle = Fix3D

看看本例中的阴影:

http://www.onteorasoftware.com /downloads/panelwithshadow.zip

最后,自定义面板可能类似于(在 VB 中):

Imports System.Drawing.Drawing2D

Public Class ShadowPanel
  Inherits Panel

  Declare Function GetWindowDC Lib "user32" Alias "GetWindowDC" (ByVal hwnd As IntPtr) _
              As IntPtr
  Declare Function ReleaseDC Lib "user32" Alias "ReleaseDC" (ByVal hwnd As IntPtr, _
      ByVal hdc As IntPtr) As Integer

  Public Sub New()
    Me.BorderStyle = BorderStyle.Fixed3D
  End Sub

  Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
    Const WM_NCPAINT As Integer = &H85

    If m.Msg = WM_NCPAINT Then
      Dim hdc As IntPtr = GetWindowDC(m.HWnd)
      Dim g As Graphics = Graphics.FromHdc(hdc)
      Dim rDraw As Rectangle = New Rectangle(0, 0, Me.Width - 1, Me.Height - 1)

      Dim pBottom As Pen = New Pen(Color.Gray, 3)
      Dim pTop As Pen = New Pen(Color.White, 3)

      g.DrawRectangle(pBottom, rDraw)

      Dim pts(2) As Point

      pts(0) = New Point(0, Me.Height - 1)
      pts(1) = New Point(0, 0)
      pts(2) = New Point(Me.Width - 1, 0)

      g.DrawLines(pTop, pts)
      ReleaseDC(Me.Handle, hdc)
    Else
      MyBase.WndProc(m)
    End If
  End Sub

  Private Sub ParentPaint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
    Dim g As Graphics = Me.Parent.CreateGraphics

    'this matrix zooms the text out to 1/4 size and offsets it by a little right and down
    Dim mx As New Matrix(1.0F, 0, 0, 1.0F, 4, 4)
    Dim rdraw As New Rectangle(Me.Left, Me.Top, Me.Width, Me.Height)

    g.Transform = mx

    g.FillRectangle(New SolidBrush(Color.FromArgb(128, Color.Black)), rdraw)
    g.Dispose()
  End Sub
End Class

BorderStyle = Fixed3D

take a look for the shadow in this example:

http://www.onteorasoftware.com/downloads/panelwithshadow.zip

Finally, the custom panel could be like (in VB):

Imports System.Drawing.Drawing2D

Public Class ShadowPanel
  Inherits Panel

  Declare Function GetWindowDC Lib "user32" Alias "GetWindowDC" (ByVal hwnd As IntPtr) _
              As IntPtr
  Declare Function ReleaseDC Lib "user32" Alias "ReleaseDC" (ByVal hwnd As IntPtr, _
      ByVal hdc As IntPtr) As Integer

  Public Sub New()
    Me.BorderStyle = BorderStyle.Fixed3D
  End Sub

  Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
    Const WM_NCPAINT As Integer = &H85

    If m.Msg = WM_NCPAINT Then
      Dim hdc As IntPtr = GetWindowDC(m.HWnd)
      Dim g As Graphics = Graphics.FromHdc(hdc)
      Dim rDraw As Rectangle = New Rectangle(0, 0, Me.Width - 1, Me.Height - 1)

      Dim pBottom As Pen = New Pen(Color.Gray, 3)
      Dim pTop As Pen = New Pen(Color.White, 3)

      g.DrawRectangle(pBottom, rDraw)

      Dim pts(2) As Point

      pts(0) = New Point(0, Me.Height - 1)
      pts(1) = New Point(0, 0)
      pts(2) = New Point(Me.Width - 1, 0)

      g.DrawLines(pTop, pts)
      ReleaseDC(Me.Handle, hdc)
    Else
      MyBase.WndProc(m)
    End If
  End Sub

  Private Sub ParentPaint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
    Dim g As Graphics = Me.Parent.CreateGraphics

    'this matrix zooms the text out to 1/4 size and offsets it by a little right and down
    Dim mx As New Matrix(1.0F, 0, 0, 1.0F, 4, 4)
    Dim rdraw As New Rectangle(Me.Left, Me.Top, Me.Width, Me.Height)

    g.Transform = mx

    g.FillRectangle(New SolidBrush(Color.FromArgb(128, Color.Black)), rdraw)
    g.Dispose()
  End Sub
End Class
空心空情空意 2024-08-24 06:16:14

是的,在面板的绘制事件中,使用如下内容:

void Paint(object sender, PaintEventArgs e) {
    TabRenderer.DrawTabPage(e.Graphics, e.Bounds);
}

yes, in the paint event of your panel, use something like this:

void Paint(object sender, PaintEventArgs e) {
    TabRenderer.DrawTabPage(e.Graphics, e.Bounds);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文