如何在 WinForms 应用程序中创建一行? [英] How can I create a line in a WinForms Application?

查看:25
本文介绍了如何在 WinForms 应用程序中创建一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 WinForms 应用程序中创建一个简单的 3D 线,以改进我的表单布局的视觉排列.此行与关于 Windows 对话框中的行完全相同(可以在 Windows 资源管理器 -> 帮助 -> 关于 Windows 中打开).

I want to create a simple 3D line in a WinForms application to improve visual arrangement of my form layout. This line is exacly like the line in About Windows dialog (can be opened in Windows Explorer -> Help -> About Windows).

此处检查示例.最后一行(3D)是我想要的,不是第一行.

An example be checked here. The last line (3D) is the one I want, not the first one.

如何在 C# 或 Visual Basic (.NET) 中完成此操作?

How can this be done in C# or Visual Basic (.NET)?

推荐答案

如果您使用 SysInternals 的 ZoomIt 实用程序,您可以看到这只是两行代码.深灰色在白色之上.使用 Graphics.DrawLine() 绘制线条非常简单,您只需要确保选择与表单的 BackColor 配合良好的深色即可.如果用户选择了另一个主题,那并不总是战舰灰色.这使得 GroupBox 技巧失败.

If you use SysInternals' ZoomIt utility, you can see that this is simply two lines. A dark gray one above a white one. Drawing lines is simple enough with Graphics.DrawLine(), you just need to make sure you pick a dark color that work well with the form's BackColor. That isn't always battleship gray if the user selected another theme. Which makes the GroupBox trick fall flat.

此示例代码可用:

    protected override void OnPaint(PaintEventArgs e) {
        Color back = this.BackColor;
        Color dark = Color.FromArgb(back.R >> 1, back.G >> 1, back.B >> 1);
        int y = button1.Bottom + 20;
        using (var pen = new Pen(dark)) {
            e.Graphics.DrawLine(pen, 30, y, this.ClientSize.Width - 30, y);
        }
        e.Graphics.DrawLine(Pens.White, 30, y+1, this.ClientSize.Width - 30, y+1);
    }

请注意在此代码中使用 button1,以确保在正确的高度绘制线条,即使在重新缩放表单时也是如此.选择您自己的控件作为该行的参考.

Note the use of button1 in this code, there to make sure the line is drawn at the right height, even when the form is rescaled. Pick your own control as a reference for the line.

这篇关于如何在 WinForms 应用程序中创建一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆