【问题标题】:Border Color of Controls Using VisualStyles使用 VisualStyles 的控件的边框颜色
【发布时间】:2011-10-31 05:34:46
【问题描述】:

Microsoft 的 winforms 视觉风格一直让我感到困惑。

我试图让Panel 坐在TreeView 旁边,并且具有相同的 VisualStyle 边框。

如您所见,TreeView 边框与我在Panel 中的绘图尝试不同。面板的 BorderStyle 设置为 None。

我试过了:

  Rectangle r = new Rectangle(0, 0, panel1.ClientRectangle.Width - 1, panel1.ClientRectangle.Height - 1);
  using (Pen p = new Pen(VisualStyleInformation.TextControlBorder))
    e.Graphics.DrawRectangle(p, r);

我已经试过了:

VisualStyleRenderer renderer = new VisualStyleRenderer(VisualStyleElement.TextBox.TextEdit.Normal);
renderer.DrawEdge(e.Graphics, panel1.ClientRectangle, 
         Edges.Bottom | Edges.Left | Edges.Right | Edges.Top,
         EdgeStyle.Sunken, EdgeEffects.Flat);

对于要使用的正确视觉边框颜色或视觉元素有什么建议吗?

【问题讨论】:

    标签: c# .net windows winforms visual-styles


    【解决方案1】:

    此问题不仅限于 WinForms... 由于 WinForms TreeView 控件只是原生 Win32 TreeView 控件的包装,它绘制的边框样式与系统中其他任何地方的 TreeView 控件相同,例如作为 Windows 资源管理器。正如您所观察到的,启用视觉样式后的 3D 边框样式看起来与以前版本的 Windows 不同。它实际上看起来根本不是 3D 的——如果将边框设置为 Single/FixedSingle,效果会更接近,但与 TreeView 周围的边框相比,它有点太暗了。

    至于如何为Panel 控件复制它,我认为诀窍不在于绘制边缘,而在于绘制背景

    如果您直接 P/Invoke DrawThemeBackground function 以及未在 .NET VisualStyleRenderer 包装器中公开的一些 Parts and States,则可能会有更优雅的解决方案,但这个对我来说看起来不错:

    VisualStyleRenderer renderer =
                  new VisualStyleRenderer(VisualStyleElement.Tab.Pane.Normal);
    renderer.DrawBackground(e.Graphics, panel1.ClientRectangle);
    

       

       (树视图在左侧;面板在右侧。)


    如果您想自己绘制边框并匹配启用视觉样式时使用的颜色,您也可以这样做。这只是确定正确颜色的问题,然后使用标准 GDI+ 绘图例程在控件周围绘制一两条线。

    但暂时不要启动 Photoshop!这些颜色都记录在名为AeroStyle.xml 的文件中,该文件位于Windows SDK 的include 文件夹中。您对globals 值感兴趣;这些:

    <globals>
        <EdgeDkShadowColor> 100 100 100</EdgeDkShadowColor>
        <EdgeFillColor>     220 220 220</EdgeFillColor>
        <EdgeHighLightColor>244 247 252</EdgeHighLightColor>
        <EdgeLightColor>    180 180 180</EdgeLightColor>
        <EdgeShadowColor>   180 180 180</EdgeShadowColor>
        <GlowColor>         255 255 255</GlowColor>
    </globals>
    

    【讨论】:

    • 我现在正在玩那个 DrawThemeBackground。 Paint.NET 告诉我 TreeView 边框颜色是 (130, 135, 144)。您的 Tab.Pane.Normal 建议非常接近,但它是 (137, 140, 149)。
    【解决方案2】:

    对于所有感兴趣的人,here 我找到了解决方案,如何让 Windows 为您的控件绘制正确的背景(使用 pinvoke.net 中的 RECT 定义):

    const string CLASS_LISTVIEW = "LISTVIEW";
    const int LVP_LISTGROUP = 2;
    
    [DllImport("uxtheme.dll", ExactSpelling = true, CharSet = CharSet.Unicode, SetLastError = true)]
    private extern static int DrawThemeBackground(IntPtr hTheme, IntPtr hdc, int iPartId, int iStateId, ref RECT pRect, IntPtr pClipRect);
    
    public static void DrawWindowBackground(IntPtr hWnd, Graphics g, Rectangle bounds)
    {
        IntPtr theme = OpenThemeData(hWnd, CLASS_LISTVIEW);
        if (theme != IntPtr.Zero)
        {
          IntPtr hdc = g.GetHdc();
          RECT area = new RECT(bounds);
          DrawThemeBackground(theme, hdc, LVP_LISTGROUP, 0, ref area, IntPtr.Zero);
          g.ReleaseHdc();
          CloseThemeData(theme);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-23
      • 2010-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-18
      相关资源
      最近更新 更多