今天有碰到需要绘制一个点的程序,想了一个办法,可以用绘制椭圆来代替绘制一个点,但是绘制出来的总是偏差,偏差还很多(我想在鼠标点画,可画出来就在鼠标点的下方),不知这是什么原因,有什么办法可以解决吗?求救!!
代码:
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.Collections;
using System.Drawing.Drawing2D;
namespace test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.MaximizeBox = false;
this.MinimizeBox = false;
this.toolStripStatusLabel1.Text = "当前坐标X值为";
this.toolStripStatusLabel2.Text = "当前坐标Y值为";
this.toolStripStatusLabel3.Text = "";
this.toolStripStatusLabel4.Text = "";
} long currentX=0;
long currentY = 0; private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics myGraphics = this.CreateGraphics();
Pen myPen = new Pen(Color.Black, 2);
myPen.DashStyle = DashStyle.Solid;
myGraphics.DrawRectangle(myPen, 18, 148, 450, 300);
myPen.Dispose();
myGraphics.Dispose();
} private void Form1_MouseMove(object sender, MouseEventArgs e)
{
currentX = MousePosition.X;
currentY = MousePosition.Y;
this.toolStripStatusLabel3.Text = Convert.ToString(currentX);
this.toolStripStatusLabel4.Text = Convert.ToString(currentY);
} private void Form1_MouseDown(object sender, MouseEventArgs e)
{
Graphics myGraphics = this.CreateGraphics();
Pen myPen = new Pen(Color.Black, 4);
myPen.DashStyle = DashStyle.Solid;
myGraphics.DrawEllipse(myPen, Convert.ToInt32(e.X), Convert.ToInt32(e.Y), 5, 5);
myPen.Dispose();
myGraphics.Dispose();
}
}
}
代码:
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.Collections;
using System.Drawing.Drawing2D;
namespace test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.MaximizeBox = false;
this.MinimizeBox = false;
this.toolStripStatusLabel1.Text = "当前坐标X值为";
this.toolStripStatusLabel2.Text = "当前坐标Y值为";
this.toolStripStatusLabel3.Text = "";
this.toolStripStatusLabel4.Text = "";
} long currentX=0;
long currentY = 0; private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics myGraphics = this.CreateGraphics();
Pen myPen = new Pen(Color.Black, 2);
myPen.DashStyle = DashStyle.Solid;
myGraphics.DrawRectangle(myPen, 18, 148, 450, 300);
myPen.Dispose();
myGraphics.Dispose();
} private void Form1_MouseMove(object sender, MouseEventArgs e)
{
currentX = MousePosition.X;
currentY = MousePosition.Y;
this.toolStripStatusLabel3.Text = Convert.ToString(currentX);
this.toolStripStatusLabel4.Text = Convert.ToString(currentY);
} private void Form1_MouseDown(object sender, MouseEventArgs e)
{
Graphics myGraphics = this.CreateGraphics();
Pen myPen = new Pen(Color.Black, 4);
myPen.DashStyle = DashStyle.Solid;
myGraphics.DrawEllipse(myPen, Convert.ToInt32(e.X), Convert.ToInt32(e.Y), 5, 5);
myPen.Dispose();
myGraphics.Dispose();
}
}
}
解决方案 »
- 求教高人用C#提升进程访问权限的方法!!~~~
- 一个很简单的问题(新手,见笑)
- 关于泛型集合一直纠结的问题,请指点
- 有没有插入的时候自动排序(根据IComparer的Compare方法)的容器,不要用key/value的形式,是单独Add一个对象就能排序的
- 上传图片如何限制大小?
- 升两星,有120多人支持,决定再散分。
- 大家看看这段代码当中参数是如何传递的?内存是如何分配的??在线等
- C# access数据库显示到listview,选中一项显示到label
- 问一个c#数据库编程的问题
- 大家好:请问在C#中如何得到XML中的值呢?具体请看内容 在线呀.谢,
- .net程序员如何快速上手java?
- 编码将wav文件转为MP3文件
DrawEllipse(
myPen,
Convert.ToInt32(e.X) - width / 2,
Convert.ToInt32(e.Y) - height / 2,
width, height);
DrawEllipse这个重载里面第2、3个参数指的是要绘制的椭圆的左上。
显然你想用鼠标点击绘一个椭圆,那么鼠标的位置应该是要绘制椭圆的中心才是。
你原来那样是以鼠标的位置定为椭圆的左上来画的,所以看似有偏移。