Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. How to do this C# unsafe pointer passing in VB.NET

How to do this C# unsafe pointer passing in VB.NET

Scheduled Pinned Locked Moved Visual Basic
csharpgraphicshelptutorial
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    the_warlord
    wrote on last edited by
    #1

    Hi folks, my problem is too dificult for me to parse. I need to translate this code snipet to VB.NET : public RECT SetTVItemRECTHandle(IntPtr hItem,RECT RC){ RECT rc1=new RECT(); unsafe { *(IntPtr*)&rc1 = hItem; } return rc1; } The part that bugs me and got me lost is the '*(IntPtr*)&rc1 = hItem;' thing. For now I'm relying on a C# function in a DLL to accomplish the work but I'd like to know if it can be done in VB so that I can discard this extra dependency from my solution. For your information, this function is used to retrieve the rectangle for a list view column header for owner drawing. :confused: Thanks :confused: The_Warlord

    D 2 Replies Last reply
    0
    • T the_warlord

      Hi folks, my problem is too dificult for me to parse. I need to translate this code snipet to VB.NET : public RECT SetTVItemRECTHandle(IntPtr hItem,RECT RC){ RECT rc1=new RECT(); unsafe { *(IntPtr*)&rc1 = hItem; } return rc1; } The part that bugs me and got me lost is the '*(IntPtr*)&rc1 = hItem;' thing. For now I'm relying on a C# function in a DLL to accomplish the work but I'd like to know if it can be done in VB so that I can discard this extra dependency from my solution. For your information, this function is used to retrieve the rectangle for a list view column header for owner drawing. :confused: Thanks :confused: The_Warlord

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      I could be wrong, but it looks like rc1 is being declared as a RECT class and a new object is being created. Then it changes the address of the rc1 class to point to a segment of memory that hItem is pointing to. As far as I know, VB doesn't support this. RageInTheMachine9532

      1 Reply Last reply
      0
      • T the_warlord

        Hi folks, my problem is too dificult for me to parse. I need to translate this code snipet to VB.NET : public RECT SetTVItemRECTHandle(IntPtr hItem,RECT RC){ RECT rc1=new RECT(); unsafe { *(IntPtr*)&rc1 = hItem; } return rc1; } The part that bugs me and got me lost is the '*(IntPtr*)&rc1 = hItem;' thing. For now I'm relying on a C# function in a DLL to accomplish the work but I'd like to know if it can be done in VB so that I can discard this extra dependency from my solution. For your information, this function is used to retrieve the rectangle for a list view column header for owner drawing. :confused: Thanks :confused: The_Warlord

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        Could you post the code that leads up to this? I'm looking for the code that generates the 'hItem' and 'RC' parameters so I can play with it and see what the possiblities are. Thanks, RageInTheMachine9532

        T 1 Reply Last reply
        0
        • D Dave Kreskowiak

          Could you post the code that leads up to this? I'm looking for the code that generates the 'hItem' and 'RC' parameters so I can play with it and see what the possiblities are. Thanks, RageInTheMachine9532

          T Offline
          T Offline
          the_warlord
          wrote on last edited by
          #4

          The context is custom draw a treeview, in particular a tree node. Here is the WndProc from my Component: Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message) Dim intLeft As Integer Dim RC As RECT Dim RC2 As CantDoInVBButInCSharp.RECT Dim RCF As RectangleF Dim nm1 As NMHDR Dim nmcd As NMTVCUSTOMDRAW Dim hNode As IntPtr Dim PU As New CantDoInVBButInCSharp.PointerUtils() If m.Msg = ReflectedMessages.OCM_NOTIFY Then nm1 = m.GetLParam((nm1.GetType)) If nm1.code = NotificationMessages.NM_CUSTOMDRAW Then nmcd = m.GetLParam(nmcd.GetType) If nmcd.nmcd.dwDrawStage = CustomDrawDrawStateFlags.CDDS_PREPAINT Then m.Result = New System.IntPtr(CustomDrawReturnFlags.CDRF_NOTIFYITEMDRAW) End If If nmcd.nmcd.dwDrawStage = CustomDrawDrawStateFlags.CDDS_ITEMPREPAINT Then hNode = New IntPtr(CInt(nmcd.nmcd.dwItemSpec.ToString)) RC2 = PU.SetTVItemRECTHandle(hNode, Nothing) SendMessage2(Handle, TreeViewMessages.TVM_GETITEMRECT, 1, RC2) mvarTextBrush = New LinearGradientBrush(New Rectangle(RC2.left, RC2.top, RC2.right - RC2.left, RC2.bottom - RC2.top), mvarTextGradientColor1, mvarTextGradientColor2, mvarGradientMode) Graphics.FromHdc(nmcd.nmcd.hdc).DrawString("Test", Me.Font, mvarTextBrush, New RectangleF(RC2.left, RC2.top, RC2.right - RC2.left, RC2.bottom - RC2.top)) hClickedItem = IntPtr.Zero m.Result = New System.IntPtr(CustomDrawReturnFlags.CDRF_SKIPDEFAULT) End If If nmcd.nmcd.dwDrawStage = CustomDrawDrawStateFlags.CDDS_ITEMPOSTPAINT Then End If ElseIf nm1.code = TreeViewNotifications.TVN_ITEMEXPANDEDW Then Me.Invalidate() m.Result = IntPtr.Zero ElseIf nm1.code = TreeViewNotifications.TVN_ITEMEXPANDINGW Then Me.Invalidate() m.Result = IntPtr.Zero ElseIf nm1.code = TreeViewNotifications.TVN_SELCHANGINGW Then Me.Invalidate() m.Result = IntPtr.Zero End If ElseIf m.Msg = Msg.WM_ERASEBKGND Then 'If Not (hClickedItem.Equals(IntPtr.Zero)) Then MyBase.CreateGraphics.FillRectangle(mvarBackBrush, ClientRec

          D 1 Reply Last reply
          0
          • T the_warlord

            The context is custom draw a treeview, in particular a tree node. Here is the WndProc from my Component: Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message) Dim intLeft As Integer Dim RC As RECT Dim RC2 As CantDoInVBButInCSharp.RECT Dim RCF As RectangleF Dim nm1 As NMHDR Dim nmcd As NMTVCUSTOMDRAW Dim hNode As IntPtr Dim PU As New CantDoInVBButInCSharp.PointerUtils() If m.Msg = ReflectedMessages.OCM_NOTIFY Then nm1 = m.GetLParam((nm1.GetType)) If nm1.code = NotificationMessages.NM_CUSTOMDRAW Then nmcd = m.GetLParam(nmcd.GetType) If nmcd.nmcd.dwDrawStage = CustomDrawDrawStateFlags.CDDS_PREPAINT Then m.Result = New System.IntPtr(CustomDrawReturnFlags.CDRF_NOTIFYITEMDRAW) End If If nmcd.nmcd.dwDrawStage = CustomDrawDrawStateFlags.CDDS_ITEMPREPAINT Then hNode = New IntPtr(CInt(nmcd.nmcd.dwItemSpec.ToString)) RC2 = PU.SetTVItemRECTHandle(hNode, Nothing) SendMessage2(Handle, TreeViewMessages.TVM_GETITEMRECT, 1, RC2) mvarTextBrush = New LinearGradientBrush(New Rectangle(RC2.left, RC2.top, RC2.right - RC2.left, RC2.bottom - RC2.top), mvarTextGradientColor1, mvarTextGradientColor2, mvarGradientMode) Graphics.FromHdc(nmcd.nmcd.hdc).DrawString("Test", Me.Font, mvarTextBrush, New RectangleF(RC2.left, RC2.top, RC2.right - RC2.left, RC2.bottom - RC2.top)) hClickedItem = IntPtr.Zero m.Result = New System.IntPtr(CustomDrawReturnFlags.CDRF_SKIPDEFAULT) End If If nmcd.nmcd.dwDrawStage = CustomDrawDrawStateFlags.CDDS_ITEMPOSTPAINT Then End If ElseIf nm1.code = TreeViewNotifications.TVN_ITEMEXPANDEDW Then Me.Invalidate() m.Result = IntPtr.Zero ElseIf nm1.code = TreeViewNotifications.TVN_ITEMEXPANDINGW Then Me.Invalidate() m.Result = IntPtr.Zero ElseIf nm1.code = TreeViewNotifications.TVN_SELCHANGINGW Then Me.Invalidate() m.Result = IntPtr.Zero End If ElseIf m.Msg = Msg.WM_ERASEBKGND Then 'If Not (hClickedItem.Equals(IntPtr.Zero)) Then MyBase.CreateGraphics.FillRectangle(mvarBackBrush, ClientRec

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #5

            Sorry, but I haven't been able to get a solution to this problem to work. I'm still working on it, but It's been slow since I've been moving my girlfriend! 8) RageInTheMachine9532

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups