今天有碰到需要绘制一个点的程序,想了一个办法,可以用绘制椭圆来代替绘制一个点,但是绘制出来的总是偏差,偏差还很多(我想在鼠标点画,可画出来就在鼠标点的下方),不知这是什么原因,有什么办法可以解决吗?求救!!
代码:
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();
        }
    }
}

解决方案 »

  1.   

    错在这里:myGraphics.DrawEllipse(myPen, Convert.ToInt32(e.X), Convert.ToInt32(e.Y), 5, 5)楼主没理解怎么绘椭圆。
    DrawEllipse(
    myPen,
    Convert.ToInt32(e.X) - width / 2, 
    Convert.ToInt32(e.Y) - height / 2,
    width, height);
      

  2.   

    myGraphics.DrawEllipse(myPen, Convert.ToInt32(e.X), Convert.ToInt32(e.Y), 1, 1);
      

  3.   

    还是没明白,为什么必须用e.x,e.y,不能用MousePosition.X转换来得值么
      

  4.   

    问题不是鼠标位置的问题。
    DrawEllipse这个重载里面第2、3个参数指的是要绘制的椭圆的左上。
    显然你想用鼠标点击绘一个椭圆,那么鼠标的位置应该是要绘制椭圆的中心才是。
    你原来那样是以鼠标的位置定为椭圆的左上来画的,所以看似有偏移。