Advertisement
armega

armega @ #vs.net on undernet

Feb 19th, 2012
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.33 KB | None | 0 0
  1.     #region ViewJob
  2.     public class ViewJob
  3.     {
  4.         private Rectangle m_rectRubberBand;
  5.         private Point m_pntPointOne;
  6.         private Point m_pntPointTwo;
  7.         private Pen m_penRubberBandPen;
  8.         private Rectangle m_rectOldRubberBand;
  9.  
  10.         //Constructor
  11.         public KofaxViewJob()
  12.         {
  13.             //Create the default pen
  14.             m_penRubberBandPen = new Pen(Color.Red, 1);
  15.             m_penRubberBandPen.DashCap = System.Drawing.Drawing2D.DashCap.Round;
  16.             m_penRubberBandPen.DashPattern = new float[] { 2f, 2f, 2f, 2f };
  17.         }
  18.  
  19.         //RubberBand
  20.         public Rectangle RubberBand
  21.         {
  22.             get
  23.             {
  24.                 return m_rectRubberBand;
  25.             }
  26.             set
  27.             {
  28.                 m_rectRubberBand = value;
  29.             }
  30.         }
  31.  
  32.         //PointOne
  33.         public Point PointOne
  34.         {
  35.             get
  36.             {
  37.                 return m_pntPointOne;
  38.             }
  39.             set
  40.             {
  41.                 m_pntPointOne = value;
  42.             }
  43.         }
  44.  
  45.         //PointTwo
  46.         public Point PointTwo
  47.         {
  48.             get
  49.             {
  50.                 return m_pntPointTwo;
  51.             }
  52.             set
  53.             {
  54.                 m_pntPointTwo = value;
  55.             }
  56.         }
  57.  
  58.         //RubberBandPen
  59.         public Pen RubberBandPen
  60.         {
  61.             get
  62.             {
  63.                 return m_penRubberBandPen;
  64.             }
  65.             set
  66.             {
  67.                 m_penRubberBandPen = value;
  68.             }
  69.         }
  70.  
  71.         //OldRubberBand
  72.         public Rectangle OldRubberBand
  73.         {
  74.             get
  75.             {
  76.                 return m_rectOldRubberBand;
  77.             }
  78.             set
  79.             {
  80.                 m_rectOldRubberBand = value;
  81.             }
  82.         }
  83.     }
  84.     #endregion
  85.  
  86.  
  87. public partial class MainGUI : Form
  88. {
  89.     private ViewJob kViewJob = new ViewJob();
  90.  
  91.         private void pbDisplay_MouseDown(object sender, MouseEventArgs e)
  92.         {
  93.             if (e.Button == System.Windows.Forms.MouseButtons.Right)
  94.             {
  95.                 if (kViewJob.RubberBand.IsEmpty)
  96.                 {
  97.                     kViewJob.PointOne = new Point(e.X, e.Y);
  98.                 }
  99.             }
  100.         }
  101.  
  102.         private void pbDisplay_MouseMove(object sender, MouseEventArgs e)
  103.         {
  104.             if (!kViewJob.PointOne.IsEmpty && kViewJob.PointTwo.IsEmpty)
  105.             {
  106.                 Rectangle rectValidArea = new Rectangle(0, 0, pbDisplay.ClientSize.Width, pbDisplay.ClientSize.Height);
  107.                 Point pntMouse = new Point(e.X, e.Y);
  108.                 if (rectValidArea.Contains(pntMouse))
  109.                 {
  110.                     Point pntTemp1 = new Point(Math.Min(kViewJob.PointOne.X, e.X), Math.Min(kViewJob.PointOne.Y, e.Y));
  111.                     Point pntTemp2 = new Point(Math.Max(kViewJob.PointOne.X, e.X), Math.Max(kViewJob.PointOne.Y, e.Y));
  112.  
  113.                     kViewJob.RubberBand = new Rectangle(pntTemp1.X, pntTemp1.Y, pntTemp2.X - pntTemp1.X, pntTemp2.Y - pntTemp1.Y);
  114.                 }
  115.                 else
  116.                 {
  117.                     Point pntInBounds = pntMouse;
  118.                     pntInBounds = new Point(Math.Max(pntInBounds.X, 0), Math.Max(pntInBounds.Y, 0));
  119.                     pntInBounds = new Point(Math.Min(pntInBounds.X, rectValidArea.Width), Math.Min(pntInBounds.Y, rectValidArea.Height));
  120.                     Point pntTemp1 = new Point(Math.Min(kViewJob.PointOne.X, pntInBounds.X), Math.Min(kViewJob.PointOne.Y, pntInBounds.Y));
  121.                     Point pntTemp2 = new Point(Math.Max(kViewJob.PointOne.X, pntInBounds.X), Math.Max(kViewJob.PointOne.Y, pntInBounds.Y));
  122.  
  123.                     kViewJob.RubberBand = new Rectangle(pntTemp1.X, pntTemp1.Y, pntTemp2.X - pntTemp1.X - 1, pntTemp2.Y - pntTemp1.Y - 1);
  124.                 }
  125.  
  126.                 //Show the rubberband on the picturebox
  127.                 pbDisplay.Invalidate();
  128.             }
  129.         }
  130.  
  131.         private void pbDisplay_MouseUp(object sender, MouseEventArgs e)
  132.         {
  133.             if (!kViewJob.PointOne.IsEmpty && kViewJob.PointTwo.IsEmpty)
  134.             {
  135.                 kViewJob.PointTwo = new Point(e.X, e.Y);
  136.  
  137.                 //Get the drawing area of the rubberband
  138.                 Rectangle rectAreaToClean = new Rectangle(
  139.                     kViewJob.RubberBand.X - (int)(kViewJob.RubberBandPen.Width / 2),
  140.                     kViewJob.RubberBand.Y - (int)(kViewJob.RubberBandPen.Width / 2),
  141.                     kViewJob.RubberBand.Width + (int)kViewJob.RubberBandPen.Width,
  142.                     kViewJob.RubberBand.Height + (int)kViewJob.RubberBandPen.Width);
  143.  
  144.                 //Save the rubberband then clear it
  145.                 kViewJob.OldRubberBand = kViewJob.RubberBand;
  146.                 kViewJob.PointOne = default(Point);
  147.                 kViewJob.PointTwo = default(Point);
  148.                 kViewJob.RubberBand = default(Rectangle);
  149.  
  150.                 //Erase the rubberband from the picturebox
  151.                 pbDisplay.Invalidate(rectAreaToClean);
  152.             }
  153.         }
  154.  
  155.         private void pbDisplay_Paint(object sender, PaintEventArgs e)
  156.         {
  157.             if (!kViewJob.RubberBand.IsEmpty)
  158.             {
  159.                 e.Graphics.DrawRectangle(kViewJob.RubberBandPen, kViewJob.RubberBand);
  160.             }
  161.         }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement