c# 鼠标移动一条画好的线,鼠标到了哪里线就跟着到了哪里?

解决方案 »

  1.   

    不是这个意思 我明白你说的 我是用c#写的 先用GDI把表格画好,然后在画一根线 这根线跟随鼠标的移动而移动,我把原代码发过来你帮我看看
      

  2.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Drawing.Drawing2D;namespace MPicture
    {
        public partial class Form1 : Form
        {
            private Graphics g;
            private string[] strTitle ={ "(00-07)点", "(08-15)点", "(16-23)点" };
            private string[,] strTitle1=new string[3,8];        public Form1()
            {
                InitializeComponent();
            }
            
            private void Form1_Load(object sender, EventArgs e)
            {
                #region 标题
                int k = -1;//临时变量存储1到23的值
                for (int i = 0; i < 3; i++)
                {
                    for (int j = 0; j < 8; j++)
                    {
                        k++;
                        strTitle1[i,j] = Convert.ToString(k);
                    }
                }
                #endregion 标题
            }
            
            #region pictureBox1的相关函数
            private void pictureBox1_Paint(object sender, PaintEventArgs e)
            {
                #region 基本参数
                Graphics g = e.Graphics;
                Pen GreenPen = new Pen(Color.Green, 2);
                Brush brush = GreenPen.Brush;
                GreenPen.DashStyle = DashStyle.Solid;   //定义画线类型为虚线
                #endregion 基本参数            #region 画横线和竖线
                GetHLien(g,pictureBox1,pictureBox1.Height,70,GreenPen,brush);
                GetSLine(g, pictureBox1, pictureBox1.Width, GreenPen, brush, strTitle1,strTitle);
                #endregion 画横线和竖线
                base.OnPaint(e);
            }
            /// <summary>
            /// 画横线
            /// </summary>
            /// <param name="g"></param>
            /// <param name="p1"></param>
            /// <param name="height"></param>
            /// <param name="step"></param>
            /// <param name="Hpen"></param>
            /// <param name="Hbrush"></param>
            private void GetHLien(Graphics g,PictureBox  p1, int height, int step,Pen Hpen,Brush Hbrush)
            {
                int i=0;
                if (g != null)
                {
                    for (i = 0; i < height; i += step)
                    {
                        g.DrawLine(Hpen, 0, 0 + i, pictureBox1.Right, 0 + i);
                    }
                }
            }
            /// <summary>
            /// 画竖线
            /// </summary>
            /// <param name="g"></param>
            /// <param name="p1"></param>
            /// <param name="width">画布的整体宽度</param>
            /// <param name="Spen"></param>
            /// <param name="Sbrush"></param>
            /// <param name="strTitle1">第一列的标题</param>
            /// <param name="strTitle">其他列的标题</param>
            private void GetSLine(Graphics g,PictureBox p1,int width,Pen Spen,Brush Sbrush,string[,]strTitle1 ,string[] strTitle)
            {
                int i,j,m,n;
                Rectangle rect = new Rectangle();
                rect.X = p1.Left;
                rect.Y = p1.Top;
                rect.Width = 0;
                rect.Height = 0;            StringFormat f1 = new StringFormat(StringFormatFlags.LineLimit);
                f1.Alignment = StringAlignment.Center;
                f1.LineAlignment = StringAlignment.Center;            int colWidth = 0;//每列的宽度
                int firstCol = 0;//第一列的宽度            colWidth = (int)((p1.Width-102) / 8);//除第一列的每列的宽度
                firstCol = 102;
                #region 第一列
                g.DrawLine(Spen, 0, 0, 0, p1.Bottom);//第一列
                g.DrawLine(Spen, firstCol, 0, firstCol, p1.Bottom);//第二列
                for (i = 0; i < strTitle .Length; i++)
                {
                    rect.Width = firstCol;
                    rect.Height = p1.Height / strTitle.Length;
                    g.DrawRectangle(Spen, rect);
                    g.DrawString(strTitle[i], new Font("黑体", 12), Sbrush, rect, f1);
                    rect.Y += rect.Height;
                }
                #endregion  第一列            #region 其他列
                f1.Alignment = StringAlignment.Center;
                f1.LineAlignment = StringAlignment.Far;
                for (i = firstCol + colWidth; i <= width; i += colWidth)
                {
                    g.DrawLine(Spen, 0 + i, 0, 0 + i, pictureBox1.Bottom);
                }
                for (i = firstCol + colWidth,m=0; i <= width; i += colWidth,m++)
                {
                    rect.X =i-colWidth;
                    rect.Width=colWidth;
                    rect.Y = 0;
                    for (j = rect.Height,n=0; j <= p1.Height; j += rect.Height,n++)
                    {
                        g.DrawRectangle(Spen, rect);
                        g.DrawString(strTitle1[n,m], new Font("黑体", 12), Sbrush, rect, f1);
                        rect.Y += rect.Height;
                    }
                }
                #endregion 其他列
            }
            #endregion pictureBox1的相关函数        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
            {
                g = this.pictureBox1.CreateGraphics();
                g.DrawLine(new Pen(Brushes.Red, 1), e.X, 0, e.X, 622);
            }   
        }
    }