Solved

C# - Modifying Cursor.Clip function

Posted on 2007-04-10
3
532 Views
Last Modified: 2008-01-09
Hi guys 'n gals,

I have a function which basically restricts Mouse movement to stay within a panel while dragging a PictureBox. I now want to modify this so that, rather than stopping the cursor from leaving the boundaries, I want to stop the PictureBox from leaving the boundaries (I don't want a PictureBox dropped half in and half out the panel).


Here is the existing code:

        void pb_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                pbDown = true;
                if (selectedPB != null)
                {
                    selectedPB.BorderStyle = BorderStyle.None;
                    selectedPB.Location = new Point(selectedPB.Location.X + 1, selectedPB.Location.Y + 1);
                    selectedPB = null;
                }
                //selectedPB = (TransparentPictureBox)sender;
                selectedPB = (PictureBox)sender;
                selectedPB.BorderStyle = BorderStyle.FixedSingle;
                selectedPB.BringToFront();
                Rectangle pnl = this.RectangleToScreen(new Rectangle(panel1.Location, panel1.Size));
                pnl.X = pnl.X + 1;
                pnl.Width = pnl.Width - 2;
                pnl.Y = pnl.Y + 1;
                pnl.Height = pnl.Height - 2;
                Cursor.Clip = pnl;
                startX = e.X;
                startY = e.Y;
            }
        }




Cheers!!
0
Comment
Question by:Cyber-Drugs
  • 2
3 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 18886120
Hi Cyber-Drugs,

In the pb_MouseMove() event, calculate what the new position of the PB will be and adjust it if necessary...

Quick Rules:

    If pb.X < 0 Then pb.X = 0
    If pb.Y < 0 Then pb.Y = 0
    If pb.X + pb.Width > panel1.Width Then pb.X = panel1.Width - pb.Width
    If pb.Y + pb.Height > panel1.Height Then pb.Y = panel1.Height - pb.Height

Fleshed out:  (I think...my C# syntax sucks)

        void pb_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                PictureBox localPB = (PictureBox)sender;
                if (localPB == selectedPB)
                {
                    Point pt = new Point(localPB.Location.X + (e.X - startX), localPB.Location.Y + (e.Y - startY));
                    if (pt.X < 0) { pt.X = 0; }
                    if (pt.Y < 0) { pt.Y = 0; }
                    if (pt.X + localPB.Width > panel1.Width) {
                        pt.X = panel1.Width - pb.Width;
                    }
                    if (pt.Y + localPB.Height > panel1.Height) {
                        pt.Y = panel1.Height - pb.Height;
                    }
                    localPB.Location = pt;
                }
            }
        }
0
 
LVL 4

Author Comment

by:Cyber-Drugs
ID: 18886188
Hi Idle_Mind,

Thanks for the heads up on that, still getting to grips with understanding mouse restriction, the code you posted 50% works...

Top and Left work fine

Right and Bottom throw an error like this:

Object reference not set to an instance of an object.

Referencing these lines:

pt.X = panel1.Width - pb.Width;
and
pt.Y = panel1.Height - pb.Height;


any thoughts?
0
 
LVL 4

Author Comment

by:Cyber-Drugs
ID: 18886237
changed "pb" for "localPB" and works fine, cheers!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now