Results 1 to 6 of 6

Thread: Draw Text with hDC/hFont question

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2001
    Location
    Canada
    Posts
    202

    Draw Text with hDC/hFont question

    Hello,

    I need to draw text within a window, but I only have the device context (hDC) for this window. Is there anyway to do this without using win32 API and if not, how do I get a font's hFont in VB .net?

    Thanks,

    Lloyd

  2. #2
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    Not sure if this is what you want (I don't know graphics using the API) but it *looks* like it:

    ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfSystemDrawingGraphicsClassFromHdcTopic.htm

    VB Code:
    1. Public Sub FromHdcHdc(e As PaintEventArgs)
    2.     ' Get handle to device context.
    3.     Dim hdc As IntPtr = e.Graphics.GetHdc()
    4.     ' Create new graphics object using handle to device context.
    5.     Dim newGraphics As Graphics = Graphics.FromHdc(hdc)
    6.     ' Draw rectangle to screen.
    7.     newGraphics.DrawRectangle(New Pen(Color.Red, 3), 0, 0, 200, 100)
    8.     ' Release handle to device context.
    9.     e.Graphics.ReleaseHdc(hdc)
    10. End Sub

    For fonts, the Font object exposes a .ToHfont method:

    ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfSystemDrawingFontClassToHfontTopic.htm

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2001
    Location
    Canada
    Posts
    202
    OK... Thanks for your help. It looks like you may have something there.

    However, this brings up another question. How do I convert an Integer (Int32) to IntPtr? The hDC is being passed as an Integer (and that can't be changed), therefore I get "Value type of 'Integer' cannot be converted to 'System.IntPtr'" on this line:

    VB Code:
    1. Dim gpxResult As Graphics = Graphics.FromHdc(hDC)

    Thanks,

    Lloyd

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Sep 2001
    Location
    Canada
    Posts
    202
    OK... I found IntPtr.op_Explicit to convert the Integer to IntPtr.

    Now to contiue on with the drawing aspect... I will let you know how it goes.

  5. #5
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    First , GDI+ contains most of the API calls with very ease of use . So make APIs your last option . Anywasy , I wanted to write this little demo to get you started . Hope it's what you are looking for .
    VB Code:
    1. Protected Overrides Sub OnPaint(ByVal e As _
    2. System.Windows.Forms.PaintEventArgs)
    3.         'Device Context
    4.         Dim dc As Graphics = e.Graphics
    5.  
    6.         'The string we are going to write
    7.         Dim drawString As String = "Pirate"
    8.  
    9.         'Font we'll use
    10.         Dim drawFont As New Font("Arial", 20, FontStyle.Bold)
    11.  
    12.         'Brush used to write the text supported with
    13.         'different colors
    14.         Dim myBrush As New Drawing2D.LinearGradientBrush(ClientRectangle, _
    15.         Color.Yellow, Color.Blue, Drawing2D.LinearGradientMode.Horizontal)
    16.  
    17.         'Check FormatFlags Enums to see different drawing styles
    18.         'you can use.
    19.         Dim drawFormat As New StringFormat()
    20.         drawFormat.FormatFlags = StringFormatFlags.NoFontFallback
    21.  
    22.         'this explain itself :)
    23.         Dim x As Integer = 150
    24.         Dim y As Integer = 50
    25.  
    26.         'Finally we apply the Drwaing on the form .
    27.         dc.DrawString(drawString, drawFont, myBrush, _
    28.         x, y, drawFormat)
    29.         'Enjoy
    30.     End Sub

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Sep 2001
    Location
    Canada
    Posts
    202
    Thanks for the example. I actually, had good success with creating a Graphics object from the hdc (Device Context). I was able to reduce nearly 60 lines of code to 30 in one procedure! And I've got 5 more similar functions that draw pie graphs and stacked bar graphs yet! Hey .NET is awsome! :-)

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