PDA

Click to See Complete Forum and Search --> : Writing text on picturebox


Celery
Jan 2nd, 2001, 07:48 AM
Hi!

This should be pretty easy question! All I want to know is
how to write text on picturebox. I know how to do it in
Delphi but I have never tried doing it in VB. Please help!

Thanx in advance for any answer.


Celery

YoungBuck
Jan 2nd, 2001, 08:52 AM
Hi,

I think this is what you are looking for...



Option Explicit

Private Declare Function TextOut Lib "gdi32" Alias "TextOutA" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal lpString As String, ByVal nCount As Long) As Long

Private Sub Picture1_Paint()
Dim lRet As Long
Dim s As String

s = "HELLO WORLD!"

lRet = TextOut(Picture1.hdc, 5, 5, s, Len(s))

End Sub



Just make sure you put this code in the Paint event of the PictureBox or else the text will be erased the next time the PictureBox is redrawn. :)

Celery
Jan 2nd, 2001, 10:24 AM
Isn't there any easier way to do this???


Celery

YoungBuck
Jan 2nd, 2001, 10:42 AM
Alternatively you can use the Print method, however I don't recommend it.



Private Sub Picture1_Paint()

Picture1.Print "HELLO WORLD!"

End Sub



Once again make sure the code is in the Paint Event

Fox
Jan 2nd, 2001, 11:43 AM
TextOut is not that hard... please dont use the Print command ;) Ill explain the same with more comments:


'Add this to the declares-section of your form:
Private Declare Function TextOut Lib "gdi32" Alias "TextOutA" _
(ByVal hdc As Long, ByVal x As Long, ByVal y As Long, _
ByVal lpString As String, ByVal nCount As Long) As Long

'And this in the button where you want to draw
'the text (Replace Picture1, the coordinates
'and the text to fit your needs):
TextOut Picture1.hdc, 5, 10, "Hello World", Len("Hello World")

'Alternatively you can use a variable, makes
'code easier and better understanding:
Dim Temp as String

Temp = "Hello World"
TextOut Picture1.hdc, 5, 10, Temp, Len(Temp)

YoungBuck
Jan 2nd, 2001, 12:02 PM
If you use the TextOut (or the evil Print function) in a command button you will have to make sure that you redraw the text in the Paint event, else you will lose the drawn text when the PictureBox is repainted.

Fox
Jan 2nd, 2001, 12:19 PM
Excepting if you set AutoRedraw of the PictureBox to true, but then make sure you refresh the box to show
its changed contents:


TextOut Picture1.hdc, [...]
Picture1.Refresh
;)