我现在动态添加好几个picturebox,想实现点击picturebox为他绘制一个边框,然后再点击另一个picturebox取消前一个的边框,为第二个加一个边框!!!
目前只做到了点击加边框但不会取消前一个的边框代码如下:怎么修改啊
private void pic_Click(object sender, EventArgs e)
{
PictureBox p = sender as PictureBox;
openurl = p.Tag.ToString();
Graphics pictureborder = p.CreateGraphics();
Pen pen = new Pen(Color.Orange, 4);
pictureborder.DrawRectangle(pen, p.ClientRectangle.X, p.ClientRectangle.Y, p.ClientRectangle.X + p.ClientRectangle.Width, p.ClientRectangle.Y +p.ClientRectangle.Height);
}
自己顶求大神帮解答啊,怎么修改代码
在这个方法后面加上获取其他PictureBox 的代码,
然后然后遍历用底色在画一遍边框就好!
picturebox是放在usercontrol里的,都是通过点击按钮,添加的。有代码可以给我参考一下吗?新手不太会,先谢谢啦
@C#求学路:加上这个试试,手打可能有误
private void pic_Click(object sender, EventArgs e)
{
PictureBox p = sender as PictureBox;
openurl = p.Tag.ToString();
Graphics pictureborder = p.CreateGraphics();
Pen pen = new Pen(Color.Orange, 4);
pictureborder.DrawRectangle(pen, p.ClientRectangle.X, p.ClientRectangle.Y, p.ClientRectangle.X + p.ClientRectangle.Width, p.ClientRectangle.Y +p.ClientRectangle.Height);
foreach(Control c in usercontrol.Controls)
{
PictureBox p1 = c as PictureBox;
if(p != p1)
{
Graphics pictureborder1= p1.CreateGraphics();
Pen pen1 = new Pen(p1.BackColor, 4);
pictureborder1.DrawRectangle(pen1, p1.ClientRectangle.X, p1.ClientRectangle.Y, p1.ClientRectangle.X + p1.ClientRectangle.Width, p1.ClientRectangle.Y +p1.ClientRectangle.Height);
}
}
}
@刘宏玺: (Control c in usercontrol.Controls)这里报错了
对象引用对于非静态的字段、方法或属性“Control.Controls”是必需的
@C#求学路: 你的usercontrol是你的自定义控件吧?
你先告诉我你把动态的usercontrol放在那里了?
我估计是加到了窗口里面
你先告诉我你的usercontrol中的PictureBox叫啥名,现在知道的太少了,或者你自己研究下,思路就是上面那个代码的思路
@刘宏玺: 自定义控件的名字是Usercontrol picturebox是我通过代码添加到自定义控件中的
创建picturebox的代码在这
PictureBox p = new PictureBox();
p.Click += new EventHandler(pic_Click);
p.Name = "pic1";
p.BorderStyle = BorderStyle.FixedSingle;
p.SizeMode = PictureBoxSizeMode.CenterImage;
p.Size = new Size(145, 145);
if (!d1.Url.Equals("111111111111"))
{
p.Image = image;
}
uc1.Controls.Add(p);
@C#求学路:
foreach(Control c in uc1.Controls)
{
PictureBox p1 = c as PictureBox;
if(p != p1)
{
Graphics pictureborder1= p1.CreateGraphics();
Pen pen1 = new Pen(p1.BackColor, 4);
pictureborder1.DrawRectangle(pen1, p1.ClientRectangle.X, p1.ClientRectangle.Y, p1.ClientRectangle.X + p1.ClientRectangle.Width, p1.ClientRectangle.Y +p1.ClientRectangle.Height);
}
}
这样试试
@刘宏玺: 还是不对,我那个创建picturebox的代码是在一个添加按钮里写的,uc1.Contols,uc1不存在,那个uc1是在这个方法里写的,这个方法是添加自定义控件和picturebox
private void button1_Click(object sender, EventArgs e)
{
XmlUtil xm = new XmlUtil();
Dictionary<string, dpclass> d = xm.LoadItems();
foreach (var item in d.Values)
{
dpclass d1 = (dpclass)item;
// MessageBox.Show(d1.Url);
Usercontorl uc1 = new Usercontorl(d1.Name, flowLayoutPanel1);
System.Drawing.Icon dd = uc1.SetIcon(d1.Url);
Image image = dd.ToBitmap();
PictureBox p = new PictureBox();
p.Click += new EventHandler(pic_Click);
p.Name = "pic1";
p.BorderStyle = BorderStyle.FixedSingle;
p.SizeMode = PictureBoxSizeMode.CenterImage;
p.Size = new Size(145, 145);
if (!d1.Url.Equals("111111111111"))
{
p.Image = image;
}
uc1.Controls.Add(p);
// pb.Image = image;
//uc.Tag = item.Name;
this.flowLayoutPanel1.Controls.Add(uc1);
}
@C#求学路: 早放代码不就得了!这样写
foreach(Control c in this.flowLayoutPanel1.Controls)
{
Usercontorl uc1 = c as Usercontorl;
PictureBox p1 = c.Controls[0] as PictureBox;
if(p != p1)
{
Graphics pictureborder1= p1.CreateGraphics();
Pen pen1 = new Pen(p1.BackColor, 4);
pictureborder1.DrawRectangle(pen1, p1.ClientRectangle.X, p1.ClientRectangle.Y, p1.ClientRectangle.X + p1.ClientRectangle.Width, p1.ClientRectangle.Y +p1.ClientRectangle.Height);
}
}
@C#求学路: 有点bug
foreach(Control c in this.flowLayoutPanel1.Controls)
{
Usercontorl uc1 = c as Usercontorl;
PictureBox p1 = uc1.Controls[0] as PictureBox;
if(p != p1)
{
Graphics pictureborder1= p1.CreateGraphics();
Pen pen1 = new Pen(p1.BackColor, 4);
pictureborder1.DrawRectangle(pen1, p1.ClientRectangle.X, p1.ClientRectangle.Y, p1.ClientRectangle.X + p1.ClientRectangle.Width, p1.ClientRectangle.Y +p1.ClientRectangle.Height);
}
}
@刘宏玺: if(p !=p1)那个p是哪里的,我找不到了- -!
@C#求学路: p是你事件里的那个
private void pic_Click(object sender, EventArgs e)
{
PictureBox p = sender as PictureBox;
openurl = p.Tag.ToString();
Graphics pictureborder = p.CreateGraphics();
Pen pen = new Pen(Color.Orange, 4);
pictureborder.DrawRectangle(pen, p.ClientRectangle.X, p.ClientRectangle.Y, p.ClientRectangle.X + p.ClientRectangle.Width, p.ClientRectangle.Y +p.ClientRectangle.Height);
foreach(Control c in this.flowLayoutPanel1.Controls)
{
Usercontorl uc1 = c as Usercontorl;
PictureBox p1 = uc1.Controls[0] as PictureBox;
if(p != p1)
{
Graphics pictureborder1= p1.CreateGraphics();
Pen pen1 = new Pen(p1.BackColor, 4);
pictureborder1.DrawRectangle(pen1, p1.ClientRectangle.X, p1.ClientRectangle.Y, p1.ClientRectangle.X + p1.ClientRectangle.Width, p1.ClientRectangle.Y +p1.ClientRectangle.Height);
}
}
}
@刘宏玺: 我把new的picturebox覆盖了。。。找到问题了
@刘宏玺: 还是没有取消掉上一个的picturebox边框啊
@C#求学路:
private void pic_Click(object sender, EventArgs e)
{
PictureBox p = sender as PictureBox;
openurl = p.Tag.ToString();
Graphics pictureborder = p.CreateGraphics();
Pen pen = new Pen(Color.Orange, 4);
pictureborder.DrawRectangle(pen, p.ClientRectangle.X, p.ClientRectangle.Y, p.ClientRectangle.X + p.ClientRectangle.Width, p.ClientRectangle.Y +p.ClientRectangle.Height);
foreach(Control c in this.flowLayoutPanel1.Controls)
{
Usercontorl uc1 = c as Usercontorl;
PictureBox p1 = uc1.Controls[0] as PictureBox;
if(p != p1)
{
Graphics pictureborder1= p1.CreateGraphics();
Pen pen1 = new Pen(this.BackColor, 4);
pictureborder1.DrawRectangle(pen1, p1.ClientRectangle.X, p1.ClientRectangle.Y, p1.ClientRectangle.X + p1.ClientRectangle.Width, p1.ClientRectangle.Y +p1.ClientRectangle.Height);
}
}
}
换成this试试
@刘宏玺: 换成this所有的picturebox边框都变成了白色,点击哪个哪个变成orange,实现了点击切换,但是我不想让他们的边框都变成白的啊
@C#求学路: 你看看原来的背景是什么颜色,然后把this.BackColor换成那个颜色不就解决问题了!
@C#求学路: 我估计是System.Drawing.SystemColors.Control
@刘宏玺: 窗体的背景是个图片,usercontrol和picturebox的backcolor都是Transparent
@C#求学路: 那你换一下试试能实现不!我估计差不多了!
@刘宏玺: System.Drawing.SystemColors.Control换这个试了,跟this.backcolor一样的效果
@C#求学路: 你都说了usercontrol和picturebox的backcolor都是Transparent
那你就使用Transparent啊
@刘宏玺: Pen pen1 = new Pen(this.BackColor, 4);改这一句吗?我不太会啊 - -#
@C#求学路:
foreach(Control c in this.flowLayoutPanel1.Controls)
{
Usercontorl uc1 = c as Usercontorl;
PictureBox p1 = uc1.Controls[0] as PictureBox;
if(p != p1)
{
Graphics pictureborder1= p1.CreateGraphics();
Pen pen1 = new Pen(Color.Transparent, 4);
pictureborder1.DrawRectangle(pen1, p1.ClientRectangle.X, p1.ClientRectangle.Y, p1.ClientRectangle.X + p1.ClientRectangle.Width, p1.ClientRectangle.Y +p1.ClientRectangle.Height);
}
}
@刘宏玺: 不行啊,点完控件之后只加颜色不清除了
@C#求学路: 这样写试试
Pen pen1 = new Pen(this.flowLayoutPanel1.BackColor, 4);
@刘宏玺: 效果没变。。。还是只能点击添加边框,不能清除
@C#求学路: 我就纳闷了,你截个图上来我看看是啥样
@刘宏玺: 这样写试试
foreach(Control c in this.flowLayoutPanel1.Controls)
{
Usercontorl uc1 = c as Usercontorl;
PictureBox p1 = uc1.Controls[0] as PictureBox;
if(p != p1)
{
p1.Invalidate();
}
}
@刘宏玺: 天呐!这样写终于对了。只需要一句,谢谢你啦
1.把picturebox放到一个容器里面设置,然后动态设置容器的border大小就行了。
2. 把另一个的颜色重置下
pictureBox2.CreateGraphics().Clear(pictureBox2.BackColor);
因为我的picturebox是放到usercontrol里的1个usercontrol里只有1个picturebox,动态添加,写pictureBox2.CreateGraphics().Clear(pictureBox2.BackColor);会报错。
这要怎么解决啊,谢谢
现在还有人用winform啊 感觉winforem有点跟不上了啊