首页  编辑  

拖动控件、绘制虚框、限制鼠标移动范围

Tags: /C#/界面处理/   Date Created:

private Point downPoint;

private Rectangle downRectangle;

private Rectangle lastRectangle;

private void pictureBox1_MouseDown( object sender , MouseEventArgs e)

{

    if (e . Button != MouseButtons . Left) return ;

   

    Cursor . Clip = (( Control )sender) . Parent . RectangleToScreen(

       (( Control )sender) . Parent . ClientRectangle);

   downPoint = e . Location;

   downRectangle =

        new Rectangle ( 0 , 0 , (( Control )sender) . Width, pictureBox1 . Height);

   downRectangle . Offset((( Control )sender) . PointToScreen( new Point ( 0 , 0 )));

    ControlPaint . DrawReversibleFrame(

       downRectangle, Color . White, FrameStyle . Thick);

   lastRectangle = downRectangle;

}

private void pictureBox1_MouseMove( object sender , MouseEventArgs e)

{

    if (e . Button != MouseButtons . Left) return ;

    ControlPaint . DrawReversibleFrame(

       lastRectangle, Color . White, FrameStyle . Thick);

    Rectangle rectangle = downRectangle;

   rectangle . Offset(e . X - downPoint . X, e . Y - downPoint . Y);

    ControlPaint . DrawReversibleFrame(

       rectangle, Color . White, FrameStyle . Thick);

   lastRectangle = rectangle;

}

private void pictureBox1_MouseUp( object sender , MouseEventArgs e)

{

    if (e . Button != MouseButtons . Left) return ;

    Cursor . Clip = Rectangle . Empty;

    ControlPaint . DrawReversibleFrame(

       lastRectangle, Color . White, FrameStyle . Thick);

   

   pictureBox1 . Location = new Point (

       (( Control )sender) . Location . X + e . X - downPoint . X,

       (( Control )sender) . Location . Y + e . Y - downPoint . Y);

}