Results 1 to 7 of 7

Thread: Writing text on picturebox

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2000
    Location
    Slovenia
    Posts
    32
    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
    VB6 Pro SP3 - Academic Licence

  2. #2
    Fanatic Member
    Join Date
    Sep 1999
    Location
    Bethel, North Carolina, USA
    Posts
    987
    Hi,

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

    Code:
    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.
    {Insert random techno-babble here}

    {Insert quote from some long gone mofo here}

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2000
    Location
    Slovenia
    Posts
    32
    Isn't there any easier way to do this???


    Celery
    VB6 Pro SP3 - Academic Licence

  4. #4
    Fanatic Member
    Join Date
    Sep 1999
    Location
    Bethel, North Carolina, USA
    Posts
    987
    Alternatively you can use the Print method, however I don't recommend it.

    Code:
    Private Sub Picture1_Paint()
        
       Picture1.Print "HELLO WORLD!"
    
    End Sub
    Once again make sure the code is in the Paint Event
    {Insert random techno-babble here}

    {Insert quote from some long gone mofo here}

  5. #5
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    TextOut is not that hard... please dont use the Print command Ill explain the same with more comments:

    Code:
    '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)

  6. #6
    Fanatic Member
    Join Date
    Sep 1999
    Location
    Bethel, North Carolina, USA
    Posts
    987
    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.
    {Insert random techno-babble here}

    {Insert quote from some long gone mofo here}

  7. #7
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    Excepting if you set AutoRedraw of the PictureBox to true, but then make sure you refresh the box to show
    its changed contents:

    Code:
    TextOut Picture1.hdc, [...]
    Picture1.Refresh

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width