Button上画线。 小问题

美丽大道 2009-05-31 04:22:19
我要在Button上画一个线,为什么鼠标在单击后能画上线,但是一旦鼠标离开了Button,画得线就自己消失了。。。貌似是有重绘?

本人对GDI不太了解。还请高手指点。。。。

代码如下:

private void button1_Click(object sender, EventArgs e)
{

Graphics g = button1.CreateGraphics();
Pen pen = new Pen(Color.Blue);
g.DrawRectangle(pen, 3, 3, button1.Width - 8, button1.Height - 8);

}
...全文
218 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
美丽大道 2009-06-01
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 wuyi8808 的回复:]
C# codeusing System;
using System.Drawing;
using System.Windows.Forms;

class Form1 : Form
{
Form1()
{
for (int i = 0; i < 5; i++)
{
MyButton button = new MyButton();
button.Parent = this;
button.Text = "按钮&" + (char)('A' + i);
button.Top = 50;
button.Left = i * 60;
button.Width = 48;
}
}

static vo…
[/Quote]


非常好的,非常感谢!
美丽大道 2009-06-01
  • 打赏
  • 举报
回复
选中button的时候会有一个选中的黑线 环绕在Button周围,请教高手怎么去掉呢。。。
hahashijie1 2009-05-31
  • 打赏
  • 举报
回复
学习中!!
wuyi8808 2009-05-31
  • 打赏
  • 举报
回复
using System;
using System.Drawing;
using System.Windows.Forms;

class Form1 : Form
{
Form1()
{
for (int i = 0; i < 5; i++)
{
MyButton button = new MyButton();
button.Parent = this;
button.Text = "按钮&" + (char)('A' + i);
button.Top = 50;
button.Left = i * 60;
button.Width = 48;
}
}

static void Main()
{
Application.Run(new Form1());
}
}

class MyButton : RadioButton
{
public MyButton()
{
Appearance = Appearance.Button;
}

protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (Checked)
{
e.Graphics.DrawRectangle(new Pen(Color.Blue), 3, 3, Width - 8, Height - 8);
}
}
}
美丽大道 2009-05-31
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 ICanUseThisID 的回复:]
引用 7 楼 UnStopable 的回复:
这个效果怎么做呢??


假定有5个按钮。。。


单击任何一个就让它画线,其他的如果之前有画线的都清除。




每个button对应一个bool变量就可以了
[/Quote]


那要是20个按钮了 ,动态个数的按钮呢。。。怎么处理啊。。不太好吧~
美丽大道 2009-05-31
  • 打赏
  • 举报
回复
我正在自己做呢 单击任何一个就让被单击的画线哦。。

然后剩下的4个清空哦。。。


呵呵~ 我正在做呢。。 可以你也可以帮我下。。
wuyi8808 2009-05-31
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 UnStopable 的回复:]


假定有5个按钮。。。


单击任何一个就让它画线,其他的如果之前有画线的都清除。 [/Quote]

不明白。

单击任何一个就让它画线,是让5个按钮都画线,还是只画被单击的按钮?

其他的如果之前有画线的都清除,是单击任何一个就清除所有的画线,还是其他说法。



参照上面的程序,你就可以自己做出需要的效果。

只要懂得原理,就不必事事依赖他人。
ICanUseThisID 2009-05-31
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 UnStopable 的回复:]
这个效果怎么做呢??


假定有5个按钮。。。


单击任何一个就让它画线,其他的如果之前有画线的都清除。



[/Quote]

每个button对应一个bool变量就可以了
wuyi8808 2009-05-31
  • 打赏
  • 举报
回复
using System;
using System.Drawing;
using System.Windows.Forms;

class Form1 : Form
{
bool DrawLine = false;

Form1()
{
Button button1 = new Button();
button1.Parent = this;
button1.Text = "画线(&D)";
button1.Click += delegate { DrawLine ^= true; };
button1.Paint += delegate(object o, PaintEventArgs e)
{
if (DrawLine)
{
e.Graphics.DrawRectangle(new Pen(Color.Blue), 3, 3, button1.Width - 8, button1.Height - 8);
}
};
}

static void Main()
{
Application.Run(new Form1());
}
}
美丽大道 2009-05-31
  • 打赏
  • 举报
回复
这个效果怎么做呢??


假定有5个按钮。。。


单击任何一个就让它画线,其他的如果之前有画线的都清除。



wuyi8808 2009-05-31
  • 打赏
  • 举报
回复
using System;
using System.Drawing;
using System.Windows.Forms;

class Form1 : Form
{
bool DrawLine = false;
Button button1;

Form1()
{
button1 = new Button();
button1.Parent = this;
button1.Text = "画线(&D)";
button1.Click += new EventHandler(button1_Click);
button1.Paint += new PaintEventHandler(button1_Paint);
}

private void button1_Click(object sender, EventArgs e)
{
DrawLine ^= true;
}

private void button1_Paint(object sender, PaintEventArgs e)
{
if (DrawLine)
{
e.Graphics.DrawRectangle(new Pen(Color.Blue), 3, 3, button1.Width - 8, button1.Height - 8);
}
}

static void Main()
{
Application.Run(new Form1());
}
}
美丽大道 2009-05-31
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 wuyi8808 的回复:]
C# codeprivate bool DrawLine = false;

private void button1_Click(object sender, EventArgs e)
{
DrawLine ^= true;
}

在每次窗体Paint重显的时候:
if (DrawLine)
{
Graphics g = button1.CreateGraphics();
Pen pen = new Pen(Color.Blue);
g.DrawRectangle(pen, 3, 3, button1.Width - 8, button1.Height - 8);
}
[/Quote]

必须用这种方法么。。。 放在单击事件中不行么。。。


那没办法了。。
wuyi8808 2009-05-31
  • 打赏
  • 举报
回复
private bool DrawLine = false;

private void button1_Click(object sender, EventArgs e)
{
DrawLine ^= true;
}

在每次窗体Paint重显的时候:
if (DrawLine)
{
Graphics g = button1.CreateGraphics();
Pen pen = new Pen(Color.Blue);
g.DrawRectangle(pen, 3, 3, button1.Width - 8, button1.Height - 8);
}
ICanUseThisID 2009-05-31
  • 打赏
  • 举报
回复
可以将划线操作放在button的Paint事件中进行
美丽大道 2009-05-31
  • 打赏
  • 举报
回复
忘记说了 我想实现的效果是按钮被单击才画线,然后再单击画线去掉。。。。


放在Paint合适么? 不是一直都有画线了。。
我不懂电脑 2009-05-31
  • 打赏
  • 举报
回复
在每次窗体Paint重显的时候
Graphics g = button1.CreateGraphics();
Pen pen = new Pen(Color.Blue);
g.DrawRectangle(pen, 3, 3, button1.Width - 8, button1.Height - 8);

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧