| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 493 人关注过本帖, 1 人收藏
标题:c#GDI 怎么实现拖动画好的直线?
只看楼主 加入收藏
未名湖的云
Rank: 3Rank: 3
来 自:安徽
等 级:论坛游侠
帖 子:130
专家分:190
注 册:2012-10-11
结帖率:94.12%
收藏(1)
已结贴  问题点数:2 回复次数:1 
c#GDI 怎么实现拖动画好的直线?
c#GDI  怎么实现拖动画好的直线?求指导
2014-02-11 14:02
wangnannan
Rank: 18Rank: 18Rank: 18Rank: 18Rank: 18
等 级:贵宾
威 望:87
帖 子:2546
专家分:9359
注 册:2007-11-3
收藏
得分:2 
程序代码:
//以下是完整代码,可以直接编译运行
//-------------------------------------------
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing; 

namespace q2
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
} 

/// <summary>
/// 线条对象
/// </summary>
class Line
{
/// <summary>
/// 建立线条对象,并设置起点
/// </summary>
/// <param name="startPoint">此线条的起点</param>
public Line(Point startPoint)
{
StartPoint = startPoint;
EndPoint = startPoint;
}
public Point StartPoint = Point.Empty;
public Point EndPoint = Point.Empty;
} 

public class DrawPanel : Control
{
public DrawPanel()
{
this.DoubleBuffered = true;
this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw, true);

 }
} 

/// <summary>
/// 窗口定义
/// </summary>
public class Form1 : Form
{
public Form1()
{
drawPanel.BackColor = Color.White;
drawPanel.Cursor = Cursors.Cross;
drawPanel.Dock = DockStyle.Fill;
drawPanel.MouseDown += new MouseEventHandler(drawPanel_MouseDown);
drawPanel.MouseUp += new MouseEventHandler(drawPanel_MouseUp);
drawPanel.MouseMove += new MouseEventHandler(drawPanel_MouseMove);
drawPanel.Paint += new PaintEventHandler(drawPanel_Paint);
Controls.Add(drawPanel);
} 

/// <summary>
/// 用于保存绘出线条的集合
/// </summary>
private List<Line> lines = new List<Line>(); 

/// <summary>
/// 用于保存当前正在绘制的线条
/// </summary>
private Line drawingLine = null; 

/// <summary>
/// 用于显示绘图的面板组件
/// </summary>
private DrawPanel drawPanel = new DrawPanel(); 

/// <summary>
/// 在绘图区释放鼠标,结束当前线条绘制
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void drawPanel_MouseUp(object sender, MouseEventArgs e)
{
if (drawingLine == null) return;
drawingLine.EndPoint = e.Location;
drawingLine = null;
} 

/// <summary>
/// 在绘图区按下鼠标,开始绘制新线条
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void drawPanel_MouseDown(object sender, MouseEventArgs e)
{
drawingLine = new Line(e.Location);
lines.Add(drawingLine);
} 

/// <summary>
/// 在绘图区移动鼠标时,如果正在绘制新线条,就更新绘制面板
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void drawPanel_MouseMove(object sender, MouseEventArgs e)
{
if(drawingLine != null)
{
drawingLine.EndPoint = e.Location;
drawPanel.Invalidate();
}
} 

/// <summary>
/// 绘制效果到面板
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void drawPanel_Paint(object sender, PaintEventArgs e)
{
Bitmap bp = new Bitmap(drawPanel.Width, drawPanel.Height); // 用于缓冲输出的位图对象
Graphics g = Graphics.FromImage(bp); 

g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; // 消锯齿(可选项) 

Pen p = new Pen(Color.Black);
foreach (Line line in lines)
{
if (line == drawingLine)
{
// 当前绘制的线条是正在鼠标定位的线条
p.Color = Color.Blue;
}
else
{
p.Color = Color.Black;
}
g.DrawLine(p, line.StartPoint, line.EndPoint);
}
// 将缓冲位图绘制到输出
e.Graphics.DrawImage(bp, Point.Empty);
}
}
}
参考这个demo

出来混,谁不都要拼命的嘛。 。拼不赢?那就看谁倒霉了。 。有机会也要看谁下手快,快的就能赢,慢。 。狗屎你都抢不到。 。还说什么拼命?
2014-02-11 15:26
快速回复:c#GDI 怎么实现拖动画好的直线?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.057021 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved