Results 1 to 5 of 5

Thread: DrawText API

  1. #1

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336

    DrawText API

    Harro. I am trying to draw some text on a picture box using the DrawText API. For Some Reason it does not work, It doesnt draw anything at all. Maybe someone can fill me in.

    I am also using the SetRect API to draw the rect to draw the text in.

    VB Code:
    1. 'In Module
    2. Public Declare Function DrawText Lib "user32" Alias "DrawTextA" _
    3.                        (ByVal hdc As Long, _
    4.                         ByVal lpStr As String, ByVal nCount As Long, _
    5.                         lpRect As RECT, _
    6.                         ByVal wFormat As Long) As Long
    7.  
    8. Public Declare Function SetRect Lib "user32" _
    9.                        (lpRect As RECT, _
    10.                         ByVal X1 As Long, ByVal Y1 As Long, _
    11.                         ByVal X2 As Long, ByVal Y2 As Long) As Long
    12.  
    13.  
    14. Public Type RECT
    15.             Left As Long
    16.             Top As Long
    17.             Right As Long
    18.             Bottom As Long
    19. End Type
    20.  
    21. Public Const DT_CENTER = &H1
    22. Public Const DT_LEFT = &H0
    23. Public Const DT_RIGHT = &H2
    24. Public Const DT_VCENTER = &H4
    25. Public Const DT_WORDBREAK = &H10
    26. Public Const DT_SINGLELINE = &H20
    27.  
    28. 'In General Section of Form
    29. Dim ItemRct As RECT
    30.  
    31. 'In a command Button
    32.  
    33. SetRect ItemRct, 0, Picture1.TextHeight("Aa"), ItemRct.Right, Picture1.TextHeight("Aa") + Picture1.TextHeight("Aa")
    34.  
    35. DrawText Picture1.hdc, Text1.Text, Len(Text1), ItemRct, DT_SINGLELINE Or DT_VCENTER

    THANKS!
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    This works. I just tried it. Hopefully you can modify this example to fit your needs.
    VB Code:
    1. Const DC_ACTIVE = &H1
    2. Const DC_ICON = &H4
    3. Const DC_TEXT = &H8
    4. Const BDR_SUNKENOUTER = &H2
    5. Const BDR_RAISEDINNER = &H4
    6. Const EDGE_ETCHED = (BDR_SUNKENOUTER Or BDR_RAISEDINNER)
    7. Const BF_BOTTOM = &H8
    8. Const BF_LEFT = &H1
    9. Const BF_RIGHT = &H4
    10. Const BF_TOP = &H2
    11. Const BF_RECT = (BF_LEFT Or BF_TOP Or BF_RIGHT Or BF_BOTTOM)
    12. Const DFC_BUTTON = 4
    13. Const DFC_POPUPMENU = 5            'Only Win98/2000 !!
    14. Const DFCS_BUTTON3STATE = &H10
    15. Const DT_CENTER = &H1
    16. Const DC_GRADIENT = &H20          'Only Win98/2000 !!
    17. Private Type RECT
    18.     Left As Long
    19.     Top As Long
    20.     Right As Long
    21.     Bottom As Long
    22. End Type
    23. Private Declare Function DrawCaption Lib "user32" (ByVal hWnd As Long, ByVal hdc As Long, pcRect As RECT, ByVal un As Long) As Long
    24. Private Declare Function DrawEdge Lib "user32" (ByVal hdc As Long, qrc As RECT, ByVal edge As Long, ByVal grfFlags As Long) As Long
    25. Private Declare Function DrawFocusRect Lib "user32" (ByVal hdc As Long, lpRect As RECT) As Long
    26. Private Declare Function DrawFrameControl Lib "user32" (ByVal hdc As Long, lpRect As RECT, ByVal un1 As Long, ByVal un2 As Long) As Long
    27. Private Declare Function DrawText Lib "user32" Alias "DrawTextA" (ByVal hdc As Long, ByVal lpStr As String, ByVal nCount As Long, lpRect As RECT, ByVal wFormat As Long) As Long
    28. Private Declare Function SetRect Lib "user32" (lpRect As RECT, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
    29. Private Declare Function OffsetRect Lib "user32" (lpRect As RECT, ByVal x As Long, ByVal y As Long) As Long
    30. Private Sub Form_Paint()
    31.     'KPD-Team 1999
    32.     'URL: [url]http://www.allapi.net/[/url]
    33.     'E-Mail: [email][email protected][/email]
    34.     Dim R As RECT
    35.     'Clear the form
    36.     Me.Cls
    37.     'API uses pixels
    38.     Me.ScaleMode = vbPixels
    39.     'Set the rectangle's values
    40.     SetRect R, 0, 0, Me.ScaleWidth, 20
    41.     'Draw a caption on the form
    42.     DrawCaption Me.hWnd, Me.hdc, R, DC_ACTIVE Or DC_ICON Or DC_TEXT Or DC_GRADIENT
    43.     'Move the recatangle
    44.     OffsetRect R, 0, 22
    45.     'Draw an edge on our window
    46.     DrawEdge Me.hdc, R, EDGE_ETCHED, BF_RECT
    47.     OffsetRect R, 0, 22
    48.     'Draw a focus rectangle on our window
    49.     DrawFocusRect Me.hdc, R
    50.     OffsetRect R, 0, 22
    51.     'Draw a frame control on our window
    52.     DrawFrameControl Me.hdc, R, DFC_BUTTON, DFCS_BUTTON3STATE
    53.     OffsetRect R, 0, 22
    54.     'draw some text on our form
    55.     DrawText Me.hdc, "Hello World !", Len("Hello World !"), R, DT_CENTER
    56. End Sub

  3. #3

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    Thanks. Turns out i was missing the line. Picture1.Scalemode=vbpixels
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  4. #4

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    ARG! Dangit i can not figure this out. I think i had it working once but then fooled around with it and now cant fix it back.


    I can draw the text on the picturebox just fine. My problem is Getting the text to add like a list. I.e. i can add this

    Text1


    But when i click again i want it to add another Text1 below it such as

    Text1
    Text1
    Text1


    I cant figure it out! Good lord how agrivating.

    VB Code:
    1. 'This code here in a command button draws 1 item
    2. 'Maybe someone can figure out how to get it to draw multiple items i HOPE:D
    3.  
    4. Picture1.ScaleMode = vbPixels
    5. SetRect ItemRct, 0, 0, Picture1.ScaleWidth, Picture1.TextHeight("Aa")
    6. DrawText Picture1.hdc, Text1.Text, Len(Text1), ItemRct, DT_SINGLELINE Or DT_VCENTER

    Just add the API from above to a module and those 3 lines to a command button or whatever.

    THANKS!

    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  5. #5

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    NM i figured it out FInally! Whoohoo!
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


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