- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
这是我用鼠标在图表上绘制自定义线条的绘图代码。你能帮我正确地做吗?
namespace Grafi
{
public partial class Form1 : Form
{
bool isDrawing = false;
Point prevPoint;
public Form1()
{
InitializeComponent();
}
private void chartTemperature_MouseDown(object sender, MouseEventArgs e)
{
isDrawing = true;
prevPoint = e.Location;
}
private void chartTemperature_MouseMove(object sender, MouseEventArgs e)
{
Pen p = new Pen(Color.Red, 2);
if (isDrawing)
{
Graphics g = chartTemperature.CreateGraphics();
g.DrawLine(p, prevPoint, e.Location);
prevPoint = e.Location;
numOfMouseEvents = 0;
}
p.Dispose();
}
private void chartTemperature_MouseUp(object sender, MouseEventArgs e)
{
isDrawing = false;
}
}
}
问题是,当我调整表格大小时,我的线条消失了。只要引发 onPaint 事件,它就会消失。
最佳答案
试试这个... 这是一种笔画绘制方法,实现起来非常简单,并且尽可能接近您自己的代码。 Stokes 是鼠标移动的单独集合。鼠标在向下和向上之间的每次移动都被记录为一个笔划,所有的笔划都被收集起来,然后在触发 paint 事件时重新绘制。这个例子很简单,但可以作为一个很好的起点。
请注意,您必须为图表对象添加绘制处理程序。
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace Grafi
{
public partial class Form1 : Form
{
bool isDrawing;
// our collection of strokes for drawing
List<List<Point>> _strokes = new List<List<Point>>();
// the current stroke being drawn
List<Point> _currStroke;
// our pen
Pen _pen = new Pen(Color.Red, 2);
public Form1()
{
InitializeComponent();
}
private void chartTemperature_MouseDown(object sender, MouseEventArgs e)
{
isDrawing = true;
// mouse is down, starting new stroke
_currStroke = new List<Point>();
// add the initial point to the new stroke
_currStroke.Add(e.Location);
// add the new stroke collection to our strokes collection
_strokes.Add(_currStroke);
}
private void chartTemperature_MouseMove(object sender, MouseEventArgs e)
{
if (isDrawing)
{
// record stroke point if we're in drawing mode
_currStroke.Add(e.Location);
Refresh(); // refresh the drawing to see the latest section
}
}
private void chartTemperature_MouseUp(object sender, MouseEventArgs e)
{
isDrawing = false;
}
private void chartTemperature_Paint(object sender, PaintEventArgs e)
{
// now handle and redraw our strokes on the paint event
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
foreach (List<Point> stroke in _strokes.Where(x => x.Count > 1))
e.Graphics.DrawLines(_pen, stroke.ToArray());
}
}
}
关于c# - 在 C# 中用鼠标画线的正确方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4164864/
有人可以向我解释该声明在C++中的含义吗?我从未见过这样的声明,只是对它的含义和作用感到困惑: int ix((dx > 0) - (dx > 1)); 最佳答案 您可以在括号或花括号中使用初始化程序
我有一个带有单词的mysql数据库。我用 while 语句打印所有单词。所以我觉得: 马铃薯番茄生菜 一切正常,但我想按长度对单词进行排序。我试过: if(strlen($go['words']) =
我忠实的路径遍历方法不再有效——它将空格视为分隔符。好久没做批处理编程了。 使用 FOR 循环时,唯一允许使用分隔符的是 FOR/F 选项。 我不想创建一个包含路径的临时文件,希望做如下的事情: C:
新建一个表: ? 1
我有一些带有多行块的文本文件,例如 2011/01/01 13:13:13,, Some Certain Text,=, [ certain text [
我想在 Vim 中文件的不同部分之间进行一些很好的分离: 我想用#'s 填充一行,然后在中间写上我的标题: ############################# 居中标题############
我该如何逃生 "*"至 "\*"在clojure?似乎无法让它工作: (s/replace "A*B" #"*" "*")生产 "A*B" (当然) (s/replace "A*B" #"*" "\*
这周我一直在努力更熟悉 C。我一直在阅读C Primer Plus (5th Edition) 但是我仍然在使用变量和指针时遇到了一些麻烦。 这是我用来测试的脚本: int main (int arg
在 Dart 中,初始化 List 有什么区别?使用 new 运算符并使用文字对其进行初始化? 情况1: List args = new List(2); args[0] = 1; args[1] =
我有一个字符向量,如下所示: "Internet" "Internet" "-1" "-5" "Internet" "Internet" 我想替换所有负数值的值(-1、-5 等
我有一个名为 gen 的数据框,如下所示 A B C D E 1 NA 4.35 35.3 3.36 4.8
我有一个字符向量,如下所示: "Internet" "Internet" "-1" "-5" "Internet" "Internet" 我想替换所有负数值的值(-1、-5 等
我想知道为什么 CMake 中的变量经常用美元符号和大括号括起来。例如,我看到这个电话in a CMake tutorial . include_directories(${PROJECT_BINAR
我正在尝试做这样的事情 $this->db->count_all("grant_money")->where('id',5); 这可能吗? 如果有任何其他方法可以做到这一点,请告诉我。谢谢 我想像上面
为什么这是有效的: int a = 5; int *aPtr = &a; printf("%i", *aPtr); 但这不是: int a = 5; int aPtr = &a; printf("%i
假设我有一个格式为“11.23.13”的日期字符串,我想用“/”替换每个点,使其看起来像“11/23/13”。 这是我的代码,但它无法正常工作,因为正则表达式看到“.”并将其解释为匹配每个字符而不是新
如何在键盘输入的字符处打印*? 例子: 如果我在控制台中输入:mouli,那么它应该将 m 替换为 *,然后是 o用 * 等等。 最佳答案 使用标准 API 无法解决此问题。如果这确实是一个明确的要求
我最近开始学习 Javascript,同时对卡在这段代码中的代码进行了一些实验: var k = { ab: "hi", func: function() { cons
我需要用“.”替换第一列中的重复项 例如: name1 name1 name1 name2 name2 name3 name3 我需要输出: name1 . . name2 . name3 . 我有这
我有以下两个表 education 和 jobs,每个表都有时间戳字段。在续集语句中,我想选择并确定两个表中保存的两个时间戳中哪个是最新的。 我已经尝试了以下但并不愉快; SELECT e.Sta
我是一名优秀的程序员,十分优秀!