yog*_*002 20 .net c# drawing winforms
IDE:Visual Studio,C#.net,Type = Windows表单应用程序
嗨,在面板属性中我已将边框样式设置为"固定单一".
当我运行我的应用程序时,它给我灰色.我不知道如何更改边框颜色.我尝试过在面板的Paint事件中
private void HCp_Paint(object sender, PaintEventArgs e)
{
Panel p = sender as Panel;
ControlPaint.DrawBorder(e.Graphics, p.DisplayRectangle, Color.Yellow, ButtonBorderStyle.Inset);
}
Run Code Online (Sandbox Code Playgroud)
它给我这样的边界:
http://i772.photobucket.com/albums/yy9/yogeshkmrsoni/giving_zps877730fc.png
我想修复这样的单边框:
http://i772.photobucket.com/albums/yy9/yogeshkmrsoni/want_zps081e3591.png
我能够获得FixedSingle Border,但它是灰色,系统或IDE默认.
所以请建议我如何制作黄色.
Sin*_*atr 11
您可以Panel在客户区创建自己的类并绘制边框:
[System.ComponentModel.DesignerCategory("Code")]
public class MyPanel : Panel
{
public MyPanel()
{
SetStyle(ControlStyles.UserPaint | ControlStyles.ResizeRedraw | ControlStyles.DoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
}
protected override void OnPaint(PaintEventArgs e)
{
using (SolidBrush brush = new SolidBrush(BackColor))
e.Graphics.FillRectangle(brush, ClientRectangle);
e.Graphics.DrawRectangle(Pens.Yellow, 0, 0, ClientSize.Width - 1, ClientSize.Height - 1);
}
}
Run Code Online (Sandbox Code Playgroud)
小智 8
以防万一您不想创建自定义面板,如Sinatra回答:
private void panel1_Paint(object sender, PaintEventArgs e)
{
ControlPaint.DrawBorder(e.Graphics, this.panel1.ClientRectangle, Color.DarkBlue, ButtonBorderStyle.Solid);
}
Run Code Online (Sandbox Code Playgroud)
我觉得这篇文章有用:
https://vicky4147.wordpress.com/2007/03/04/how-to-draw-a-custom-border-around-a-form-or-control/
我还将面板的填充设置为边框的粗细,以便面板内的控件不会与边框重叠并隐藏它.在我的情况下,我没有使用填充,否则这是一个很好的解决方案,但如果你还计划使用填充不仅仅是显示边框,事情可能会变得更棘手......