问题描述
准备开发一个小程序(类型画图程序的设计器),需要借用直线VS自带的控件Microsoft.VisualBasic.PowerPacksLineShape(重新画就算了,毕竟VS有这个东西了)用户可以用鼠标随意移动控件,就是在VS开发中一样,主要功能就是:鼠标移动控件,拉长、拉短。自己随即就想到使用MouseDownMouseMove事件,于是自己就写了一下,但是写出来后,很是奇怪,鼠标移动是,【??一卡一卡的,有时可以移动,有时不会移动???】有时突然跑了很远的距离,调了多久都没有成功,所有把代码贴出来,请大家给指点一下使用C#开发,引用usingMicrosoft.VisualBasic.PowerPacks;usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingMicrosoft.VisualBasic.PowerPacks;namespaceWinDrawTest{publicpartialclassfrmLineCS:Form{publicLineShapeSelectedControl_line=newLineShape();publicfrmLineCS(){InitializeComponent();}///<summary>///画直线///</summary>privatevoidbtnLine_Click(objectsender,EventArgse){ShapeContainercanvas=newShapeContainer();LineShapectrl=newLineShape();canvas.Parent=this.MYpaint;ctrl.Parent=canvas;ctrl.StartPoint=newSystem.Drawing.Point(22,58);//初始开始坐标ctrl.EndPoint=newSystem.Drawing.Point(80,80);//初始结束坐标ctrl.MouseEnter+=newEventHandler(controlLine_MouseEnter);//运行控件事件:鼠标进入控件可见区域ctrl.MouseLeave+=newEventHandler(controlLine_MouseLeave);//运行控件事件:鼠标离开控件可见区域ctrl.MouseDown+=newMouseEventHandler(controlLine_MouseDown);//运行控件事件:鼠标按下事件ctrl.MouseMove+=newMouseEventHandler(controlLine_MouseMove);//运行控件事件:鼠标移动ctrl.MouseUp+=newMouseEventHandler(controlLine_MouseUp);//运行控件事件:鼠标松开事件MYpaint.Invalidate();SelectedControl_line=ctrl;//记录当前控件properControl.SelectedObject=ctrl;//当前控件属性显示}///<summary>///运行控件事件:鼠标进入控件可见区域///</summary>privatevoidcontrolLine_MouseEnter(objectsender,EventArgse){//鼠标样式Cursor=Cursors.SizeAll;}///<summary>///运行控件事件:鼠标离开控件可见区域///</summary>privatevoidcontrolLine_MouseLeave(objectsender,EventArgse){//鼠标样式Cursor=Cursors.Default;}///<summary>///移动按钮点击直线控件XY坐标自动加1移动///</summary>privatevoidbutton1_Click(objectsender,EventArgse){if(this.SelectedControl_line!=null){LineShapecontrol=(LineShape)SelectedControl_line;control.X1+=1;control.Y1+=1;control.X2+=1;control.Y2+=1;properControl.SelectedObject=control;}}PointLineBegin=newPoint();PointLineEnd=newPoint();PointmPos=newPoint();///<summary>///运行控件事件:鼠标按下事件///</summary>privatevoidcontrolLine_MouseDown(objectsender,MouseEventArgse){if(e.Button==MouseButtons.Left){MYpaint.Invalidate();//unselectothercontrolSelectedControl_line=(LineShape)sender;LineShapecontrol=(LineShape)sender;LineBegin=control.StartPoint;LineEnd=control.EndPoint;mPos=MYpaint.PointToClient(MousePosition);log.Text+="鼠标按下记录X:"+mPos.X+"当前Y:"+mPos.Y+"rn";log.Text+="鼠标按下记录开始坐标X:"+control.StartPoint.X+"Y:"+control.StartPoint.Y+"rn";log.Text+="鼠标按下记录结果坐标X:"+control.EndPoint.X+"Y:"+control.EndPoint.Y+"rn";control.Invalidate();}}///<summary>///运行控件事件:鼠标移动///</summary>privatevoidcontrolLine_MouseMove(objectsender,MouseEventArgse){if(e.Button==MouseButtons.Left){LineShapecontrol=(LineShape)sender;PointmPos1=MYpaint.PointToClient(MousePosition);log.Text+="当前X:"+mPos1.X+"Y:"+mPos1.Y+"rn";mPos1.Offset(-mPos.X,-mPos.Y);log.Text+="偏移X:"+mPos1.X+"Y:"+mPos1.Y+"rn";//控件坐标移动control.X1=LineBegin.X+mPos1.X;control.Y1=LineBegin.Y+mPos1.Y;control.X2=LineEnd.X+mPos1.X;control.Y2=LineEnd.Y+mPos1.Y;//control.X1+=1;//control.Y1+=1;//control.X2+=1;//control.Y2+=1;log.Text+="LineBegin:"+control.StartPoint.X+"LineBegin:"+control.StartPoint.Y+"rn";log.Text+="LineEnd:"+control.EndPoint.X+"LineEnd:"+control.EndPoint.Y+"rn";properControl.SelectedObject=control;Invalidate();return;}}///<summary>///运行控件事件:鼠标松开///</summary>privatevoidcontrolLine_MouseUp(objectsender,MouseEventArgse){if(e.Button==MouseButtons.Left){LineShapecontrol=(LineShape)sender;Cursor.Clip=System.Drawing.Rectangle.Empty;control.Invalidate();}}}}
解决方案
解决方案二:
开双缓冲,换cskin或者dskin,换d2d
解决方案三:
开双缓冲SetStyle(ControlStyles.ResizeRedraw,true);SetStyle(ControlStyles.OptimizedDoubleBuffer,true);SetStyle(ControlStyles.AllPaintingInWmPaint,true);加上没有用啊????