windows 使用 VisualStyles 的控件边框颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7132329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Border Color of Controls Using VisualStyles
提问by LarsTech
Microsoft's visual styles for winforms have always befuddled me.
微软的 winform 视觉风格一直让我感到困惑。
I am trying to have a Panel
sit next to a TreeView
and just have the same VisualStyle border.
我正在尝试Panel
坐在a旁边,TreeView
并且只有相同的 VisualStyle 边框。
As you can see, the TreeView
border is different than my drawing attempts in my Panel
. The panel's BorderStyle is set to None.
如您所见,TreeView
边框与我在Panel
. 面板的 BorderStyle 设置为 None。
I've tried this:
我试过这个:
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);
and I've tried this:
我试过这个:
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);
Any suggestions for the correct visual border color or visual element to use?
对使用正确的视觉边框颜色或视觉元素有什么建议吗?
采纳答案by Cody Gray
This issue isn't limited to WinForms... Since the WinForms TreeView
control is simply a wrapper around the native Win32 TreeView control, it's drawing the same border style as a TreeView control would anywhere else in the system, such as Windows Explorer. And as you've observed, the 3D border style looks different with visual styles enabled than it did on previous versions of Windows. It actually doesn't look 3D at all—the effect is closer if you set the border to Single
/FixedSingle
, except that it's a little too dark compared to the one around the TreeView.
这个问题不仅限于 WinForms... 由于 WinFormsTreeView
控件只是本机 Win32 TreeView 控件的包装器,它绘制的边框样式与 TreeView 控件在系统中的任何其他位置(例如 Windows 资源管理器)相同。正如您所观察到的,启用视觉样式后的 3D 边框样式看起来与在以前版本的 Windows 上不同。它实际上看起来根本不是 3D — 如果将边框设置为Single
/效果会更接近FixedSingle
,只是与 TreeView 周围的边框相比它有点太暗了。
As far as how to replicate that for a Panel
control, I think the trick lies not in drawing an edge, but rather in drawing a background.
至于如何为Panel
控件复制它,我认为诀窍不在于绘制边缘,而在于绘制背景。
There might be a more elegant solution if you P/Invoke the DrawThemeBackground
functiondirectly along with some of the Parts and Statesthat aren't exposed in the .NET VisualStyleRenderer
wrapper, but this one looks pretty good to me:
如果您直接P/InvokeDrawThemeBackground
函数以及一些未在 .NET包装器中公开的部件和状态,可能会有更优雅的解决方案VisualStyleRenderer
,但这个对我来说看起来很不错:
VisualStyleRenderer renderer =
new VisualStyleRenderer(VisualStyleElement.Tab.Pane.Normal);
renderer.DrawBackground(e.Graphics, panel1.ClientRectangle);
(The TreeView is on the left; the Panel is on the right.)
(TreeView 在左边;Panel 在右边。)
If you want to draw the border yourself and match the colors used when visual styles are enabled, you can do that, too. This would simply be a matter of determining the correct color, and then using the standard GDI+ drawing routines to draw a line or two around the control.
如果您想自己绘制边框并匹配启用视觉样式时使用的颜色,您也可以这样做。这只是确定正确颜色的问题,然后使用标准 GDI+ 绘图程序在控件周围绘制一两条线。
But don't fire up Photoshop just yet! The colors are all documented in a file named AeroStyle.xml
, located in the include
folder of the Windows SDK. You're interested in the globals
values; these:
但是暂时不要启动 Photoshop!颜色都记录在名为 的文件中,该文件AeroStyle.xml
位于include
Windows SDK的文件夹中。你对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>
回答by Baloo
for all of you interested, hereI found the solution, how you can let draw Windows the correct background for your control (use RECT definition from pinvoke.net):
对于所有感兴趣的人,在这里我找到了解决方案,如何让 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);
}
}