Hi all,

Does anyone have a good example of using Cursor.Clip?

I need to be able to restrict movement to a form only when the left mouse button is pressed, so using:

if(e.Button == MouseButtons.Left){}

Any help would be appreciated.


7 Years
Discussion Span
Last Post by sknake

I'm off somewhere by a couple of pixels... but close enough:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace daniweb
  public partial class frmPanel : Form
    private Point start;
    private Rectangle origClip;

    public frmPanel()

    private void panel1_MouseDown(object sender, MouseEventArgs e)
      if (e.Button == MouseButtons.Left)
        origClip = Cursor.Clip;
        start = e.Location;
        panel1.MouseUp += new MouseEventHandler(panel1_MouseUp);
        panel1.MouseMove += new MouseEventHandler(panel1_MouseMove);

        Point topLeft = this.PointToScreen(new Point(0 + e.X, 0 + e.Y));
        Point rightBottom = this.PointToScreen(new Point(this.Width, this.Height));
        Size sz = new Size(rightBottom.X - topLeft.X - e.X, rightBottom.Y - topLeft.Y - e.Y);
        Cursor.Clip = new Rectangle(topLeft, sz);

    void panel1_MouseUp(object sender, MouseEventArgs e)
      Cursor.Clip = origClip;
      panel1.MouseMove -= new MouseEventHandler(panel1_MouseMove);
      panel1.MouseUp -= new MouseEventHandler(panel1_MouseUp);

    void panel1_MouseMove(object sender, MouseEventArgs e)
      panel1.Location = new Point(panel1.Location.X - (start.X - e.X), panel1.Location.Y - (start.Y - e.Y));
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.