Solved

C# - Modifying Cursor.Clip function

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

708 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

19 Experts available now in Live!

Get 1:1 Help Now