我找到的一个解决方法只能基于整个form,不能针对指定的控件(如一个画图区域),那位高手能解决一下?
下面是我找到的在整个form上画十字线的代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private Point currentPoint = new Point(0, 0);
        private Pen pen = new Pen(Color.Black);        private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.X != currentPoint.X || e.Y != currentPoint.Y)
            {
                currentPoint = new Point(e.X, e.Y);
                this.Invalidate(true);    
            }
        }        
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e); 
            e.Graphics.DrawLine(pen, 0, currentPoint.Y, this.Width, currentPoint.Y);    //绘制横线
            e.Graphics.DrawLine(pen, currentPoint.X, 0, currentPoint.X, this.Height);  //会制纵线
        }        private void Form1_Closed(object sender, System.EventArgs e)
        {
            pen.Dispose();
        }
|

解决方案 »

  1.   

    protected override void OnPaint(PaintEventArgs e) 
            { 
                base.OnPaint(e);  
                e.Graphics.DrawLine(pen, 0, currentPoint.Y, this.Width, currentPoint.Y);    //绘制横线 
                e.Graphics.DrawLine(pen, currentPoint.X, 0, currentPoint.X, this.Height);  //会制纵线 
            } 
    这个窗体重绘,当然只针对form,在this.clientRectange内有效,不针对某控件你可以写成单独事件public void MouseEnvents(MouseEventArgs e)
    {
      if (e.X != currentPoint.X  ¦ ¦ e.Y != currentPoint.Y) 
         { 
            currentPoint = new Point(e.X, e.Y); 
            //this.Invalidate(true); ?????
         }
    }
    public void Piants(Pen pen1,Pen pen2,PaintEventArgs e)
    {
       e.Graphics.DrawLine(pen1, 0, currentPoint.Y, this.Width, currentPoint.Y);    //绘制横线 
       e.Graphics.DrawLine(pen2, currentPoint.X, 0, currentPoint.X, this.Height);  //会制纵线 
    }////调用
    private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) 
    {
        MouseEnvents(e);
        this.Invalidate(true);
    }
    protected override void OnPaint(PaintEventArgs e) 
    {
        Piants(pen,pen,e);
    }你只要在其他控件调用这两个自己写的事件就可以了!
      

  2.   

    我照着做了,可是十字线只在form上显示,控件内还是空白?
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace WindowsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private Point currentPoint = new Point(0, 0);
            private Pen pen = new Pen(Color.Black);        private void Form1_Closed(object sender, System.EventArgs e)
            {
                pen.Dispose();
            }
            public void MouseEnvents(MouseEventArgs e)
            {
                if (e.X != currentPoint.X  || e.Y != currentPoint.Y) 
                { 
                    currentPoint = new Point(e.X, e.Y); 
                    //this.Invalidate(true); ?????
                }
            }
            public void Piants(Pen pen1,Pen pen2,PaintEventArgs e)
            {
                e.Graphics.DrawLine(pen1, 0, currentPoint.Y, this.Width, currentPoint.Y);    //绘制横线 
                e.Graphics.DrawLine(pen2, currentPoint.X, 0, currentPoint.X, this.Height);  //会制纵线 
            }
            protected override void OnPaint(PaintEventArgs e) 
            {
                Piants(pen,pen,e);
            }        private void panel1_MouseMove(object sender, MouseEventArgs e)
            {
                MouseEnvents(e);
                this.Invalidate(true);
            }
       }
    }
      

  3.   

    晕倒了 private void panel1_Paint(PaintEventArgs e)//在你要出现画线的控件的paint事件都调用!
      { 
        MouseEnvents(e); 
        this.Invalidate(true); 
      } 
      

  4.   

    this.Invalidate(true); 
    这个不要(让子控件无效)
      

  5.   

    给你调试了下        private void button1_MouseMove(object sender, MouseEventArgs e)
            {
                MouseEnvents(e); 
            }        private void button1_Paint(object sender, PaintEventArgs e)
            {
                Piants(Pens.Red, Pens.Orange, e);
                button1.Invalidate(true);//这个还是要,不然线不会动!
            }
      

  6.   

    这样做,发现CPU占用率及高,而原先的却没有?有什么好方法吗