VERSION 5.00 Begin VB.Form sketcher Caption = "Andy's Sketch Pad" ClientHeight = 3480 ClientLeft = 60 ClientTop = 345 ClientWidth = 5010 LinkTopic = "Form1" ScaleHeight = 3480 ScaleWidth = 5010 StartUpPosition = 3 'Windows Default Begin VB.CommandButton cmdCircle Caption = "circle" Height = 375 Left = 120 TabIndex = 21 Top = 1920 Width = 495 End Begin VB.PictureBox picDrawColor Height = 495 Left = 2400 ScaleHeight = 435 ScaleWidth = 435 TabIndex = 20 Top = 3000 Width = 495 End Begin VB.PictureBox picColor Height = 255 Index = 15 Left = 4560 ScaleHeight = 195 ScaleWidth = 195 TabIndex = 19 Top = 3240 Width = 255 End Begin VB.PictureBox picColor Height = 255 Index = 14 Left = 4320 ScaleHeight = 195 ScaleWidth = 195 TabIndex = 18 Top = 3240 Width = 255 End Begin VB.PictureBox picColor Height = 255 Index = 13 Left = 4080 ScaleHeight = 195 ScaleWidth = 195 TabIndex = 17 Top = 3240 Width = 255 End Begin VB.PictureBox picColor Height = 255 Index = 12 Left = 3840 ScaleHeight = 195 ScaleWidth = 195 TabIndex = 16 Top = 3240 Width = 255 End Begin VB.PictureBox picColor Height = 255 Index = 11 Left = 3600 ScaleHeight = 195 ScaleWidth = 195 TabIndex = 15 Top = 3240 Width = 255 End Begin VB.PictureBox picColor Height = 255 Index = 10 Left = 3360 ScaleHeight = 195 ScaleWidth = 195 TabIndex = 14 Top = 3240 Width = 255 End Begin VB.PictureBox picColor Height = 255 Index = 9 Left = 3120 ScaleHeight = 195 ScaleWidth = 195 TabIndex = 13 Top = 3240 Width = 255 End Begin VB.PictureBox picColor Height = 255 Index = 8 Left = 2880 ScaleHeight = 195 ScaleWidth = 195 TabIndex = 12 Top = 3240 Width = 255 End Begin VB.PictureBox picColor Height = 255 Index = 7 Left = 4560 ScaleHeight = 195 ScaleWidth = 195 TabIndex = 11 Top = 3000 Width = 255 End Begin VB.PictureBox picColor Height = 255 Index = 6 Left = 4320 ScaleHeight = 195 ScaleWidth = 195 TabIndex = 10 Top = 3000 Width = 255 End Begin VB.PictureBox picColor Height = 255 Index = 5 Left = 4080 ScaleHeight = 195 ScaleWidth = 195 TabIndex = 9 Top = 3000 Width = 255 End Begin VB.PictureBox picColor Height = 255 Index = 4 Left = 3840 ScaleHeight = 195 ScaleWidth = 195 TabIndex = 8 Top = 3000 Width = 255 End Begin VB.PictureBox picColor Height = 255 Index = 3 Left = 3600 ScaleHeight = 195 ScaleWidth = 195 TabIndex = 7 Top = 3000 Width = 255 End Begin VB.PictureBox picColor Height = 255 Index = 2 Left = 3360 ScaleHeight = 195 ScaleWidth = 195 TabIndex = 6 Top = 3000 Width = 255 End Begin VB.PictureBox picColor Height = 255 Index = 1 Left = 3120 ScaleHeight = 195 ScaleWidth = 195 TabIndex = 5 Top = 3000 Width = 255 End Begin VB.PictureBox picColor Height = 255 Index = 0 Left = 2880 ScaleHeight = 195 ScaleWidth = 195 TabIndex = 4 Top = 3000 Width = 255 End Begin VB.CommandButton cmdRect Caption = "rect" Height = 375 Left = 120 TabIndex = 3 Top = 1440 Width = 495 End Begin VB.CommandButton cmdLine Caption = "line" Height = 375 Left = 120 TabIndex = 2 Top = 960 Width = 495 End Begin VB.CommandButton cmdDraw Caption = "draw" Height = 375 Left = 120 TabIndex = 1 Top = 480 Width = 495 End Begin VB.PictureBox picDraw BackColor = &H00FFFFFF& Height = 2655 Left = 720 ScaleHeight = 2595 ScaleWidth = 4035 TabIndex = 0 Top = 360 Width = 4095 Begin VB.Shape shpStretcher Height = 495 Left = 480 Top = 360 Visible = 0 'False Width = 855 End End End Attribute VB_Name = "Form1" Attribute VB_GlobalNameSpace = False Attribute VB_Creatable = False Attribute VB_PredeclaredId = True Attribute VB_Exposed = False 'sketch 'a sketch pad by Andy Harris 'example for Adv. VB 1/00 'demonstrates interesting adv. VB concepts 'these variables will be used in line drawing commands 'they will hold positions from where mouse was pressed Dim oldX As Single Dim oldY As Single 'Mode will tell us what the user is trying to do Dim mode As Integer 'These constants are all the legal values for mode 'The advantage of making them constants is we get the 'best of both worlds: mDRAW obviously means drawing mode, 'but it's an integer, so it's not as ambiguous as a string value 'Lateer on we'll talk about ENUMs, which are perfect for this kind 'of thing. Const mDRAW = 1 Const mLINE = 2 Const mRECT = 3 Const mCIRCLE = 4 'This is the drawing color Dim drawColor As Long Public Sub moveShape() 'I tried to put stuff in here, so I wouldn't have to repeat it in 'mouseMove (Note code in select case is nearly identical for 'mRECT and mCIRCLE End Sub Private Sub cmdCircle_Click() 'This and all the cmd_clicks is very simple: 'we simply change the mode to one of the pre-existing constant values mode = mCIRCLE End Sub Private Sub cmdDraw_Click() mode = mDRAW End Sub Private Sub cmdLine_Click() mode = mLINE End Sub Private Sub cmdRect_Click() mode = mRECT End Sub Private Sub Form_Load() Dim i As Integer 'set up drawing as the default mode mode = mDRAW 'initialize the color pallate For i = 0 To 15 picColor(i).BackColor = QBColor(i) Next i End Sub Private Sub picColor_Click(Index As Integer) 'set up the drawing color drawColor = QBColor(Index) picDrawColor.BackColor = drawColor picDraw.ForeColor = drawColor End Sub Private Sub picDraw_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) 'set up oldX and oldY for line, rect, and circle commands 'will be one endpoint of these commands oldX = X oldY = Y 'move the top and left coords of the drawing shape shpStretcher.Top = Y shpStretcher.Left = X End Sub Private Sub picDraw_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) 'do something if mouse button is down If (Button = 1) Then Select Case mode Case mDRAW 'draw a line from the last drawn point to curent x, y picDraw.Line -(X, Y) Case mRECT 'show a rectangle shpStretcher.Shape = 0 'show the preview shape shpStretcher.Visible = True shpStretcher.FillColor = drawColor 'if going up or to left, move the shape If X < oldX Then shpStretcher.Left = X End If If Y < oldY Then shpStretcher.Top = Y End If 'make the shape the correct size shpStretcher.Height = Abs(oldY - Y) shpStretcher.Width = Abs(oldX - X) Case mCIRCLE shpStretcher.Shape = 3 'show the preview shape shpStretcher.Visible = True shpStretcher.FillColor = drawColor 'if going up or to left, move the shape If X < oldX Then shpStretcher.Left = X End If If Y < oldY Then shpStretcher.Top = Y End If 'make the shape the correct size shpStretcher.Height = Abs(oldY - Y) shpStretcher.Width = Abs(oldX - X) End Select End If End Sub Private Sub picDraw_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) Dim circX As Single Dim circY As Single Dim circRad As Single Select Case mode Case mLINE picDraw.Line (oldX, oldY)-(X, Y) Case mRECT 'kill off the stretcher shpStretcher.Height = 1 shpStretcher.Width = 1 shpStretcher.Visible = False picDraw.Line (oldX, oldY)-(X, Y), , BF Case mCIRCLE 'generate some circle values from stretcher circX = shpStretcher.Left + (shpStretcher.Width / 2) circY = shpStretcher.Top + (shpStretcher.Height / 2) circRad = shpStretcher.Height / 2 'kill off the stretcher shpStretcher.Height = 1 shpStretcher.Width = 1 shpStretcher.Visible = False picDraw.Circle (circX, circY), circRad End Select End Sub