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