Draw lines in a userform

G

gbottesi

I need to draw oblique lines in a form, as you can do in VB using th
drawline tool, but I don't know how todo that in VBA since there´s n
drawline tool.
I saw a post with the same problem, solved by using 1-pixel-widt
labels, but its only works with horizontal al vertical lines
 
T

Tom Ogilvy

while cumbersome, you could create a bitmap that appears as you wish and
have it as the background of the userform or use several with image
controls.
 
M

Michel Pierron

Hi gbottesi,
For the fun, in the userform module:
Option Explicit
Private Declare Function GetDC& Lib "user32" (ByVal hWnd&)
Private Declare Function LineTo& Lib "gdi32" (ByVal hDc& _
, ByVal x&, ByVal y&)
Private Declare Function MoveToEx& Lib "gdi32" _
(ByVal hDc&, ByVal x&, ByVal y&, lpPoint As POINTAPI)
Private Declare Function GetActiveWindow& Lib "user32" ()

Private Type POINTAPI
x As Long
y As Long
End Type

Private hDc As Long

Private Sub DrawLine(ByVal X1&, ByVal Y1&, ByVal X2&, ByVal Y2&)
Dim pt As POINTAPI
MoveToEx hDc, X1, Y1, pt
LineTo hDc, X2, Y2
End Sub

Private Sub UserForm_Activate()
hDc = GetDC(GetActiveWindow)
Dim i As Byte
DoEvents
For i = 1 To 10
DrawLine 8, 8, i * 30, (Me.InsideHeight * 4 / 3) - 8
Next i
End Sub

MP
 
J

Jon Peltier

You can paste a drawing object into an image control. Copy the drawing
object, select the image control, click in the picture property in the
Properties pane (where it probably says (none), and paste. (none)
changes to (Metafile). Format the image control so it has no border and
a transparent background, and choose the Center PictureAlignment and the
Stretch PictureSizeMode.

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Peltier Technical Services
Tutorials and Custom Solutions
http://PeltierTech.com/
_______
 
Joined
Apr 30, 2019
Messages
1
Reaction score
0
Hi there,

I used the code below to draw a line on a Userform.

Now can anyone tell me how I can delete the line I just drew?

Thank you,




Hi gbottesi,
For the fun, in the userform module:
Option Explicit
Private Declare Function GetDC& Lib "user32" (ByVal hWnd&)
Private Declare Function LineTo& Lib "gdi32" (ByVal hDc& _
, ByVal x&, ByVal y&)
Private Declare Function MoveToEx& Lib "gdi32" _
(ByVal hDc&, ByVal x&, ByVal y&, lpPoint As POINTAPI)
Private Declare Function GetActiveWindow& Lib "user32" ()

Private Type POINTAPI
x As Long
y As Long
End Type

Private hDc As Long

Private Sub DrawLine(ByVal X1&, ByVal Y1&, ByVal X2&, ByVal Y2&)
Dim pt As POINTAPI
MoveToEx hDc, X1, Y1, pt
LineTo hDc, X2, Y2
End Sub

Private Sub UserForm_Activate()
hDc = GetDC(GetActiveWindow)
Dim i As Byte
DoEvents
For i = 1 To 10
DrawLine 8, 8, i * 30, (Me.InsideHeight * 4 / 3) - 8
Next i
End Sub

MP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top