0

WinForms でこのようなボタンを作成するにはどうすればよいですか?

ここに画像の説明を入力

いつもこんな感じ

ここに画像の説明を入力

4

4 に答える 4

4

これは完全な解決策ではありませんが、実際の動作を確認し、さらに独自のボタンを作成するのに役立ちます。

public class RoundButton : Button
{
    GraphicsPath borderPath;
    protected override void OnSizeChanged(EventArgs e)
    {
        base.OnSizeChanged(e);
        UpdateRegion();
    }
    private void UpdateRegion()
    {
        borderPath = new GraphicsPath();
        borderPath.AddArc(new Rectangle(0, 0, Height, Height), 90, 180);
        borderPath.AddLine(new Point(Height / 2, 0), new Point(Width - Height / 2, 0));
        borderPath.AddArc(new Rectangle(Width - Height, 0, Height, Height), -90, 180);
        borderPath.AddLine(new Point(Width - Height / 2, Height), new Point(Height / 2, Height));
        Region = new Region(borderPath);
    }
    protected override void OnPaint(PaintEventArgs pevent)
    {
        pevent.Graphics.Clear(Color.Black);            
        Color cl1 = isMouseOver ? Color.FromArgb(100, Color.Yellow) : Color.FromArgb(100, Color.Aqua);
        Color cl2 = isMouseOver ? Color.Yellow : Color.Aqua;
        if (MouseButtons == MouseButtons.Left) cl2 = cl1;
        using (LinearGradientBrush brush = new LinearGradientBrush(ClientRectangle, cl1, cl2, 90))
        {
            pevent.Graphics.FillPath(brush, borderPath);
            pevent.Graphics.ScaleTransform(0.8f, 0.4f);                
            pevent.Graphics.TranslateTransform(0.1f * ClientSize.Width, 0.1f * ClientSize.Height, MatrixOrder.Append);
        }
        if(!(MouseButtons == MouseButtons.Left)) 
            pevent.Graphics.FillPath(new SolidBrush(Color.FromArgb(100, Color.White)), borderPath);
        pevent.Graphics.ResetTransform();

        pevent.Graphics.SmoothingMode = SmoothingMode.HighQuality;
        float penSize = MouseButtons == MouseButtons.Left ? 4 : 2.5f;
        using (Pen pen = new Pen(Color.Gray) { Width = penSize })
        {
            pevent.Graphics.DrawPath(pen, borderPath);
        }
        using (StringFormat sf = new StringFormat { LineAlignment = StringAlignment.Center, Alignment = StringAlignment.Center })
        {
            Rectangle rect = ClientRectangle;
            if (MouseButtons == MouseButtons.Left) rect.Offset(-1, -1);
            pevent.Graphics.DrawString(Text, Font, new SolidBrush(ForeColor), rect, sf);
        }
    }
    bool isMouseOver;        
    protected override void OnMouseEnter(EventArgs e)
    {
         base.OnMouseEnter(e);
         isMouseOver = true;             
    }
    protected override void OnMouseLeave(EventArgs e)
    {
        base.OnMouseLeave(e);
        isMouseOver = false;            
    }        
}

その外観は次のとおりです。

ここに画像の説明を入力

の と を入れ替えるcl1cl2new LinearGradientBrush(...)ボタンの外観が次のように変わります。

ここに画像の説明を入力

于 2013-07-13T16:39:57.637 に答える
1

回避策として(境界線を削除するため) 、 に設定Button.FlatStyleしてFlatに設定Button.FlatAppearance.BorderSizeできます0FlatAppearance.MouseDownBackColor/を変更することもできますMouseOverBackColor

于 2013-07-13T13:51:51.350 に答える
-1

画像ボタンにクリックイベントを追加できます

于 2013-07-13T14:28:17.727 に答える