datagridview能设置圆角吗?
发布网友
发布时间:2022-05-24 21:48
我来回答
共2个回答
热心网友
时间:2023-11-04 19:21
在C#winform布局的时候,我们拖一个datagridview到窗体上面,
将datagridview调整为适合窗体的大小,但是我们运行之后,点击最大化按钮的时候,却发现datagridview的大小没有随着窗体的大小而变化,
影响窗体的整个布局效果,这时候可以设置一下datagridview的相应属性就可以实现,将Anchor的属性设置为TOP,BOTTOM,LEFT,RIGHT,将DOCK属性设置为BOTTOM即可实现datagridview随窗体的大小而改变
热心网友
时间:2023-11-04 19:22
private int _Radius = 20; // 圆角弧度
/// <summary>圆角弧度(0为不要圆角)</summary>
[Browsable(true)]
[Description("圆角弧度(0为不要圆角)")]
public int Radius
{
get
{
return _Radius;
}
set
{
if (value < 0) { _Radius = 0; }
else { _Radius = value; }
base.Refresh();
}
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs pe)
{
base.OnPaint(pe);
Round(this.Region); // 圆角
Color top = Color.FromArgb(240, 244, 251);
ControlPaint.DrawBorder(pe.Graphics,
this.ClientRectangle,
top,
1,
ButtonBorderStyle.Solid,
top,
1,
ButtonBorderStyle.Solid,
top,
1,
ButtonBorderStyle.Solid,
top,
1,
ButtonBorderStyle.Solid
);
//Graphics e = pe.Graphics;
//e.DrawRectangle(new Pen(Color.Red), new Rectangle(dataGridView1.Location.X, dataGridView1.Location.Y, dataGridView1.Width - 1, dataGridView1.Height - 1));
}
public void Round(System.Drawing.Region region)
{
System.Drawing.Drawing2D.GraphicsPath oPath = new System.Drawing.Drawing2D.GraphicsPath();
int x = 0;
int y = 0;
int thisWidth = this.Width;
int thisHeight = this.Height;
int angle = _Radius;
if (angle > 0)
{
System.Drawing.Graphics g = CreateGraphics();
oPath.AddArc(x, y, angle, angle, 180, 90); // 左上角
oPath.AddArc(thisWidth - angle, y, angle, angle, 270, 90); // 右上角
oPath.AddArc(thisWidth - angle, thisHeight - angle, angle, angle, 0, 90); // 右下角
oPath.AddArc(x, thisHeight - angle, angle, angle, 90, 90); // 左下角
oPath.CloseAllFigures();
Region = new System.Drawing.Region(oPath);
}
// -----------------------------------------------------------------------------------------------
else
{
oPath.AddLine(x + angle, y, thisWidth - angle, y); // 顶端
oPath.AddLine(thisWidth, y + angle, thisWidth, thisHeight - angle); // 右边
oPath.AddLine(thisWidth - angle, thisHeight, x + angle, thisHeight); // 底边
oPath.AddLine(x, y + angle, x, thisHeight - angle); // 左边
oPath.CloseAllFigures();
Region = new System.Drawing.Region(oPath);
}
}