winform 根据鼠标移动画线,这一个问题想了3年了还不太会

csdsuper 2010-07-07 03:17:35
怎么做在窗体或者什么控件上画线,就像写字一样,画出的线后可以保存成图片,谁会的,这一个问题想了3年了还不太会
...全文
541 21 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
足球中国 2010-07-21
  • 打赏
  • 举报
回复
效率确实是个大问题。看mfc上的做法。用记忆重画为背景效率会高很多。不会都个刷新重绘。



rizher 2010-07-21
  • 打赏
  • 举报
回复
拜楼主高人
WIKESOFT 2010-07-21
  • 打赏
  • 举报
回复
加我QQ 381914029
我有一个vs 2005的画图小程序
xjyyaqy 2010-07-07
  • 打赏
  • 举报
回复
精神可嘉,不过还是建议多查查资料
xky578353168 2010-07-07
  • 打赏
  • 举报
回复
这不就和C#画图差不多吗?感觉还要简单,为什么要想三年呢?楼主精神值得学习!!
cejay 2010-07-07
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 robin521 的回复:]

三年,一天写一行代码也给搞出来了。
[/Quote]

呵呵呵,电脑画线没写过,到时以前鼓捣windows mobile 的时候写个手写记事本,就是保存成图片
jianuMan 2010-07-07
  • 打赏
  • 举报
回复
http://blog.csdn.net/jianuMan/archive/2010/06/18/5677839.aspx
保存图片的吧 你google一下 窗体截屏函数
robin521 2010-07-07
  • 打赏
  • 举报
回复
三年,一天写一行代码也给搞出来了。
csdsuper 2010-07-07
  • 打赏
  • 举报
回复

private void DrawAllLines(Graphics g)
{
Pen p = new Pen(Color.Black);
int TotalLines = m_lines.Count;
for (int i = 0; i < TotalLines; i++)
{
g.DrawLines(p, m_lines[i].ToArray());
}
p.Dispose();
g.Dispose();
}
这里怎么会经常出现参数无效的呢,有时又不有
csdsuper 2010-07-07
  • 打赏
  • 举报
回复
这个还不错,但还有不少的bug和错误的
[Quote=引用 8 楼 roc196 的回复:]
C# code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace HandDra……
[/Quote]
csdsuper 2010-07-07
  • 打赏
  • 举报
回复
你的代码好像运行出错
[Quote=引用 8 楼 roc196 的回复:]
C# code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace HandDra……
[/Quote]
lovelan1748 2010-07-07
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 csdsuper 的回复:]
不是我要的呀,浪费我一个资源分
[/Quote]
我汗,你把我涂鸦换成绘制不就是你要的了,兄弟放弃这行吧,娃娃生出来三年也会打酱油了
88csdn 2010-07-07
  • 打赏
  • 举报
回复
晕,没刷新不知道这么多人回复了~~~
88csdn 2010-07-07
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace HandDrawing
{
public partial class HandDrawing : Form
{
public HandDrawing()
{
InitializeComponent();
m_lines = new List<List<Point>>();
}

private List<List<Point>> m_lines;
private List<Point> m_newLine;

private void Form1_MouseDown(object sender, MouseEventArgs e)
{
m_newLine = new List<Point>();
}

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
m_addPoint(e.X, e.Y);
}

private void Form1_MouseUp(object sender, MouseEventArgs e)
{
m_addPoint(e.X, e.Y);
m_lines.Add(m_newLine);
}

private void m_addPoint(int x, int y)
{
m_newLine.Add(new Point(x, y));
int points = m_newLine.Count;
if (points > 1)
{
Graphics g = this.CreateGraphics();
Pen p = new Pen(Color.Black);
g.DrawLine(p, m_newLine[points - 2].X, m_newLine[points - 2].Y, m_newLine[points - 1].X, m_newLine[points - 1].Y);
p.Dispose();
g.Dispose();
}
}

private void Form1_Paint(object sender, PaintEventArgs e)
{
DrawAllLines(e.Graphics);
}

private void DrawAllLines(Graphics g)
{
Pen p = new Pen(Color.Black);
int TotalLines = m_lines.Count;
for (int i = 0; i < TotalLines; i++)
{
g.DrawLines(p, m_lines[i].ToArray());
}
p.Dispose();
g.Dispose();
}

private void miClear_Click(object sender, EventArgs e)
{
//foreach (List<Point> l in m_lines)
// l.Clear();
m_lines.Clear();
this.Invalidate();
}

private void miSave_Click(object sender, EventArgs e)
{
if (saveFileDialog1.ShowDialog() != DialogResult.OK)
return;
Bitmap bmp = null;
Graphics g = null;
Brush b = null;
try
{
bmp = new Bitmap(this.ClientSize.Width, this.ClientSize.Height);
g = Graphics.FromImage(bmp);
b = new SolidBrush(Color.White);
g.FillRectangle(b, this.ClientRectangle);
DrawAllLines(g);
bmp.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Bmp);
}
catch (Exception ex)
{
MessageBox.Show(string.Format("Cannot save picture,error message:{0}", ex.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
}
finally
{
if (bmp != null)
bmp.Dispose();
if (g != null)
g.Dispose();
if (b != null)
b.Dispose();
}
}

private void miExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
304的的哥 2010-07-07
  • 打赏
  • 举报
回复

我这里有个在窗体上可以写字的例子,不能保存

namespace WriteWords
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

//MyPoint1, MyPoint2表示鼠标按下和弹起时鼠标的坐标位置
public Point MyPoint1, MyPoint2;
public int MyFlag = 0;

private void Form1_MouseUp(object sender, MouseEventArgs e)
{
//当鼠标弹起时,设置MyFlag = 0,表示不能画线
this.MyFlag = 0;
}

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
this.Text = "X=" + e.X.ToString() + ",Y=" + e.Y.ToString();
Graphics g = this.CreateGraphics();
Pen MyPen = new Pen(Color.Black);

//MyFlag=0表示鼠标弹起,不能进行画线
//当鼠标按下时,设置MyFlag=1表示可以画线
if (this.MyFlag == 0)
return;

//鼠标移动,每次变换时,MyPoint2都记录下鼠标的位置,以便进行鼠标移动画线
this.MyPoint2.X = e.X;
this.MyPoint2.Y = e.Y;
g.DrawLine(MyPen, MyPoint1.X, MyPoint1.Y, MyPoint2.X, MyPoint2.Y);

//当画完一条线后(很短的,可以当做一个小点看待),将MyPoint1的坐标重置为此时鼠标的位置
MyPoint1.X = e.X;
MyPoint1.Y = e.Y;
}

private void Form1_MouseDown(object sender, MouseEventArgs e)
{
//鼠标第一次按下时,设置鼠标坐标为第一个点的坐标
this.MyFlag = 1;
this.MyPoint1.X = e.X;
this.MyPoint1.Y = e.Y;
}

}
}
csdsuper 2010-07-07
  • 打赏
  • 举报
回复
不是我要的呀,浪费我一个资源分
[Quote=引用 3 楼 lovelan1748 的回复:]
真雷人呐,看个例子吧
http://download.csdn.net/source/2199536
涂鸦
[/Quote]
oushengfen 2010-07-07
  • 打赏
  • 举报
回复
牛人一个,无语
lovelan1748 2010-07-07
  • 打赏
  • 举报
回复
处理几个鼠标事件即可,上面的例子也有保存
lovelan1748 2010-07-07
  • 打赏
  • 举报
回复
真雷人呐,看个例子吧
http://download.csdn.net/source/2199536
涂鸦
兔子-顾问 2010-07-07
  • 打赏
  • 举报
回复
太执着了
加载更多回复(1)

111,097

社区成员

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

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

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