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
Printable View
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
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:
Public Sub FromHdcHdc(e As PaintEventArgs) ' Get handle to device context. Dim hdc As IntPtr = e.Graphics.GetHdc() ' Create new graphics object using handle to device context. Dim newGraphics As Graphics = Graphics.FromHdc(hdc) ' Draw rectangle to screen. newGraphics.DrawRectangle(New Pen(Color.Red, 3), 0, 0, 200, 100) ' Release handle to device context. e.Graphics.ReleaseHdc(hdc) End Sub
For fonts, the Font object exposes a .ToHfont method:
ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfSystemDrawingFontClassToHfontTopic.htm
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:
Dim gpxResult As Graphics = Graphics.FromHdc(hDC)
Thanks,
Lloyd
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.
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:
Protected Overrides Sub OnPaint(ByVal e As _ System.Windows.Forms.PaintEventArgs) 'Device Context Dim dc As Graphics = e.Graphics 'The string we are going to write Dim drawString As String = "Pirate" 'Font we'll use Dim drawFont As New Font("Arial", 20, FontStyle.Bold) 'Brush used to write the text supported with 'different colors Dim myBrush As New Drawing2D.LinearGradientBrush(ClientRectangle, _ Color.Yellow, Color.Blue, Drawing2D.LinearGradientMode.Horizontal) 'Check FormatFlags Enums to see different drawing styles 'you can use. Dim drawFormat As New StringFormat() drawFormat.FormatFlags = StringFormatFlags.NoFontFallback 'this explain itself :) Dim x As Integer = 150 Dim y As Integer = 50 'Finally we apply the Drwaing on the form . dc.DrawString(drawString, drawFont, myBrush, _ x, y, drawFormat) 'Enjoy End Sub
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! :-)