home *** CD-ROM | disk | FTP | other *** search
/ Mastering Visual Basic 6 / mastvb6.iso / ch_code / ch06 / draw / draw.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1998-02-20  |  12.9 KB  |  420 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
  3. Begin VB.Form DrawForm 
  4.    AutoRedraw      =   -1  'True
  5.    BackColor       =   &H00FFFFFF&
  6.    Caption         =   "Draw"
  7.    ClientHeight    =   4200
  8.    ClientLeft      =   165
  9.    ClientTop       =   735
  10.    ClientWidth     =   6705
  11.    BeginProperty Font 
  12.       Name            =   "MS Sans Serif"
  13.       Size            =   12
  14.       Charset         =   0
  15.       Weight          =   400
  16.       Underline       =   0   'False
  17.       Italic          =   0   'False
  18.       Strikethrough   =   0   'False
  19.    EndProperty
  20.    ForeColor       =   &H000000FF&
  21.    LinkTopic       =   "Form1"
  22.    ScaleHeight     =   4200
  23.    ScaleWidth      =   6705
  24.    StartUpPosition =   3  'Windows Default
  25.    Begin VB.PictureBox Picture1 
  26.       AutoRedraw      =   -1  'True
  27.       BeginProperty Font 
  28.          Name            =   "MS Sans Serif"
  29.          Size            =   8.25
  30.          Charset         =   0
  31.          Weight          =   400
  32.          Underline       =   0   'False
  33.          Italic          =   0   'False
  34.          Strikethrough   =   0   'False
  35.       EndProperty
  36.       Height          =   2535
  37.       Left            =   4320
  38.       ScaleHeight     =   2475
  39.       ScaleWidth      =   1515
  40.       TabIndex        =   0
  41.       Top             =   120
  42.       Visible         =   0   'False
  43.       Width           =   1575
  44.    End
  45.    Begin MSComDlg.CommonDialog CommonDialog1 
  46.       Left            =   315
  47.       Top             =   165
  48.       _ExtentX        =   847
  49.       _ExtentY        =   847
  50.       _Version        =   393216
  51.       FontSize        =   2.54052e-29
  52.    End
  53.    Begin VB.Label Label1 
  54.       Appearance      =   0  'Flat
  55.       AutoSize        =   -1  'True
  56.       BackColor       =   &H80000005&
  57.       BackStyle       =   0  'Transparent
  58.       ForeColor       =   &H80000008&
  59.       Height          =   300
  60.       Left            =   3600
  61.       TabIndex        =   1
  62.       Top             =   240
  63.       Visible         =   0   'False
  64.       Width           =   60
  65.    End
  66.    Begin VB.Menu FileMenu 
  67.       Caption         =   "File"
  68.       Begin VB.Menu FileNew 
  69.          Caption         =   "New"
  70.       End
  71.       Begin VB.Menu FileOpen 
  72.          Caption         =   "Open"
  73.       End
  74.       Begin VB.Menu FileSave 
  75.          Caption         =   "Save"
  76.       End
  77.       Begin VB.Menu FileSaveAs 
  78.          Caption         =   "Save As"
  79.       End
  80.       Begin VB.Menu FileExit 
  81.          Caption         =   "Exit"
  82.       End
  83.    End
  84.    Begin VB.Menu EditMenu 
  85.       Caption         =   "Edit"
  86.       Begin VB.Menu EditCopy 
  87.          Caption         =   "Copy"
  88.       End
  89.       Begin VB.Menu EditCut 
  90.          Caption         =   "Cut"
  91.       End
  92.       Begin VB.Menu EditPaste 
  93.          Caption         =   "Paste"
  94.       End
  95.       Begin VB.Menu EditClear 
  96.          Caption         =   "Clear"
  97.       End
  98.    End
  99.    Begin VB.Menu ShapeMenu 
  100.       Caption         =   "Shape"
  101.       Begin VB.Menu DrawLine 
  102.          Caption         =   "Line"
  103.       End
  104.       Begin VB.Menu DrawCircle 
  105.          Caption         =   "Circle"
  106.       End
  107.       Begin VB.Menu DrawBox 
  108.          Caption         =   "Box"
  109.       End
  110.       Begin VB.Menu DrawText 
  111.          Caption         =   "Text"
  112.       End
  113.    End
  114.    Begin VB.Menu WidthMenu 
  115.       Caption         =   "Width"
  116.       Begin VB.Menu width1 
  117.          Caption         =   "1 pixel"
  118.       End
  119.       Begin VB.Menu Width2 
  120.          Caption         =   "2 pixels"
  121.       End
  122.       Begin VB.Menu Width3 
  123.          Caption         =   "3 pixels"
  124.       End
  125.    End
  126.    Begin VB.Menu StyleMenu 
  127.       Caption         =   "DrawStyle"
  128.       Begin VB.Menu StyleSolid 
  129.          Caption         =   "Solid"
  130.       End
  131.       Begin VB.Menu StyleDash 
  132.          Caption         =   "Dash"
  133.       End
  134.       Begin VB.Menu StyleDot 
  135.          Caption         =   "Dot"
  136.       End
  137.       Begin VB.Menu menuSeparator 
  138.          Caption         =   "-"
  139.       End
  140.       Begin VB.Menu StyleFilled 
  141.          Caption         =   "Solid Shape"
  142.       End
  143.    End
  144.    Begin VB.Menu ColorMenu 
  145.       Caption         =   "Colors"
  146.       Begin VB.Menu ColorPage 
  147.          Caption         =   "Page Color"
  148.       End
  149.       Begin VB.Menu ColorPen 
  150.          Caption         =   "Pen Color"
  151.       End
  152.       Begin VB.Menu ColorFill 
  153.          Caption         =   "Fill Color"
  154.       End
  155.    End
  156. Attribute VB_Name = "DrawForm"
  157. Attribute VB_GlobalNameSpace = False
  158. Attribute VB_Creatable = False
  159. Attribute VB_PredeclaredId = True
  160. Attribute VB_Exposed = False
  161. Option Explicit
  162. Dim Shape As String
  163. Dim XStart, YStart As Single
  164. Dim XPrevious, YPrevious As Single
  165. Dim CopyBMP, PasteBMP, CutBMP, PrintText As Integer
  166. Dim PDrawWidth, PDrawStyle, PFillStyle As Integer
  167. Dim CopyWidth, CopyHeight As Integer
  168. Dim XLabel, YLabel As Integer
  169. Dim OpenFile As String
  170. Private Sub UnCheckStyles()
  171.     StyleSolid.Checked = False
  172.     StyleDash.Checked = False
  173.     StyleDot.Checked = False
  174. End Sub
  175. Private Sub ColorFill_Click()
  176.     CommonDialog1.Color = DrawForm.FillColor
  177.     CommonDialog1.Flags = cdlCCRGBInit
  178.     CommonDialog1.ShowColor
  179.     DrawForm.FillColor = CommonDialog1.Color
  180. End Sub
  181. Private Sub ColorPage_Click()
  182.     CommonDialog1.Color = DrawForm.BackColor
  183.     CommonDialog1.Flags = cdlCCRGBInit
  184.     CommonDialog1.ShowColor
  185.     DrawForm.BackColor = CommonDialog1.Color
  186. End Sub
  187. Private Sub ColorPen_Click()
  188.     CommonDialog1.Color = DrawForm.ForeColor
  189.     CommonDialog1.Flags = cdlCCRGBInit
  190.     CommonDialog1.ShowColor
  191.     DrawForm.ForeColor = CommonDialog1.Color
  192. End Sub
  193. Private Sub DrawBox_Click()
  194.     Shape = "BOX"
  195. End Sub
  196. Private Sub DrawCircle_Click()
  197.     Shape = "CIRCLE"
  198. End Sub
  199. Private Sub DrawLine_Click()
  200.     Shape = "LINE"
  201. End Sub
  202. Private Sub DrawText_Click()
  203. Dim DrawString As String
  204.     DrawString = InputBox("Enter string")
  205.     Label1.Caption = DrawString
  206.     PrintText = True
  207. End Sub
  208. Private Sub EditClear_Click()
  209.     DrawForm.Cls
  210. End Sub
  211. Private Sub EditCopy_Click()
  212.     CopyBMP = True
  213. End Sub
  214. Private Sub EditCut_Click()
  215.     CutBMP = True
  216. End Sub
  217. Private Sub EditPaste_Click()
  218.     PasteBMP = True
  219. End Sub
  220. Private Sub FileExit_Click()
  221.     End
  222. End Sub
  223. Private Sub FileNew_Click()
  224.     DrawForm.Picture = LoadPicture()
  225.     OpenFile = ""
  226. End Sub
  227. Private Sub FileOpen_Click()
  228.     CommonDialog1.Filter = "Images|*.bmp;*.gif;*.jpg"
  229.     CommonDialog1.DefaultExt = "BMP"
  230.     CommonDialog1.ShowOpen
  231.     If CommonDialog1.FileName = "" Then Exit Sub
  232.     DrawForm.Picture = LoadPicture(CommonDialog1.FileName)
  233.     OpenFile = CommonDialog1.FileName
  234.     Shape = ""
  235. End Sub
  236. Private Sub FileSave_Click()
  237.     If OpenFile <> "" Then
  238.         SavePicture Image, OpenFile
  239.     End If
  240. End Sub
  241. Private Sub FileSaveAs_Click()
  242.     CommonDialog1.Filter = "Images|*.bmp"
  243.     CommonDialog1.DefaultExt = "BMP"
  244.     CommonDialog1.ShowSave
  245.     If CommonDialog1.FileName = "" Then Exit Sub
  246.     SavePicture DrawForm.Image, CommonDialog1.FileName
  247.     OpenFile = CommonDialog1.FileName
  248. End Sub
  249. Private Sub Form_Load()
  250.     CopyBMP = False
  251.     PasteBMP = False
  252.     PrintText = False
  253.     XPrevious = -9999
  254.     YPrevious = -9999
  255. End Sub
  256. Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  257.     If Button = 2 Then Shape = ""
  258.     If Button = 1 Then
  259.         XStart = X
  260.         YStart = Y
  261.         XPrevious = XStart
  262.         YPrevious = YStart
  263.         DrawForm.DrawMode = 7
  264.     End If
  265.     If CopyBMP Or CutBMP Then
  266.         PDrawWidth = DrawForm.DrawWidth
  267.         PDrawStyle = DrawForm.DrawStyle
  268.         PFillStyle = DrawForm.FillStyle
  269.         DrawForm.DrawWidth = 1
  270.         DrawForm.DrawStyle = 0
  271.         DrawForm.FillStyle = 1
  272.     End If
  273.     If PasteBMP Then
  274.         DrawForm.PaintPicture Picture1.Image, X, Y, CopyWidth, CopyHeight, 0, 0, CopyWidth, CopyHeight, &H660046
  275.         XPrevious = X
  276.         YPrevious = Y
  277.         Exit Sub
  278.     End If
  279.     If PrintText Then
  280.         Label1.ForeColor = DrawForm.ForeColor
  281.         Label1.Visible = True
  282.         Label1.Left = X
  283.         Label1.Top = Y
  284.         Exit Sub
  285.     End If
  286. End Sub
  287. Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  288.     If Button <> 1 Then Exit Sub
  289.     If CopyBMP Or CutBMP Then
  290.         DrawForm.Line (XStart, YStart)-(XPrevious, YPrevious), , B
  291.         DrawForm.Line (XStart, YStart)-(X, Y), , B
  292.         XPrevious = X
  293.         YPrevious = Y
  294.         Exit Sub
  295.     End If
  296.     If PasteBMP Then
  297.         DrawForm.PaintPicture Picture1.Image, XPrevious, YPrevious, CopyWidth, CopyHeight, 0, 0, CopyWidth, CopyHeight, &H660046
  298.         DrawForm.PaintPicture Picture1.Image, X, Y, CopyWidth, CopyHeight, 0, 0, CopyWidth, CopyHeight, &H660046
  299.         XPrevious = X
  300.         YPrevious = Y
  301.         Exit Sub
  302.     End If
  303.     If PrintText Then
  304.         Label1.Left = X
  305.         Label1.Top = Y
  306.         Exit Sub
  307.     End If
  308.     Select Case Shape
  309.         Case "LINE":
  310.             DrawForm.Line (XStart, YStart)-(XPrevious, YPrevious)
  311.             DrawForm.Line (XStart, YStart)-(X, Y)
  312.         Case "CIRCLE":
  313.             DrawForm.Circle (XStart, YStart), Sqr((XPrevious - XStart) ^ 2 + (YPrevious - YStart) ^ 2)
  314.             DrawForm.Circle (XStart, YStart), Sqr((X - XStart) ^ 2 + (Y - YStart) ^ 2)
  315.         Case "BOX":
  316.             DrawForm.Line (XStart, YStart)-(XPrevious, YPrevious), , B
  317.             DrawForm.Line (XStart, YStart)-(X, Y), , B
  318.     End Select
  319.     XPrevious = X
  320.     YPrevious = Y
  321.         
  322. End Sub
  323. Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
  324. Dim X1, Y1
  325. Dim oldDrawMode
  326.     If CopyBMP Then
  327.         DrawForm.Line (XStart, YStart)-(XPrevious, YPrevious), , B
  328.         If X > XStart Then X1 = XStart Else X1 = X
  329.         If Y > YStart Then Y1 = YStart Else Y1 = Y
  330.         Picture1.PaintPicture DrawForm.Image, 0, 0, Abs(X - XStart), Abs(Y - YStart), X1, Y1, Abs(X - XStart), Abs(Y - YStart), &HCC0020
  331.         CopyBMP = False
  332.         DrawForm.DrawWidth = PDrawWidth
  333.         DrawForm.DrawStyle = PDrawStyle
  334.         DrawForm.FillStyle = PFillStyle
  335.         CopyWidth = Abs(X - XStart)
  336.         CopyHeight = Abs(Y - YStart)
  337.         Exit Sub
  338.     End If
  339.     If CutBMP Then
  340.         oldDrawMode = DrawForm.DrawMode
  341.         DrawForm.DrawMode = 13
  342.         CopyWidth = XStart - X
  343.         CopyHeight = YStart - Y
  344.         If X > XStart Then X1 = XStart Else X1 = X
  345.         If Y > YStart Then Y1 = YStart Else Y1 = Y
  346.         Picture1.PaintPicture DrawForm.Image, 0, 0, Abs(X - XStart), Abs(Y - YStart), X1, Y1, Abs(X - XStart), Abs(Y - YStart), &HCC0020
  347.         DrawForm.Line (X, Y)-Step(CopyWidth, CopyHeight), DrawForm.BackColor, BF
  348.         CutBMP = False
  349.         DrawForm.DrawWidth = PDrawWidth
  350.         DrawForm.DrawStyle = PDrawStyle
  351.         DrawForm.FillStyle = PFillStyle
  352.         DrawForm.DrawMode = oldDrawMode
  353.         CopyWidth = Abs(X - XStart)
  354.         CopyHeight = Abs(Y - YStart)
  355.        
  356.         Exit Sub
  357.     End If
  358.     If PasteBMP Then
  359.         DrawForm.PaintPicture Picture1.Image, X, Y, CopyWidth, CopyHeight, 0, 0, CopyWidth, CopyHeight, &HCC0020
  360.         PasteBMP = False
  361.         Exit Sub
  362.     End If
  363.     If PrintText Then
  364.         DrawForm.AutoRedraw = True
  365.         DrawForm.CurrentX = X
  366.         DrawForm.CurrentY = Y
  367.         DrawForm.Print Label1.Caption
  368.         Label1.Visible = False
  369.         PrintText = False
  370.         Exit Sub
  371.     End If
  372.     DrawForm.DrawMode = 13
  373.      Select Case Shape
  374.         Case "LINE":
  375.             DrawForm.Line (XStart, YStart)-(X, Y)
  376.         Case "CIRCLE":
  377.             DrawForm.Circle (XStart, YStart), Sqr((X - XStart) ^ 2 + (Y - YStart) ^ 2)
  378.             ' the following statement erases the dot at the circle's center
  379.             DrawForm.Circle (XStart, YStart), DrawForm.DrawWidth, DrawForm.BackColor
  380.         Case "BOX":
  381.             DrawForm.Line (XStart, YStart)-(X, Y), , B
  382.     End Select
  383. End Sub
  384. Private Sub Form_Resize()
  385.     Picture1.Width = DrawForm.Width
  386.     Picture1.Height = DrawForm.Height
  387. End Sub
  388. Private Sub StyleDash_Click()
  389.     UnCheckStyles
  390.     StyleDash.Checked = True
  391.     DrawForm.DrawStyle = 1
  392. End Sub
  393. Private Sub StyleDot_Click()
  394.     UnCheckStyles
  395.     StyleDot.Checked = True
  396.     DrawForm.DrawStyle = 2
  397. End Sub
  398. Private Sub StyleFilled_Click()
  399.     StyleFilled.Checked = Not StyleFilled.Checked
  400.     If StyleFilled.Checked Then
  401.         DrawForm.FillStyle = 0
  402.     Else
  403.         DrawForm.FillStyle = 1
  404.     End If
  405. End Sub
  406. Private Sub StyleSolid_Click()
  407.     UnCheckStyles
  408.     StyleSolid.Checked = True
  409.     DrawForm.DrawStyle = 0
  410. End Sub
  411. Private Sub width1_Click()
  412.     DrawForm.DrawWidth = 1
  413. End Sub
  414. Private Sub Width2_Click()
  415.     DrawForm.DrawWidth = 2
  416. End Sub
  417. Private Sub Width3_Click()
  418.     DrawForm.DrawWidth = 3
  419. End Sub
  420.