【问题标题】:Rounded Corners in C# windows forms [duplicate]C# windows窗体中的圆角[重复]
【发布时间】:2013-09-16 06:59:44
【问题描述】:

我有一个没有边框的窗口。我在网上搜索了圆角,但都带有边框。如何制作表格(not with borders) 的圆角?有没有办法做到这一点?

我是c#的新手,请解释一下...

谢谢

【问题讨论】:

    标签: c# windows winforms


    【解决方案1】:

    试试这个:

    using System;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            [DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
            private static extern IntPtr CreateRoundRectRgn
            (
                int nLeftRect,     // x-coordinate of upper-left corner
                int nTopRect,      // y-coordinate of upper-left corner
                int nRightRect,    // x-coordinate of lower-right corner
                int nBottomRect,   // y-coordinate of lower-right corner
                int nWidthEllipse, // width of ellipse
                int nHeightEllipse // height of ellipse
            );
    
            public Form1()
            {
                InitializeComponent();
                this.FormBorderStyle = FormBorderStyle.None;
                Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 20, 20));
            }
        }
    }
    

    从这里:Form with Rounded Borders in C#?

    【讨论】:

    • 尝试在面板上使用它。它只使边缘之一变圆(左上角)!
    • @ElMac:我不会再试一次了,它对我很有用。你能验证你复制粘贴代码是否正确吗?
    • 我发现了问题。如果我让面板太小,一些边缘会被切断。我还不知道原因,但我不能使用这种方法,因为我使用的面板应该几乎是按钮大小。
    • 您可能还需要添加“this.AllowTransparency = true;”
    • 切角的原因是该区域没有随窗口调整大小,需要重新创建并在调整大小时重新签名
    【解决方案2】:

    Region 属性只是切断了角落。要获得真正的圆角,您必须绘制圆角矩形。

    Drawing rounded rectangles

    绘制所需形状的图像并将其放在透明表单上可能会更容易。更容易绘制,但无法调整大小。

    也可以查看Another One

    【讨论】:

    • “区域属性 [SIC] 只是切断了角落。”这是真的,它剪辑了表单的渲染,使表单的角具有圆形透明角。为了绘制圆角,比如具有 3D 斜角外观,需要额外的绘图工作。所以你没有错,但我认为你的措辞有点混乱。
    【解决方案3】:

    我找到了这段代码

    为了设计圆角文本框,我开始尝试使用绘制覆盖事件,但不幸的是没有任何结果,这是因为(我假设)文本框是从 Windows 派生的。因此,我尝试改写 WM_PAINT API,得到了预期的结果

    http://www.codeproject.com/Articles/17453/Textbox-with-rounded-corners

    谢谢

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-17
      • 2018-04-17
      • 2018-12-29
      • 2013-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多