C#GDI+如何在WinForm上用鼠标来绘制图形?

lq0922 2012-10-09 08:23:34
C#GDI+如何在WinForm上用鼠标来绘制图形?
...全文
933 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
足球中国 2012-10-09
  • 打赏
  • 举报
回复
绘制的代码写到onpaint里就好了。
Trent1985 2012-10-09
  • 打赏
  • 举报
回复
这个是最简单的,你试一下:
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 WindowsFormsApplication14
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
Graphics g = this.CreateGraphics();
g.DrawEllipse(new Pen(Color.Red, 2), new Rectangle(e.X - 1, e.Y - 1, 2, 2));
g.Dispose();
}
}
}
Trent1985 2012-10-09
  • 打赏
  • 举报
回复
用鼠标事件结合窗体的绘图事件就可以了!
lq0922 2012-10-09
  • 打赏
  • 举报
回复
首先,感谢楼上的回帖! 但是还有很多问题。

第一,我需要类似于word里面插入图形之后单击鼠标左键移动绘制指定图形
第二,绘制图形的时候鼠标单击左键移动会出现很多图形
leixf2016 2012-10-09
  • 打赏
  • 举报
回复

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;
using System.Net;
using System.IO;

using System.Threading;
using System.Drawing.Drawing2D;

namespace Test
{
public partial class Form1 : Form
{
enum MouseState
{
None = 0,
MouseLeftDown = 1,
MouseRightDown = 2,
}

private MouseState _MouseState = MouseState.None;

public Form1()
{
InitializeComponent();

this.Load += new EventHandler(Form1_Load);
this.MouseMove += new MouseEventHandler(Form1_MouseMove);
this.MouseDown += new MouseEventHandler(Form1_MouseDown);
this.MouseUp += new MouseEventHandler(Form1_MouseUp);
}

void Form1_MouseUp(object sender, MouseEventArgs e)
{
_MouseState = MouseState.None;
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
this.Refresh();
}
}

void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
_MouseState = MouseState.MouseLeftDown;
return;
}

if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
_MouseState = MouseState.MouseRightDown;
return;
}
}

void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (_MouseState == MouseState.None)
{
return;
}

if (_MouseState == MouseState.MouseLeftDown)
{
Console.WriteLine(e.X + ", " + e.Y);

Graphics g = this.CreateGraphics();
//g.DrawEllipse(new Pen(Color.Red, 2), new Rectangle(e.X - 1, e.Y - 1, 2, 2));
g.DrawLine(new Pen(Color.Red, 2), new Point(e.X - 2, e.Y - 2), new Point(e.X, e.Y));
g.Dispose();
return;
}
}

void Form1_Load(object sender, EventArgs e)
{
this.AutoScaleBaseSize = new Size(5, 13);
this.ClientSize = new Size(800, 500);
}
}
}



实现功能:当鼠标按下左键并不松开的情况下,可以在工作区域画图,松开鼠标后停止画图,点击鼠标右键后,重新刷新界面。

BUG:当鼠标移动速度过快时,线条不连续,求解决!


内容概要:本文围绕不确定环境下的多式联运路径优化问题展开研究,提出并实现了基于AFO算法、遗传算法(GA)和粒子群优化算法(PSO)的三种智能优化方法,并借助Matlab平台完成算法编程与仿真。研究构建了考虑时间、成本、转运风险等多重不确定因素的路径优化模型,系统比较了AFO、GA、PSO三种算法在收敛速度、全局寻优能力和稳定性方面的表现,同时引入Matlab自带的全局优化搜索器作为基准对照,深入分析各算法在复杂物流网络中的适用边界与性能差异。研究表明,AFO算法在解决此类组合优化问题时展现出更快的收敛效率和更强的局部规避能力。; 适合人群:具备一定Matlab编程基础与运筹优化知识,从事物流工程、交通运输规划、智能算法开发等相关领域的研究生、科研人员及工程技术人员。; 使用场景及目标:①应用于多式联运、综合货运网络中的路径决策支持系统构建;②为不确定性条件下复杂路径规划问题提供智能算法选型依据与技术实现方案;③支持科研人员复现主流优化算法并开展横向性能对比实验,推动算法改进与实际落地。; 阅读建议:建议读者结合提供的Matlab代码逐模块分析算法实现流程,重点理解目标函数设计、约束条件处理及参数敏感性分析部分,可通过调整问题规模与算法参数进行对比实验,进一步拓展至动态路径规划或大规模网络优化等延伸场景。

111,129

社区成员

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

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

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