You are on page 1of 8

' Visual Basic 6.

0
Private Sub Form_Paint()
' Draw a line with a thickness of 1 pixel.
DrawWidth = 1
Line (0, 200)-(ScaleWidth, 200), vbBlack
' Draw a line with a thickness of 5 pixels.
DrawWidth = 5
Line (0, 400)-(ScaleWidth, 400), vbBlack
' Draw a line with a thickness of 10 pixels.
DrawWidth = 10
Line (0, 600)-(ScaleWidth, 600), vbBlack
End Sub

Determining the Height and Width of a String


The following code demonstrates graphics methods for determining the size of a s
tring on a form at run time, and then drawing a rectangle around it. The Visual
Basic 6.0 example uses the TextHeight and TextWidth methods. The Visual Basic 20
05 example uses the MeasureString method, which returns a SizeF structure.
Copy' Visual Basic 6.0
Private Sub Form_Paint()
Me.Font.Size = 24
Me.Font.Bold = True
Me.ForeColor = vbRed
Print "Hello World!"
Line (0, 0)-(TextWidth("Hello World!"), _
TextHeight("Hello World!")), vbBlack, B
End Sub

Drawing a Single Pixel


The following example demonstrates graphics methods for changing the color of a
single pixel on a form at run time. The Visual Basic 6.0 example uses the PSet m
ethod. The Visual Basic 2005 example uses the DrawEllipse method with the Height
and Width parameters set to 1.
Note In Visual Basic 6.0, the default unit of measurement is twips; in Visual
Basic 2005 it is pixels.
Copy' Visual Basic 6.0
Private Sub Form_Paint()
Me.DrawWidth = 1
PSet (1000, 1000), vbRed
End Sub

Re: Mouse click get coordinate and draw line


To capture the mouse co-ordinates, try something like -

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)


'In a module add the following code...Private Type POINTAPI x As Long
y As LongEnd Type 'Private Declare Function M_GetCursorPos Lib "user
32" Alias "GetCursorPos" (lpPoint As POINTAPI) As Long Public Sub GetCursorPos(x
X As Long, xY As Long)Dim pt As POINTAPICall M_GetCursorPos(pt) xX = pt.xxY
= pt.yEnd Sub'In a module add the following code...
Private Type POINTAPI
x As Long
y As Long
End Type
'Private Declare Function M_GetCursorPos Lib "user32" Alias "GetCursorPos" (lpPo
int As POINTAPI) As Long
Public Sub GetCursorPos(xX As Long, xY As Long)
Dim pt As POINTAPI
Call M_GetCursorPos(pt)
xX = pt.x
xY = pt.y
End Sub

Re: Mouse click get coordinate and draw line


To draw a line with the mouse, you can use the click events of the mouse as in -

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)


'Enter the following code in the Form_MouseDown ( ) procedure, Form_MouseMove (
) procedure and cmdClear_Click ( ) procedures respectively. Private Sub cmdClear
_Click()frmDraw.ClsEnd Sub Private Sub Form_MouseDown(Button As Integer, Shift A
s Integer, X As Single, Y As Single)frmDraw.CurrentX = XfrmDraw.CurrentY = YEnd
Sub Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single,
Y As Single)If Button = 1 ThenLine (frmDraw.CurrentX, frmDraw.CurrentY)-(X, Y)E
nd IfEnd Sub'Enter the following code in the Form_MouseDown ( ) procedure, Form_
MouseMove ( ) procedure and cmdClear_Click ( ) procedures respectively.
Private Sub cmdClear_Click()
frmDraw.Cls
End Sub
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y A
s Single)
frmDraw.CurrentX = X
frmDraw.CurrentY = Y
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y A
s Single)
If Button = 1 Then
Line (frmDraw.CurrentX, frmDraw.CurrentY)-(X, Y)
End If
End Sub
VB6 monitoring the mouse track or a straight line on a form
Random lines
Straight Lines
Coding

Here are some tips on how you can monitor some lines in a straight line on a for
m
Random lines
To follow that track the mouse, click on the form, press and hold the right butt
on and move.
Release button to finish.
Straight Lines
To draw straight lines, click on the form, from the starting point and press and
hold the button, move the mouse pointer to the destination point and release th
e button.
Coding
For this function, move the code as shown below.
It can be used on any control that contains an hdc.

Option Explicit
Const PS_SOLID = 0
Const Epp = 10 'size of the line
Private Type POINTAPI
X As Long
Y As Long
End Type
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Lon
g
Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObj
ect As Long) As Long
Private Declare Function CreatePen Lib "gdi32" (ByVal nPenStyle As Long, ByVal n
Width As Long, ByVal crColor As Long) As Long
Private Declare Function LineTo Lib "gdi32" (ByVal hdc As Long, ByVal X As Long,
ByVal Y As Long) As Long
Private Declare Function MoveToEx Lib "gdi32" (ByVal hdc As Long, _
ByVal X As Long, ByVal Y As Long, lpPoint As Any) As Long

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y A


s Single)
DeleteObject SelectObject(Me.hdc, CreatePen(PS_SOLID, Epp, RGB(0, 255, 0)))
MoveToEx Me.hdc, X, Y, &H0
End Sub
'Put the code in Form_MouseUp for a straight line
'without a line>> If Button <> 1 Then Exit Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y A
s Single)
If Button <> 1 Then Exit Sub
DeleteObject SelectObject(Me.hdc, CreatePen(PS_SOLID, Epp, RGB(0, 255, 0)))
LineTo Me.hdc, X, Y
Me.Refresh
End Sub

-----------

Line1.X1 = 0
Line1.Y1 = 0
Line1.X2 = 0
Line1.Y2 = 0
Line1.Visible = False
Picture1.Visible = False
Picture1.Visible = True
'*************************************************
'All this extra code will draw a square on any angle, but not the rectangle
'that I want to use
Dim X1 As Long
Dim X2 As Long
Dim Y1 As Long
Dim Y2 As Long
Dim SlopeT As Long 'Top part of slope fraction
Dim SlopeB As Long 'Bottom part of slope fraction
SlopeT = (h - f) * 1
SlopeB = (g - e) * -1
X1 = e + SlopeT
Y1 = f + SlopeB
Picture1.Line (e + SlopeT, f + SlopeB)-(e, f), RGB(50, 75, 255)
'the other side
SlopeT = (f - h) * -1
SlopeB = (e - g) * 1
X2 = g + SlopeT
Y2 = h + SlopeB
Picture1.Line (g + SlopeT, h + SlopeB)-(g, h), RGB(50, 75, 255)
'And the last line
Picture1.Line (X1, Y1)-(X2, Y2), RGB(50, 75, 255)
End Sub
--------------------------------------------------------------------------------
The Angle.
regarding the code below, i didn't anderstand how do you set the angle to be 90
dgrees left or right?
thanks.

Private Sub bmp_rotate(pic1 As Control, pic2 As Control, ByVal theta!)


Const Pi = 3.14159265359
Dim c1x As Integer ' Center of pic1.
Dim c1y As Integer ' "
Dim c2x As Integer ' Center of pic2.
Dim c2y As Integer ' "
Dim a As Single ' Angle of c2 to p2.
Dim r As Integer ' Radius from c2 to p2.
Dim p1x As Integer ' Position on pic1.
Dim p1y As Integer ' "
Dim p2x As Integer ' Position on pic2.
Dim p2y As Integer ' "
Dim n As Integer ' Max width or height of pic2.
' Compute the centers.
c1x = pic1.ScaleWidth / 2
c1y = pic1.ScaleHeight / 2
c2x = pic2.ScaleWidth / 2
c2y = pic2.ScaleHeight / 2
' Compute the image size.
n = pic2.ScaleWidth
If n < pic2.ScaleHeight Then n = pic2.ScaleHeight
n = n / 2 - 1
' For each pixel position on pic2.
For p2x = 0 To n
For p2y = 0 To n
' Compute polar coordinate of p2.
If p2x = 0 Then
a = Pi / 2
Else
a = Atn(p2y / p2x)
End If
r = Sqr(1& * p2x * p2x + 1& * p2y * p2y)
' Compute rotated position of p1.
p1x = r * Cos(a + theta)
p1y = r * Sin(a + theta)
' Copy pixels, 4 quadrants at once.
c0& = pic1.Point(c1x + p1x, c1y + p1y)
c1& = pic1.Point(c1x - p1x, c1y - p1y)
c2& = pic1.Point(c1x + p1y, c1y - p1x)
c3& = pic1.Point(c1x - p1y, c1y + p1x)
If c0& <> -1 Then pic2.PSet (c2x + p2x, c2y + p2y), c0&
If c1& <> -1 Then pic2.PSet (c2x - p2x, c2y - p2y), c1&
If c2& <> -1 Then pic2.PSet (c2x + p2y, c2y - p2x), c2&
If c3& <> -1 Then pic2.PSet (c2x - p2y, c2y + p2x), c3&
Next
' Allow pending Windows messages to be processed.
t% = DoEvents()
Next
End Sub

'Here is a shorter routine for testing purposes:

Private Sub Command1_Click()


Const Pi = 3.14159265359
angle = Pi / 6
Picture2.Cls
Call bmp_rotate(Picture1, Picture2, angle)
End Sub
--------------------------------------------------------------------------------
Line Input #1, Line...how Do I Read Ahead One Line (or Back Up One Line)
I am loading a file line by line using the LIne Input but at one point i need to
look ahead at the next line to decided what to do on the current line...how do
i look ahead one line? or how do i use the line Input function and then back up?
Any help would be greatly appreciated.
Shane Thomas
p.s. please email replys in addition to the reply of post too
shathoma@nmsu.edu
--------------------------------------------------------------------------------
Angle Of Reflection
I am trying to make a ball bounce off a surface given the direction the ball is
heading to the surface, the direction the surface is facing, and the location of
the ball and the surface. I have tried to apply a formula such as shown on this
website:
http://www.cs.princeton.edu/~cdecoro...up/RTG_HW3.htm
Though I have not been able to transpose it correctly it seems. Could anyone pos
sibly help me out?
--------------------------------------------------------------------------------
Angle Prob Nob
my problem is when i use the builtin d3dxmatrixrotationx y z scaling translation
functions
the angles i set the matrix to is off by at least 10 degs! i think it could have
something to do with using singles instead of doubles or converting for degs to
rads
--------------------------------------------------------------------------------
Help - Finding An Angle By Its Sin...
I'm sorry if this is in the wrong Forum...
In VBA, is there any way of finding an angle by its sin?
Thanks.
--------------------------------------------------------------------------------
Angle Question...
Hey, im having a little bit of a problem... In a game, i have "stars" coming fro
m the center and moving to the edges, however there is a blank gap where they ne
ver pass. I believe the reason is due to rounding.
in the following code
Code:
Debug.Print Cos(RadToDeg(10))
Debug.Print Cos(RadToDeg(370))
0.986143231562925
0.993219379893898
They should be the same value...
Heres my radtodeg function:

Code:
Public Const PI As Long = 3.14159265358979
Public Function RadToDeg(dAngle As Double) As Double
RadToDeg = dAngle * (PI / 180)
End Function
Any ideas on how to solve this problem?
--------------------------------------------------------------------------------
Angle Calculation
Hey there
I would like to ask about angel calculations, using grid..does anyone know how t
o go about this cause i can't seem to find examples on this in vb.... esp vb gra
phics
thanks
--------------------------------------------------------------------------------
How I Get Angle Between Two Lines?
I only get the angle between two segment lines of 0º to 180º.
HowIi get of 0º to 360º?
The result I get is from 0º to 180º and I want from 0º to 360º.

Marco mota
--------------------------------------------------------------------------------
GFX.RotateTransform(angle) In VB6?
I read a little on how to use this, but then i realized it was talking about .NE
T....is there any equilivence to this in VB6? basically I want to rotate an imag
e.
--------------------------------------------------------------------------------
Rotate By Angle (in Dx)
Hi,
how can I rotate the object(dx) by angle???
thanks
--------------------------------------------------------------------------------
Angle Between Two Lines
I have two lines AB, BC with known coordinates. they both have a common coordina
te at point B.
How to find angle between AB and BC wrt VB6.
any Help for formula for finding angle at B.
--------------------------------------------------------------------------------
Finding An Angle
with known value of TanA, how to find the value of A.
A is in radians. i am using VB6.
any help pl.

You might also like