Solved

C# - Modifying Cursor.Clip function

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
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
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

765 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