using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.VisualBasic.PowerPacks;
using VLK.CommonClass;
using System.Drawing.Drawing2D; namespace VLK
{
    public partial class frmMain : Form
    {
        int screenhori;
        int screenver;
        int screentotal;
        long picwidthlen;
        long picheightlen;
        Point startPoint;
        Point endPoint;
        Point realStart;
        Point realEnd;         //private bool mouseStatus = false;//鼠标状态,false为松开            Microsoft.VisualBasic.PowerPacks.ShapeContainer canvas = new ShapeContainer();
        Microsoft.VisualBasic.PowerPacks.RectangleShape theShape = new RectangleShape();        public frmMain()
        {
            InitializeComponent();
           
        }        private void frmMain_Load(object sender, EventArgs e)
        {                        DataCon dataconnum = new DataCon();
            DataOperate dataoperatenum = new DataOperate();
            string strSqlscreennum = "select screenhorinum,screenvernum  from tb_screenset where ID=1";
            DataSet dsscreennum = dataoperatenum.getDs(strSqlscreennum, "tb_screenset");            screenhori = (int)dsscreennum.Tables[0].Rows[0]["screenhorinum"];
            screenver = (int)dsscreennum.Tables[0].Rows[0]["screenvernum"];
            screentotal = screenver * screenhori;            picwidthlen = Panl_Screen.Width / screenhori;
            picheightlen = Panl_Screen.Width / screenver;
            //if (screentotal > 1)
            //{
                PictureBox[] PictureBoxArray = new PictureBox[screentotal];
                //PictureBoxArray[0] = new PictureBox();
                //PictureBoxArray[0] = Pic_Screen;                for (int i = 0; i < screentotal-1; i++)
                {
                    PictureBoxArray[i] = new PictureBox();
                    PictureBoxArray[i].Name = "Pic_Screen" + i;
                    //PictureBoxArray[i].Parent = Panl_Screen;
                    
                    Panl_Screen.Controls.Add(PictureBoxArray[i]);
                                    }
                Panl_Screen.Controls.Add(canvas);                canvas.Parent = Panl_Screen;
                theShape.Parent = canvas;
                canvas.Visible = false;
                theShape.Visible = false;
                theShape.BorderColor = Color.Red;
                theShape.BorderStyle = System.Drawing.Drawing2D.DashStyle.Dot;
                canvas.SendToBack();                Panl_Screen.Controls.Add(shapewinsy1);
                shapewinsy1.Parent = Panl_Screen;
                shapewinsy1.BackColor = Color.Transparent;
                                for (int i = 0; i < screentotal-1; i++)
                {
                    PictureBoxArray[i].MouseDown += new System.Windows.Forms.MouseEventHandler(this.PictureBoxArray_MouseDown); 
                    PictureBoxArray[i].MouseMove += new System.Windows.Forms.MouseEventHandler(this.PictureBoxArray_MouseMove);
                    PictureBoxArray[i].MouseUp += new System.Windows.Forms.MouseEventHandler(this.PictureBoxArray_MouseUp);
                                        PictureBoxArray[i].Location = new System.Drawing.Point((i % screenhori) * (int)picwidthlen, (i / screenhori) * (int)picheightlen);//图像的位置,以左上角定位
                    PictureBoxArray[i].Size = new System.Drawing.Size((int)picwidthlen, (int)picheightlen);//图像的大小
                    PictureBoxArray[i].Image = imageListScreen.Images[1];
                    PictureBoxArray[i].SizeMode = PictureBoxSizeMode.StretchImage;
                    PictureBoxArray[i].Tag = i;
                    PictureBoxArray[i].SendToBack();
                }                                
        }
               public void PictureBoxArray_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            
            if (e.Button == MouseButtons.Left)
            {                //mouseStatus = true;
                int n;
                PictureBox gtnum = (PictureBox)sender;
                n = (int)gtnum.Tag;
                startPoint.X = e.X + (n % screenhori) * (int)picwidthlen;
                startPoint.Y = e.Y + (n / screenhori) * (int)picheightlen;             }
        }        public void PictureBoxArray_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            //程序逻辑  
            //提示 ((PictureBox)sender)  可以操作触发此事件的控件对象            if (e.Button == MouseButtons.Left)
            {                //if (mouseStatus)
                //{
                    canvas.Visible = false;
                    theShape.Visible = false;
                    canvas.SendToBack();
                int n;
                PictureBox gtnu = (PictureBox)sender;
                n = (int)gtnu.Tag;
                endPoint.X = e.X + (n % screenhori) * (int)picwidthlen;
                endPoint.Y = e.Y + (n / screenhori) * (int)picheightlen;
                realStart.X = Math.Min(startPoint.X, endPoint.X);
                realStart.Y = Math.Min(startPoint.Y, endPoint.Y);
                realEnd.X = Math.Max(startPoint.X, endPoint.X);
                realEnd.Y = Math.Max(startPoint.Y, endPoint.Y);
                canvas.Location = realStart;
                canvas.Width = realEnd.X - realStart.X;
                canvas.Height = realEnd.Y - realStart.Y;
                theShape.Location = realStart;
                theShape.Width = realEnd.X - realStart.X;
                theShape.Height = realEnd.Y - realStart.Y;
                canvas.Visible = true;
                theShape.Visible = true;
                canvas.BringToFront();
                this.UpdateZOrder();
                //}            }         }        public void PictureBoxArray_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            //程序逻辑  
            //提示 ((PictureBox)sender)  可以操作触发此事件的控件对象
            //mouseStatus = false;            //theShape.Visible = false;
            //canvas.SendToBack();
        }               }
        }

解决方案 »

  1.   

    你应该把画图的过程放到OnPaint事件中,mousemove时,invalidate让画布时时刷新
      

  2.   

    我显示矩形框用的是以下这两个控件呀:
    Microsoft.VisualBasic.PowerPacks.ShapeContainer canvas = new ShapeContainer();
    Microsoft.VisualBasic.PowerPacks.RectangleShape theShape = new RectangleShape();
    没有在画布上画矩形也需要重绘吗?后来我把显示矩形的代码放到重绘事件里面试了一下,也还是不跟鼠标同步。这是什么问题呢?我的显示矩形的代码是这样的:
     public void PictureBoxArray_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
            {           
                    canvas.Visible = true;
                    theShape.Visible = true;
                    canvas.BringToFront();                                           
            }请 bdmh 帮忙给看一下,这样有问题吗?到底是什么原因造成的矩形框显示跟鼠标轨迹不同步呢?
     
      

  3.   

    你在mouseover事件中设置下断点跟踪一下,看看。
      

  4.   

    kingdom_0 你好,谢谢你的回复!但是PictureBox没有mouseover事件呀?我在mousemove事件里面跟踪了一下,也是正常的呀?
      

  5.   

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
            {
                label1.Text = e.X.ToString() + "^^" + e.Y.ToString();
            }