home *** CD-ROM | disk | FTP | other *** search
/ Mastering Visual Basic 5 / MasteringVisualBasic5.iso / ch_code / ch06 / draw / draw.frm (.txt) next >
Encoding:
Visual Basic Form  |  1997-02-20  |  12.5 KB  |  416 lines

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