c# 动态生成Panel的边框颜色
用鼠标滑动生成一个Panel,怎么设置这个Panel的边框颜色啊.代码如下:privatevoidForm1_MouseDown(objectsender,MouseEv...
用鼠标滑动生成一个Panel,怎么设置这个Panel的边框颜色啊.代码如下:
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
mouseStatus = true;
startPoint.X = e.X;
startPoint.Y = e.Y;
//重新一个矩形,重置最大重绘矩形的上下左右的坐标
minStartX = e.X;
minStartY = e.Y;
maxEndX = e.X;
maxEndY = e.Y;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (mouseStatus)
{
endPoint.X = e.X; endPoint.Y = e.Y;
//这一段是获取要绘制矩形的上下左右的坐标,如果不这样处理的话,只有从左上开始往右下角才能画出矩形。
//这样处理的话,可以任意方向,当然中途可以更换方向。
int realStartX = Math.Min(startPoint.X, endPoint.X);
int realStartY = Math.Min(startPoint.Y, endPoint.Y);
int realEndX = Math.Max(startPoint.X, endPoint.X);
int realEndY = Math.Max(startPoint.Y, endPoint.Y);
minStartX = Math.Min(minStartX, realStartX);
minStartY = Math.Min(minStartY, realStartY);
maxEndX = Math.Max(maxEndX, realEndX);
maxEndY = Math.Max(maxEndY, realEndY);
currRect = new Rectangle(realStartX, realStartY, realEndX - realStartX, realEndY - realStartY);
//一下是为了获取最大重绘矩形。
Rectangle refeshRect = new Rectangle(minStartX, minStartY, maxEndX - minStartX, maxEndY - minStartY);
refeshRect.Inflate(1, 1);//重绘矩形的大小扩展1个单位
this.Invalidate(refeshRect);//失效一个区域,并使其重绘。
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
mouseStatus = false;
endPoint.X = e.X; endPoint.Y = e.Y;
int realStartX = Math.Min(startPoint.X, endPoint.X);
int realStartY = Math.Min(startPoint.Y, endPoint.Y);
int realEndX = Math.Max(startPoint.X, endPoint.X);
int realEndY = Math.Max(startPoint.Y, endPoint.Y);
currPanel = new Panel();
currPanel.Name = "panel" + (rectList.Count + 10);
this.SuspendLayout();
currPanel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
currPanel.Location = new System.Drawing.Point(currRect.X, currRect.Y);
currPanel.Size = new System.Drawing.Size(currRect.Width, currRect.Height);
currPanel.TabIndex = 0;
this.panel1.Controls.Add(currPanel);
this.panelList.Add(currPanel);
this.Invalidate();//重绘整个界面
} 展开
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
mouseStatus = true;
startPoint.X = e.X;
startPoint.Y = e.Y;
//重新一个矩形,重置最大重绘矩形的上下左右的坐标
minStartX = e.X;
minStartY = e.Y;
maxEndX = e.X;
maxEndY = e.Y;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (mouseStatus)
{
endPoint.X = e.X; endPoint.Y = e.Y;
//这一段是获取要绘制矩形的上下左右的坐标,如果不这样处理的话,只有从左上开始往右下角才能画出矩形。
//这样处理的话,可以任意方向,当然中途可以更换方向。
int realStartX = Math.Min(startPoint.X, endPoint.X);
int realStartY = Math.Min(startPoint.Y, endPoint.Y);
int realEndX = Math.Max(startPoint.X, endPoint.X);
int realEndY = Math.Max(startPoint.Y, endPoint.Y);
minStartX = Math.Min(minStartX, realStartX);
minStartY = Math.Min(minStartY, realStartY);
maxEndX = Math.Max(maxEndX, realEndX);
maxEndY = Math.Max(maxEndY, realEndY);
currRect = new Rectangle(realStartX, realStartY, realEndX - realStartX, realEndY - realStartY);
//一下是为了获取最大重绘矩形。
Rectangle refeshRect = new Rectangle(minStartX, minStartY, maxEndX - minStartX, maxEndY - minStartY);
refeshRect.Inflate(1, 1);//重绘矩形的大小扩展1个单位
this.Invalidate(refeshRect);//失效一个区域,并使其重绘。
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
mouseStatus = false;
endPoint.X = e.X; endPoint.Y = e.Y;
int realStartX = Math.Min(startPoint.X, endPoint.X);
int realStartY = Math.Min(startPoint.Y, endPoint.Y);
int realEndX = Math.Max(startPoint.X, endPoint.X);
int realEndY = Math.Max(startPoint.Y, endPoint.Y);
currPanel = new Panel();
currPanel.Name = "panel" + (rectList.Count + 10);
this.SuspendLayout();
currPanel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
currPanel.Location = new System.Drawing.Point(currRect.X, currRect.Y);
currPanel.Size = new System.Drawing.Size(currRect.Width, currRect.Height);
currPanel.TabIndex = 0;
this.panel1.Controls.Add(currPanel);
this.panelList.Add(currPanel);
this.Invalidate();//重绘整个界面
} 展开
1个回答
展开全部
框架自带的Panel是默认不设置边框样式的,只能设置边框类型。
你需要自己在Paint事件中实现边框绘制。当然,自己继承一下panel改一改也是可以的。
下面的控件继承自Panel.
public class PanelWithCustomBorder : Panel
{
public Color BorderColor;
public int BorderWidth = 1;
public ButtonBorderStyle BorderLineStyle = ButtonBorderStyle.Solid;
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (BorderColor != null)
ControlPaint.DrawBorder(e.Graphics, this.ClientRectangle,
BorderColor,BorderWidth,BorderLineStyle,
BorderColor, BorderWidth, BorderLineStyle,
BorderColor, BorderWidth, BorderLineStyle,
BorderColor, BorderWidth, BorderLineStyle);
}
}
使用BorderColor属性指定边框颜色,不指定则为默认;
使用BorderWidth指定边框宽度;
使用BorderLineStyle指定边框线条的样式。
你也可以在标准panel的Paint事件中使用ControlPaint完成一样的工作。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询