65.9K
CodeProject 正在变化。 阅读更多。
Home

圆角按钮控件 - 揭秘 DrawArc

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.54/5 (3投票s)

2018年1月20日

CPOL

1分钟阅读

viewsIcon

12177

downloadIcon

273

这是“圆角按钮控件 - 揭秘 DrawArc”的替代方案。

引言

更新了 VS2017 的 RoundedButton 控件,经过重构并符合 StyleCop 规范。

V 1.3

改进了绘图例程 DrawBorderGraphic(),现在边缘更平滑。

GraphicsBuffer 类中,将“旧式”属性替换为更现代的单行等效属性。

可以通过遵循 VS2017 编辑器显示的边距提示来轻松完成此操作。

public Bitmap BitmapX { get; set; }

public int WidthG { get; set; }

public int HeightG { get; set; }

public string NameG { get; set; }

/// <summary>
/// returns the current Graphic Graphics object
/// </summary>
public Graphics Graphic => (graphic);

/// <summary>
/// returns true if the graphics object exists; false otherwise
/// </summary>
public bool GraphicsBufferExists => (graphic != null);

背景

由于按钮与控件重叠导致测试窗体显示不正确,我决定更改代码以避免这种情况发生,奇怪的是作者似乎没有遇到这个问题。

因此,当 roundedbutton 设置为非常大的高度并且将要覆盖下方GroupBox 中的其他控件时,GroupBox button_GBborder_GB 将向下移动,并且在 RecalculateSizes() 方法中重新计算窗体大小。

/// <summary>
/// Recalculate the form and groupbox positions.
/// </summary>
private void RecalculateSizes()
{
    if (this.button_GB.Top < this.rounded_button_RB.Bottom)
    {
        this.button_GB.Top = this.rounded_button_RB.Bottom;
        this.border_GB.Top = this.button_GB.Bottom + 5;
    }
    
    if (this.Height < this.border_GB.Bottom + 100)
    {
        this.Height = this.border_GB.Bottom + 100;
    }
    
    this.Invalidate();
}

我还添加了一个按钮,允许用户使用高分辨率显示器缩放窗体。这种工作方式非常简单,与我在网上搜索时找到的大多数复杂/无法工作的建议不同。

请注意,要使此功能正常工作,窗体的 AutoScaleMode 属性必须设置为默认值 Font

private void Button_Click(object sender, EventArgs e)
{
    Button button = (Button)sender;
    string tag = button.Tag.ToString().ToUpper().Trim();
    
    switch (tag)
    {
        case "ENLARGE":
            // Scale form, AutoScaleMode.Font must be set for this to work.
            this.Font = new Font(this.Font.Name, this.Font.Size + 2);
            break;

警告:此缩放方法适用于大多数控件,但不适用于像 FlowLayoutPanel 这样复杂的控件。

我还添加了新的属性

  • RotatedText
  • RotatedTextAngle

这些允许以度为单位旋转文本,通常为垂直方向的文本的 -90 或 90 度。

Using the Code

不要忘记在加载解决方案后将 RoundedButtonDialog 设置为启动项目。

历史

  • v 1.2
    • 修复了更改宽度或高度时的错误大小,按钮太大。
    • 修复了厚度为 1 时缺少底部和右侧线条的问题。
  • V 1.3
    • 改进了绘图例程 DrawBorderGraphic()
© . All rights reserved.