⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 paint.c

📁 WINDOWS图形编程随书光盘
💻 C
📖 第 1 页 / 共 3 页
字号:
663 {
664    HBRUSH hBrush;
665 
666    if (nHatchStyle == -1)
667      hBrush = CreateSolidBrush(crColor);
668    else
669      hBrush = CreateHatchBrush(nHatchStyle, crColor);
670 
671    return (hBrush);
672 }
673 
674 
675 
676 void DrawGraph(HDC hDC, HMENU hMenu, BOOL bSure)
677 {
678    HPEN   hPen, hPrePen;
679    HBRUSH hBrush, hPreBrush;
680 
681    if (ToolID==IDM_PENCIL || bSure)
682      {
683        hPen   = CreatePen(nPenStyle, nPenWidth,
684                         MKCOLOR(crPCurColor[nPenColor]));
685        hPrePen   = SelectObject(hDC, hPen);
686 
687        if (ToolID==IDM_RECT_F   || ToolID==IDM_ELLIP_F ||
688            ToolID==IDM_CIRCLE_F || ToolID==IDM_ROUNDRECT_F)
689          {
690            hBrush = MyCreateBrush(nHatch,
691                         MKCOLOR(crBCurColor[nBrushColor]));
692            hPreBrush  = SelectObject(hDC, hBrush);
693          }
694        else
695          {
696            hBrush = GetStockObject(NULL_BRUSH);
697            hPreBrush  = SelectObject(hDC, hBrush);
698          }
699      }
700    else
701      SelectObject(hDC, GetStockObject(NULL_BRUSH));
702 
703    switch (ToolID)
704      {
705        case IDM_PENCIL :
706               DrawPencil(hDC, hMenu);
707               break;
708 
709        case IDM_LINE :
710               DrawLine(hDC, hMenu, bSure);
711               break;
712 
713        case IDM_BLOCK :
714               BoundBlock(hDC, hMenu, bSure);
715               break;
716 
717        case IDM_ERASE :
718               Erase(hDC, hMenu, bSure);
719               break;
720 
721        case IDM_RECT_F :
722        case IDM_RECT   :
723               DrawRect(hDC, hMenu, bSure);
724               break;
725 
726        case IDM_ELLIP_F :
727        case IDM_ELLIP   :
728               DrawEllip(hDC, hMenu, bSure);
729               break;
730 
731        case IDM_CIRCLE_F :
732        case IDM_CIRCLE   :
733               DrawCircle(hDC, hMenu, bSure);
734               break;
735 
736        case IDM_ROUNDRECT_F :
737        case IDM_ROUNDRECT   :
738               DrawRoundRect(hDC, hMenu, bSure);
739               break;
740      }
741 
742    if (ToolID==IDM_PENCIL || bSure)
743      {
744        SelectObject(hDC, hPrePen);
745        DeleteObject(hPen);
746 
747        if (ToolID==IDM_RECT_F   || ToolID==IDM_ELLIP_F ||
748            ToolID==IDM_CIRCLE_F || ToolID==IDM_ROUNDRECT_F)
749          {
750            SelectObject(hDC, hPreBrush);
751            DeleteObject(hBrush);
752          }
753        else
754          {
755            SelectObject(hDC, hPreBrush);
756          }
757      }
758 }
759 
760 
761 
762 void DrawPencil(HDC hDC, HMENU hMenu)
763 {
764    MoveTo(hDC, PrePoint.x, PrePoint.y);
765    LineTo(hDC, CurPoint.x, CurPoint.y);
766 
767    if (! CanUndo)
768      {
769        EnableMenuItem(hMenu, IDM_UNDO, MF_ENABLED);
770        CanUndo = TRUE;
771      }
772 }
773 
774 
775 
776 void DrawLine(HDC hDC, HMENU hMenu, BOOL bSure)
777 {
778    int  nDrawMode;
779 
780    if (! bSure)
781      {
782        nDrawMode = SetROP2(hDC, R2_NOT);
783 
784        MoveTo(hDC, OrgPoint.x, OrgPoint.y);
785        LineTo(hDC, PrePoint.x, PrePoint.y);
786 
787        MoveTo(hDC, OrgPoint.x, OrgPoint.y);
788        LineTo(hDC, CurPoint.x, CurPoint.y);
789 
790        SetROP2(hDC, nDrawMode);
791      }
792    else
793      {
794        MoveTo(hDC, OrgPoint.x, OrgPoint.y);
795        LineTo(hDC, CurPoint.x, CurPoint.y);
796 
797        if (! CanUndo)
798          {
799            EnableMenuItem(hMenu, IDM_UNDO, MF_ENABLED);
800            CanUndo = TRUE;
801          }
802      }
803 }
804 
805 
806 
807 void Erase(HDC hDC, HMENU hMenu, BOOL bSure)
808 {
809    int  nDrawMode;
810 
811    if (! bSure)
812      {
813        nDrawMode = SetROP2(hDC, R2_NOT);
814 
815        Rectangle(hDC, OrgPoint.x, OrgPoint.y,
816                       PrePoint.x, PrePoint.y);
817 
818        Rectangle(hDC, OrgPoint.x, OrgPoint.y,
819                       CurPoint.x, CurPoint.y);
820 
821        SetROP2(hDC, nDrawMode);
822      }
823    else
824      {
825        PatBlt(hDC, OrgPoint.x, OrgPoint.y,
826                    CurPoint.x-OrgPoint.x,
827                    CurPoint.y-OrgPoint.y,
828                    WHITENESS);
829 
830        if (! CanUndo)
831          {
832            EnableMenuItem(hMenu, IDM_UNDO, MF_ENABLED);
833            CanUndo = TRUE;
834          }
835      }
836 }
837 
838 
839 
840 void DrawRect(HDC hDC, HMENU hMenu, BOOL bSure)
841 {
842    int  nDrawMode;
843 
844    if (! bSure)
845      {
846        nDrawMode = SetROP2(hDC, R2_NOT);
847 
848        Rectangle(hDC, OrgPoint.x, OrgPoint.y,
849                       PrePoint.x, PrePoint.y);
850 
851        Rectangle(hDC, OrgPoint.x, OrgPoint.y,
852                       CurPoint.x, CurPoint.y);
853 
854        SetROP2(hDC, nDrawMode);
855      }
856    else
857      {
858        Rectangle(hDC, OrgPoint.x, OrgPoint.y,
859                       CurPoint.x, CurPoint.y);
860 
861        if (! CanUndo)
862          {
863            EnableMenuItem(hMenu, IDM_UNDO, MF_ENABLED);
864            CanUndo = TRUE;
865          }
866      }
867 }
868 
869 
870 
871 void DrawEllip(HDC hDC, HMENU hMenu, BOOL bSure)
872 {
873    int  nDrawMode;
874 
875    if (! bSure)
876      {
877        nDrawMode = SetROP2(hDC, R2_NOT);
878 
879        Ellipse(hDC, OrgPoint.x, OrgPoint.y,
880                     PrePoint.x, PrePoint.y);
881 
882        Ellipse(hDC, OrgPoint.x, OrgPoint.y,
883                     CurPoint.x, CurPoint.y);
884 
885        SetROP2(hDC, nDrawMode);
886      }
887    else
888      {
889        Ellipse(hDC, OrgPoint.x, OrgPoint.y,
890                     CurPoint.x, CurPoint.y);
891 
892        if (! CanUndo)
893          {
894            EnableMenuItem(hMenu, IDM_UNDO, MF_ENABLED);
895            CanUndo = TRUE;
896          }
897      }
898 }
899 
900 
901 
902 void DrawRoundRect(HDC hDC, HMENU hMenu, BOOL bSure)
903 {
904    int  nDrawMode;
905 
906    if (! bSure)
907      {
908        nDrawMode = SetROP2(hDC, R2_NOT);
909 
910        RoundRect(hDC, OrgPoint.x, OrgPoint.y,
911                       PrePoint.x, PrePoint.y,
912                       (PrePoint.x-OrgPoint.x)/4,
913                       (PrePoint.y-OrgPoint.y)/4);
914 
915        RoundRect(hDC, OrgPoint.x, OrgPoint.y,
916                       CurPoint.x, CurPoint.y,
917                       (CurPoint.x-OrgPoint.x)/4,
918                       (CurPoint.y-OrgPoint.y)/4);
919 
920        SetROP2(hDC, nDrawMode);
921      }
922    else
923      {
924        RoundRect(hDC, OrgPoint.x, OrgPoint.y,
925                       CurPoint.x, CurPoint.y,
926                       (CurPoint.x-OrgPoint.x)/4,
927                       (CurPoint.y-OrgPoint.y)/4);
928 
929        if (! CanUndo)
930          {
931            EnableMenuItem(hMenu, IDM_UNDO, MF_ENABLED);
932            CanUndo = TRUE;
933          }
934      }
935 }
936 
937 
938 
939 void DrawCircle(HDC hDC, HMENU hMenu, BOOL bSure)
940 {
941    int    nDrawMode;
942    int    nLogPixSx, nLogPixSy;
943    int    Width, Height;
944    int    SignX, SignY;
945 
946    nLogPixSx = GetDeviceCaps(hDC, LOGPIXELSX);
947    nLogPixSy = GetDeviceCaps(hDC, LOGPIXELSY);
948 
949    Width  = CurPoint.x - OrgPoint.x;
950    Height = CurPoint.y - OrgPoint.y;
951    SignX  = (Width  >= 0 ? 1 : -1);
952    SignY  = (Height >= 0 ? 1 : -1);
953 
954    if (fabs((float) Width/nLogPixSx) >
955        fabs((float) Height/nLogPixSy) )
956       {
957          CurPoint.x = OrgPoint.x + (float)
958            fabs(Height) * nLogPixSx / nLogPixSy * SignX;
959       }
960    else
961       {
962          CurPoint.y = OrgPoint.y + (float)
963            fabs(Width) * nLogPixSy / nLogPixSx * SignY;
964       }
965 
966 
967    if (! bSure)
968      {
969        nDrawMode = SetROP2(hDC, R2_NOT);
970 
971        Ellipse(hDC, OrgPoint.x, OrgPoint.y,
972                     PrePoint.x, PrePoint.y);
973 
974        Ellipse(hDC, OrgPoint.x, OrgPoint.y,
975                     CurPoint.x, CurPoint.y);
976 
977        SetROP2(hDC, nDrawMode);
978      }
979    else
980      {
981        Ellipse(hDC, OrgPoint.x, OrgPoint.y,
982                     CurPoint.x, CurPoint.y);
983 
984        if (! CanUndo)
985          {
986            EnableMenuItem(hMenu, IDM_UNDO, MF_ENABLED);
987            CanUndo = TRUE;
988          }
989      }
990 }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -