做一个有类似画图板的界面(部分功能)
操作鼠标,在picturebox的图片上,画线,且能够保存。当前已经可以画线,但是出了一点问题。
按下鼠标左键,拉动鼠标,没有先出现,放开鼠标左键,还是没有线,最后在picturebox上随便找个地方点击左键,刚才那个line直线就出来了,
为什么会这样啊。。
这个是picturebox的paint事件,
private void pb_Pic_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
//Graphics displayGraphics = e.Graphics;
Graphics displayGraphics = Graphics.FromImage(this.pb_Pic.Image);
//Graphics displayGraphics = this.pb_Pic.CreateGraphics();
switch (newType)
{
case ImageType.Line:
displayGraphics.DrawLine(new Pen(newColor, lineWidth), newPoint.StartP, newPoint.EndP);
break;
}
drawingList.DrawList(displayGraphics);
}
这个事鼠标的下压事件mousedown事件
private void pb_Pic_MouseDown_Modify(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button != MouseButtons.Left)
return;
newPoint = new MousePoint(e.X, e.Y);
switch (newType)
{
case ImageType.Text:
newForm.Show();
break;
}
}
这个是mousemove事件
private void pb_Pic_MouseMove_Modify(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button != MouseButtons.Left)
return;
switch (newType)
{
}
newPoint.EndX = e.X;
newPoint.EndY = e.Y;
//pb_Pic.Invalidate();
}
这个是mouseUp事件,
private void pb_Pic_MouseUp_Modify(object sender, System.Windows.Forms.MouseEventArgs e)
{
switch (newType)
{
case ImageType.Line:
//case不重要,省略了
pb_Pic.Invalidate();
pb_Pic_MouseDown_Modify(pb_Pic,e);
}
}
—- 2分
没出现的原因肯定是没有重绘啦。
不然为什么点一下就出现呢?
点一下出现就是重绘了嘛。
—-
引用 1 楼 kwfly 的回复:
没出现的原因肯定是没有重绘啦。
不然为什么点一下就出现呢?
点一下出现就是重绘了嘛。
我去。。。“点击拿一下操作,就不要了吧”,那该怎么做呢?我加了pb_Pic.Invalidate(); 这句话没有用啊,还是得点
—-
鼠标抬起时,重绘一下估计就可以了!pb_Pic.Refresh();
—- 18分
你没有调用pb_Pic.Invalidated();这个是绘制完后刷新的操作,你不执行,让然不会显示了
—-
我真的加了
mouseup里边加了,但没有用啊,还是那样。如果不加,直接不划线了
mousemove里边不加的话,还是原来那样,没啥影响
mousemove里边如果加上这句,鼠标拖动时候,可以看到线,但是每次移动鼠标,这就会像刷子一样,把
刷过的地方全部画上。也就是说,画出来的,是个扇面。
mousedown里边,加不加都无所谓了,都那样
我该怎么办啊
这是那个扇面

—-
引用 4 楼 Trent1985 的回复:
你没有调用pb_Pic.Invalidated();这个是绘制完后刷新的操作,你不执行,让然不会显示了
大虾帮忙看那个我自己恢复的,忘记点击引用了!!
—-
你要设定鼠标按下并且Move的时候不画,鼠标Up后在绘制就不会出现扇形了,扇形是因为你的pic_paint一直在画!
—-
引用 7 楼 Trent1985 的回复:
你要设定鼠标按下并且Move的时候不画,鼠标Up后在绘制就不会出现扇形了,扇形是因为你的pic_paint一直在画!
好的 我改改!!!
—-
引用 7 楼 Trent1985 的回复:
你要设定鼠标按下并且Move的时候不画,鼠标Up后在绘制就不会出现扇形了,扇形是因为你的pic_paint一直在画!
我加了bool判定,
down或者move的时候是false,up的时候是ture。
paint也加了判定,false就不画,true就开始画,
可是还不行。。又变成了,拉完鼠标,必须点击一下picture才能够画出来的现象。
我是无奈死了。
—-
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
pictureBox1.Image = (Image)(new Bitmap(“1.jpg”));
}
private Point src;
private Point dst;
private Boolean s = false;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
src = new Point(e.X, e.Y);
s = false;
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
dst = new Point(e.X, e.Y);
s = true;
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (s)
{
Graphics g = Graphics.FromImage(this.pictureBox1.Image);
g.DrawLine(new Pen(Color.Red, 2), src, dst);
s = !s;
}
pictureBox1.Invalidate();
}
}
}
这么简单一个程序,不知道你怎么搞的!这个贴上去,刚写的,试试
—-
参考
http://blog.csdn.net/zjq/article/details/
—-
引用 10 楼 Trent1985 的回复:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
pictureBox1.Image = (Image)(new Bitmap(“1.jpg”));
}
private Point src;
private Point dst;
private Boolean s = false;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
src = new Point(e.X, e.Y);
s = false;
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
dst = new Point(e.X, e.Y);
s = true;
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (s)
{
Graphics g = Graphics.FromImage(this.pictureBox1.Image);
g.DrawLine(new Pen(Color.Red, 2), src, dst);
s = !s;
}
pictureBox1.Invalidate();
}
}
}
这么简单一个程序,不知道你怎么搞的!这个贴上去,刚写的,试试
谢谢你 思路清晰了
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/219411.html原文链接:https://javaforall.net
