Solved

C# - Modifying Cursor.Clip function

Posted on 2007-04-10
3
542 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 86

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

726 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