943,633 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 437
  • C# RSS
Jul 23rd, 2009
0

Cursor.Clip

Expand Post »
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:

C# Syntax (Toggle Plain Text)
  1. if(e.Button == MouseButtons.Left){}

Any help would be appreciated.

Thanks
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nlblnx is offline Offline
10 posts
since Aug 2008
Jul 23rd, 2009
0

Re: Cursor.Clip

I'm off somewhere by a couple of pixels... but close enough:
c# Syntax (Toggle Plain Text)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace daniweb
  11. {
  12. public partial class frmPanel : Form
  13. {
  14. private Point start;
  15. private Rectangle origClip;
  16.  
  17. public frmPanel()
  18. {
  19. InitializeComponent();
  20. }
  21.  
  22. private void panel1_MouseDown(object sender, MouseEventArgs e)
  23. {
  24. if (e.Button == MouseButtons.Left)
  25. {
  26. origClip = Cursor.Clip;
  27. start = e.Location;
  28. panel1.MouseUp += new MouseEventHandler(panel1_MouseUp);
  29. panel1.MouseMove += new MouseEventHandler(panel1_MouseMove);
  30.  
  31. Point topLeft = this.PointToScreen(new Point(0 + e.X, 0 + e.Y));
  32. Point rightBottom = this.PointToScreen(new Point(this.Width, this.Height));
  33. Size sz = new Size(rightBottom.X - topLeft.X - e.X, rightBottom.Y - topLeft.Y - e.Y);
  34. Cursor.Clip = new Rectangle(topLeft, sz);
  35. }
  36. }
  37.  
  38. void panel1_MouseUp(object sender, MouseEventArgs e)
  39. {
  40. Cursor.Clip = origClip;
  41. panel1.MouseMove -= new MouseEventHandler(panel1_MouseMove);
  42. panel1.MouseUp -= new MouseEventHandler(panel1_MouseUp);
  43. }
  44.  
  45. void panel1_MouseMove(object sender, MouseEventArgs e)
  46. {
  47. panel1.Location = new Point(panel1.Location.X - (start.X - e.X), panel1.Location.Y - (start.Y - e.Y));
  48. }
  49. }
  50. }
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Program Won't Work With Vista
Next Thread in C# Forum Timeline: taskbar and keyboardhook help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC