How to do this C# unsafe pointer passing in VB.NET
-
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
-
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
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
-
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
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
-
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
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
-
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
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