Results 1 to 9 of 9

Thread: hDC in VB.Net

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2004
    Posts
    2

    hDC in VB.Net

    does any one knwo how to get this value in Vb.Net?

    and is this value large for an object in vb6?

  2. #2
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: hDC in VB.Net

    Form1.DefInstance.Handle.ToInt32

    Yes since you are using ToInt32

  3. #3
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: hDC in VB.Net

    Shoot. My bad. It only works when VB6 Forms have been upgraded to VB.NET and have contained code on hDC's.

  4. #4
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: hDC in VB.Net

    Try doing this:

    Dim Form as New Control

    and use Form.Handle.Int32 for whereever hDC was needed.

  5. #5
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: hDC in VB.Net

    I was wrong. Handle is actually like VB6's hWnd. Sorry, but at least I tried.

  6. #6
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: hDC in VB.Net

    AH HA, here it is:

    Dim Form as New Form1
    Dim hDC as Long

    hdc = Form1.CreateGraphics.GetHdc.ToInt32

    GetHdc is a hidden member and won't appear in the list after typing CreateGraphics.

  7. #7
    New Member
    Join Date
    May 2011
    Posts
    15

    Re: hDC in VB.Net

    Thanks Jacob Roman from 8 years ago.
    hdc = Form1.CreateGraphics.GetHdc.ToInt32 Its exactely what I needed to get the handler of my form..

  8. #8
    New Member
    Join Date
    Feb 2012
    Posts
    7

    Re: hDC in VB.Net

    Just an FYI, Verzulsan from 1 month ago, there is no need to append ".ToInt32" to that since it is already an IntPtr. Also, be sure to use ".ReleaseHdc" afterwards, like so:

    vb Code:
    1. Dim g As Graphics = Form1.CreateGraphics()
    2. Dim hdc As IntPtr = g.GetHdc() ' Borrows control over the device context
    3. '
    4. ' Use the device context here...
    5. '
    6. g.ReleaseHdc()' Gives control back to owner
    This will pass ownership of the device context back to the original owner, allowing the graphics device to continue as it normally would.

  9. #9
    Addicted Member Mikle's Avatar
    Join Date
    Oct 2009
    Location
    Tuapse, Russia
    Posts
    139

    Re: hDC in VB.Net

    A more correct version:
    Code:
    Imports System.Runtime.InteropServices
    Public Class Form1
      Dim Grp As Graphics
      Dim hDC As HandleRef
    
      Private Sub Form1_HandleCreated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.HandleCreated
        Grp = Me.CreateGraphics
        hDC = New HandleRef(Grp, Grp.GetHdc)
      End Sub
    
      Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        DirectCast(hDC.Wrapper, Graphics).ReleaseHdc()
        Grp.Dispose()
      End Sub
    End Class

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