程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> 用C# 實現截圖功能(2)(類似QQ截圖)

用C# 實現截圖功能(2)(類似QQ截圖)

編輯:關於C#

503    /// Gets or sets the width of the 8 little rectangles
504    /// (rectangles that on the 4 corners and the 4 middle points that of the 4 lines of the main rectangle)
505    /// </summary>
506    public int LittleRectangleWidth
507    {
508      get { return littleRectangleWidth; }
509      set { littleRectangleWidth = value; }
510    }
511    /**//// <summary>
512    /// Gets or sets the height of the 8 little rectangles
513    /// (rectangles that on the 4 corners and the 4 middle points that of the 4 lines of the main rectangle)
514    /// </summary>
515    public int LittleRectangleHeight
516    {
517      get { return littleRectangleHeight; }
518      set { littleRectangleHeight = value; }
519    }
520
521    /**//// <summary>
522    /// Gets or sets he little rectangle on the upper-left corner of the main rectangle
523    /// </summary>
524    public Rectangle LeftTopRectangle
525    {
526      get { return leftTopRectangle; }
527      set { leftTopRectangle = value; }
528    }
529    /**//// <summary>
530    /// Gets or sets he little rectangle on the middle point of the left line of the main rectangle
531    /// </summary>
532    public Rectangle LeftMiddleRectangle
533    {
534      get { return leftMiddleRectangle; }
535      set { leftMiddleRectangle = value; }
536    }
537    /**//// <summary>
538    /// Gets or sets he little rectangle on the bottom-left corner of the main rectangle
539    /// </summary>
540    public Rectangle LeftBottomRectangle
541    {
542      get { return leftBottomRectangle; }
543      set { leftBottomRectangle = value; }
544    }
545
546    /**//// <summary>
547    /// Gets or sets he little rectangle on the upper-right corner of the main rectangle
548    /// </summary>
549    public Rectangle RightTopRectangle
550    {
551      get { return rightTopRectangle; }
552      set { rightTopRectangle = value; }
553    }
554    /**//// <summary>
555    /// Gets or sets he little rectangle on the middle point of the right line of the main rectangle
556    /// </summary>
557    public Rectangle RightMiddleRectangle
558    {
559      get { return rightMiddleRectangle; }
560      set { rightMiddleRectangle = value; }
561    }
562    /**//// <summary>
563    /// Gets or sets he little rectangle on the bottom-right corner of the main rectangle
564    /// </summary>
565    public Rectangle RightBottomRectangle
566    {
567      get { return rightBottomRectangle; }
568      set { rightBottomRectangle = value; }
569    }
570
571    /**//// <summary>
572    /// Gets or sets he little rectangle on the middle point of the top line of the main rectangle
573    /// </summary>
574    public Rectangle TopMiddleRectangle
575    {
576      get { return topMiddleRectangle; }
577      set { topMiddleRectangle = value; }
578    }
579    /**//// <summary>
580    /// Gets or sets he little rectangle on the middle point of the bottom line of the main rectangle
581    /// </summary>
582    public Rectangle BottomMiddleRectangle
583    {
584      get { return bottomMiddleRectangle; }
585      set { bottomMiddleRectangle = value; }
586    }
587
588    /**//// <summary>
589    /// Gets or sets the main rectangle
590    /// </summary>
591    public Rectangle Rect
592    {
593      get { return rect; }
594      set
595      {
596        rect = value;
597        x = value.X;
598        y = value.Y;
599        width = value.Width;
600        height = value.Height;
601      }
602    }
603
604    /**//// <summary>
605    /// Gets the size of the main rectangle
606    /// </summary>
607    public Size Size
608    {
609      get { return rect.Size; }
610    }
611
612    /**//// <summary>
613    /// Gets or sets the background image of the screen
614    /// </summary>
615    public Image BackImage
616    {
617      get { return backImage; }
618      set { backImage = value; }
619    }
620
621
622    /**//// <summary>
623    /// Gets or sets the manner of the cursor
624    /// </summary>
625    public Cursor MyCursor
626    {
627      get { return myCursor; }
628      set { myCursor = value; }
629    }
630
631
632    /**//// <summary>
633    /// Constructor function
634    /// </summary>
635    public MyRectangle()
636    {
637      Rect = new Rectangle();
638
639      setAllModeFalse();
640
641      LittleRectangleWidth = 4;
642      LittleRectangleHeight = 4;
643
644      MinHeight = 5;
645      MinWidth = 5;
646
647      LeftTopRectangle = new Rectangle();
648      LeftMiddleRectangle = new Rectangle();
649      LeftBottomRectangle = new Rectangle();
650
651      RightTopRectangle = new Rectangle();
652      RightMiddleRectangle = new Rectangle();
653      RightBottomRectangle = new Rectangle();
654
655      TopMiddleRectangle = new Rectangle();
656      BottomMiddleRectangle = new Rectangle();
657
658      MyCursor = new Cursor(@"..\..\Cursors\hcross.cur");
659
660    }
661
662    public void SetLittleRectangle()
663    {
664      int excursionX = LittleRectangleWidth / 2;
665      int excursionY = LittleRectangleHeight / 2;
666      LeftTopRectangle = new Rectangle(X - excursionX, Y - excursionY, LittleRectangleWidth, LittleRectangleHeight);
667      leftMiddleRectangle = new Rectangle(X - excursionX, Y - excursionY + Height / 2, LittleRectangleWidth, LittleRectangleHeight);
668      leftBottomRectangle = new Rectangle(X - excursionX, Y - excursionY + Height, LittleRectangleWidth, LittleRectangleHeight);
669
670      rightTopRectangle = new Rectangle(X - excursionX + Width, Y - excursionY, LittleRectangleWidth, LittleRectangleHeight);
671      rightMiddleRectangle = new Rectangle(X - excursionX + Width, Y - excursionY + Height / 2, LittleRectangleWidth, LittleRectangleHeight);
672      rightBottomRectangle = new Rectangle(X - excursionX + Width, Y - excursionY + Height, LittleRectangleWidth, LittleRectangleHeight);
673
674      topMiddleRectangle = new Rectangle(X - excursionX + Width / 2, Y - excursionY, LittleRectangleWidth, LittleRectangleHeight);
675      bottomMiddleRectangle = new Rectangle(X - excursionX + Width / 2, Y - excursionY + Height, LittleRectangleWidth, LittleRectangleHeight);
676    }
677
678    /**//// <summary>
679    /// draw rectangle function
680    /// </summary>
681    /// <param name="e">mouse event </param>
682    /// <param name="backColor">back color</param>
683    public void Draw(MouseEventArgs e, Color backColor)
684    {
685      Draw(backColor);
686      if (e.X < DownPointX)
687      {
688        Width = DownPointX - e.X;
689        X = e.X;
690      }
691      else
692      {
693        Width = e.X - DownPointX;
694      }
695      if (e.Y < DownPointY)
696      {
697        Height = DownPointY - e.Y;
698        Y = e.Y;
699      }
700      else
701      {
702        Height = e.Y - DownPointY;
703      }
704      Draw(backColor);
705    }
706    /**//// <summary>
707    /// draw rectangle function
708    /// </summary>
709    /// <param name="backColor">back color</param>
710    public void Draw(Color backColor)
711    {
712      //Initialize the 8 little rectangles
713      SetLittleRectangle();
714
715      //draw the main rectangle and the 8 little rectangles
716      ControlPaint.DrawReversibleFrame(rect, backColor, FrameStyle.Dashed);
717
718      ControlPaint.FillReversibleRectangle(leftTopRectangle, Color.White);
719      ControlPaint.FillReversibleRectangle(leftMiddleRectangle, Color.White);
720      ControlPaint.FillReversibleRectangle(leftBottomRectangle, Color.White);
721      ControlPaint.FillReversibleRectangle(rightTopRectangle, Color.White);
722      ControlPaint.FillReversibleRectangle(rightMiddleRectangle, Color.White);
723      ControlPaint.FillReversibleRectangle(rightBottomRectangle, Color.White);
724      ControlPaint.FillReversibleRectangle(topMiddleRectangle, Color.White);
725      ControlPaint.FillReversibleRectangle(bottomMiddleRectangle, Color.White);
726    }
727
728    /**//// <summary>
729    /// change size when the rectangle is on the change size mode
730    /// </summary>
731    /// <param name="e"></param>
732    public void ChangeSize(MouseEventArgs e)
733    {
734      //change size according the mouse location
735      if (ChangeSizeMode)
736      {
737
738        if (MouseOnLeftTop)
739        {
740          if ((Rect.Right - e.X < MinWidth) || (Rect.Bottom - e.Y < MinHeight))
741          {
742            return;
743          }
744          Width = Math.Abs(Rect.Right - e.X);
745          Height = Math.Abs(Rect.Bottom - e.Y);
746          X = e.X;
747          Y = e.Y;
748        }
749        else if (MouseOnLeftMiddle)
750        {
751          if (Rect.Right - e.X < MinWidth)
752          {
753            return;
754          }
755          else
756          {
757            Width = Math.Abs(Rect.Right - e.X);
758            X = e.X;
759          }
760        }
761        else if (MouseOnLeftBottom)
762        {
763          if (Rect.Right - e.X < MinWidth || e.Y - Rect.Top < MinHeight)
764          {
765            return;
766          }
767          Width = Math.Abs(Rect.Right - e.X);
768          Height = Math.Abs(e.Y - Rect.Top);
769          X = e.X;
770        }
771        else if (MouseOnRightTop)
772        {
773          if (e.X - Rect.Left < MinWidth || Rect.Bottom - e.Y < MinHeight)
774          {
775            return;
776          }
777          Width = Math.Abs(e.X - X);
778          Height = Math.Abs(Rect.Bottom - e.Y);
779          Y = e.Y;
780        }
781        else if (MouseOnRightMiddle)
782        {
783          if (e.X - Rect.Left < MinWidth)
784          {
785            return;
786          }
787          Width = Math.Abs(e.X - X);
788
789        }
790        else if (MouseOnRightBottom)
791        {
792          if (e.X - Rect.Left < MinWidth || e.Y - Rect.Top < MinHeight)
793          {
794            return;
795          }
796          Width = Math.Abs(e.X - X);
797          Height = Math.Abs(e.Y - Y);
798        }
799        else if (MouseOnTopMiddle)
800        {
801          if (Rect.Bottom - e.Y < MinHeight)
802          {
803            return;
804          }
805          Height = Math.Abs(Rect.Bottom - e.Y);
806          Y = e.Y;
807        }
808        else if (MouseOnBottomMiddle)
809        {
810          if (e.Y - Rect.Top < MinHeight)
811          {
812            return;
813          }
814          Height = Math.Abs(e.Y - Y);
815        }
816
817      }
818    }
819
820    /**//// <summary>
821    /// move the location of the rectangle
822    /// </summary>
823    /// <param name="newX">The x-coordinate of the new location</param>
824    /// <param name="newY">The y-coordinate of the new location</param>
825    public void Move(int newX, int newY)
826    {
827      X = newX;
828      Y = newY;
829    }
830
831    /**//// <summary>
832    /// check the current mouse location and set the cursor manner according the mouse location
/// </summary>
834    /// <param name="e">mouse</param>
835    public void CheckMouseLocation(MouseEventArgs e)
836    {
837      if (leftTopRectangle.Contains(e.X, e.Y))
838      {
839        if (!onChangingMode())
840        {
841          MouseOnLeftTop = true;
842          myCursor = new Cursor(@"..\..\Cursors\size2_m.cur");
843        }
844
845        if (e.Button == MouseButtons.Left)
846        {
847          if (!MoveMode)
848          {
849            ChangeSizeMode = true;
850          }
851        }
852        else
853        {
854          changeSizeMode = false;
855          moveMode = false;
856        }
857      }
858      else if (leftMiddleRectangle.Contains(e.X, e.Y))
859      {
860        if (!onChangingMode())
861        {
862          MouseOnLeftMiddle = true;
863          myCursor = new Cursor(@"..\..\Cursors\size3_m.cur");
864        }
865
866        if (e.Button == MouseButtons.Left)
867        {
868          if (!MoveMode)
869          {
870            ChangeSizeMode = true;
871          }
872        }
873        else
874        {
875          changeSizeMode = false;
876          moveMode = false;
877        }
878      }
879      else if (leftBottomRectangle.Contains(e.X, e.Y))
880      {
881         if (!onChangingMode())
882        {
883          MouseOnLeftBottom = true;
884          myCursor = new Cursor(@"..\..\Cursors\size1_m.cur");
885        }
886
887        if (e.Button == MouseButtons.Left)
888        {
889          if (!MoveMode)
890          {
891            ChangeSizeMode = true;
892          }
893        }
894        else
895        {
896          changeSizeMode = false;
897          moveMode = false;
898        }
899      }
900
901      else if (rightTopRectangle.Contains(e.X, e.Y))
902      {
903        mouseOnLeftBottom = false;
904
905        if (!onChangingMode())
906        {
907          MouseOnRightTop = true;
908          myCursor = new Cursor(@"..\..\Cursors\size1_m.cur");
909        }
910
911        mouseOnMiddle = false;
912
913        if (e.Button == MouseButtons.Left)
914        {
915          if (!MoveMode)
916          {
917            ChangeSizeMode = true;
918          }
919        }
920        else
921        {
922          changeSizeMode = false;
923          moveMode = false;
924        }
925      }
926      else if (rightMiddleRectangle.Contains(e.X, e.Y))
927      {
928        if (!onChangingMode())
929        {
930          MouseOnRightMiddle = true;
931          myCursor = new Cursor(@"..\..\Cursors\size3_m.cur");
932        }
933
934        if (e.Button == MouseButtons.Left)
935        {
936          if (!MoveMode)
937          {
938            ChangeSizeMode = true;
939          }
940        }
941        else
942        {
943          changeSizeMode = false;
944          moveMode = false;
945        }
946      }
947      else if (rightBottomRectangle.Contains(e.X, e.Y))
948      {
949        if (!onChangingMode())
950        {
951          MouseOnRightBottom = true;
952          myCursor = new Cursor(@"..\..\Cursors\size2_m.cur");
953        }
954
955        if (e.Button == MouseButtons.Left)
956        {
957          if (!MoveMode)
958          {
959            ChangeSizeMode = true;
960          }
961        }
962        else
963        {
964          changeSizeMode = false;
965          moveMode = false;
966        }
967      }
968      else if (topMiddleRectangle.Contains(e.X, e.Y))
969      {
970
971        if (!onChangingMode())
972        {
973          MouseOnTopMiddle = true;
974          myCursor = new Cursor(@"..\..\Cursors\size4_m.cur");
975        }
976
977
978        if (e.Button == MouseButtons.Left)
979        {
980          if (!MoveMode)
981          {
982            ChangeSizeMode = true;
983          }
984        }
985        else
986        {
987          changeSizeMode = false;
988          moveMode = false;
989        }
990      }
991      else if (bottomMiddleRectangle.Contains(e.X, e.Y))
992      {
993        if (!onChangingMode())
994        {
995          MouseOnBottomMiddle = true;
996          myCursor = new Cursor(@"..\..\Cursors\size4_m.cur");
997        }
998
999        if (e.Button == MouseButtons.Left)
1000        {
1001          if (!MoveMode)
1002          {
1003            ChangeSizeMode = true;
1004          }
1005        }
1006        else
1007        {
1008          changeSizeMode = false;
1009          moveMode = false;
1010        }
1011      }
1012      else if (rect.Contains(e.X, e.Y))
1013      {
1014
1015        if (!changeSizeMode)
1016        {
1017          MouseOnMiddle = true;
1018          myCursor = new Cursor(@"..\..\Cursors\move_m.cur");
1019        }
1020        if (e.Button == MouseButtons.Left)
1021        {
1022          if (!ChangeSizeMode)
1023          {
1024            MoveMode = true;
1025          }
1026        }
1027        else
1028        {
1029          moveMode = false;
1030          changeSizeMode = false;
1031        }
1032      }
1033      else
1034      {
1035        if (e.Button != MouseButtons.Left)
1036        {
1037          setAllModeFalse();
1038
1039          myCursor = new Cursor(@"..\..\Cursors\hcross.cur");
1040        }
1041      }
1042    }
1043    public bool Contains(int x, int y)
1044    {
1045      if (rect.Contains(x, y))
1046      {
1047        return true;
1048      }
1049      else return false;
1050    }
1051
1052    /**//// <summary>
1053    /// set all mode false
1054    /// (the sign of the mouse location and the change size mode ,move mode)
1055    /// </summary>
1056    public void setAllModeFalse()
1057    {
1058      mouseOnLeftTop = false;
1059      mouseOnLeftMiddle = false;
1060      mouseOnLeftBottom = false;
1061
1062      mouseOnRightTop = false;
1063      mouseOnRightMiddle = false;
1064      mouseOnRightBottom = false;
1065
1066      mouseOnTopMiddle = false;
1067      mouseOnBottomMiddle = false;
1068
1069      mouseOnMiddle = false;
1070      changeSizeMode = false;
1071      moveMode = false;
1072      myCursor = new Cursor(@"..\..\Cursors\hcross.cur");
1073    }
1074
1075    /**//// <summary>
1076    /// check whether the rectangle is on change mode now
1077    /// </summary>
1078    /// <returns></returns>
1079    public bool onChangingMode()
1080    {
1081      return ((MouseOnLeftTop || MouseOnLeftMiddle || mouseOnLeftBottom || mouseOnRightTop || mouseOnRightMiddle || MouseOnRightBottom || mouseOnTopMiddle || MouseOnBottomMiddle) && changeSizeMode);
1082    }
1083
1084    /**//// <summary>
1085    /// Initialize the rectangle
1086    /// </summary>
1087    /// <param name="x">The x-coordinate of the rectangle</param>
1088    /// <param name="y">The y-coordinate of the rectangle</param>
1089    /// <param name="width">The width of the rectangle</param>
1090    /// <param name="height">The height of the rectangle</param>
1091    public void Initialize(int x, int y, int width, int height)
1092    {
1093      X = x;
1094      Y = y;
1095      Width = width;
1096      Height = height;
1097      SetLittleRectangle();
1098    }
1099  }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved