N
NickP
Hi there,
I am trying to use GradientFillRect in a VB.NET application.
Does anyone have any examples of the correct PInvoke signature and
marshaling that I need to perform in order to achieve this?
BTW, I do not want to use .Nets gradient fill capabilities, and so far I
have this.
----------------------------------------------
Private Const GRADIENT_FILL_RECT_H As Long = &H0
Private Const GRADIENT_FILL_RECT_V As Long = &H1
<StructLayout(LayoutKind.Sequential)> _
Public Structure TRIVERTEX
Public x As Int32
Public y As Int32
Public Red As Int16
Public Green As Int16
Public Blue As Int16
Public Alpha As Int16
End Structure
<StructLayout(LayoutKind.Sequential)> _
Public Structure GRADIENT_RECT
Public UpperLeft As Integer
Public LowerRight As Integer
End Structure
<DllImport("msimg32.dll", EntryPoint:="GradientFill")> _
Public Shared Function GradientFillRect(ByVal hdc As IntPtr, _
ByVal pVertex As IntPtr, _
ByVal dwNumVertex As Integer, _
ByRef pMesh As GRADIENT_RECT, _
ByVal dwNumMesh As Integer, _
ByVal dwMode As Long) As Integer
End Function
Private Sub doGradientFill(ByVal iGraphics As Graphics, _
ByVal iSize As Size)
'//--------------------------------------------------
With iGraphics
Dim pTVxVert(2) As TRIVERTEX
Dim pGRtRect As GRADIENT_RECT
pTVxVert(0).x = 0
pTVxVert(0).y = 0
pTVxVert(0).Red = (Color.Green.R)
pTVxVert(0).Green = (Color.Green.G)
pTVxVert(0).Blue = (Color.Green.B)
pTVxVert(0).Alpha = (Color.Green.A)
pTVxVert(1).x = iSize.Width
pTVxVert(1).y = iSize.Height
pTVxVert(1).Red = (Color.Red.R)
pTVxVert(1).Green = (Color.Red.G)
pTVxVert(1).Blue = (Color.Red.B)
pTVxVert(1).Alpha = (Color.Red.A)
pGRtRect.UpperLeft = 0
pGRtRect.LowerRight = 1
Dim pIPrVert As IntPtr =
Marshal.AllocHGlobal(Marshal.SizeOf(pTVxVert))
Call Marshal.StructureToPtr(pTVxVert, pIPrVert, False)
Dim pIPrHDC As IntPtr = iGraphics.GetHdc()
GradientFillRect(pIPrHDC, pIPrVert, 2, pGRtRect, 1,
GRADIENT_FILL_RECT_H)
'GradientFillRect(pIPrHDC, pTVxVert, 2, pGRtRect, 1,
GRADIENT_FILL_RECT_V)
Call iGraphics.ReleaseHdc(pIPrHDC)
End With
End Sub
----------------------------------------------
Unfortunately this does not work as pTVxVert is an array and the size
cannot be calculated correctly. Any ideas?
Many thanks in advance.
Nick.
I am trying to use GradientFillRect in a VB.NET application.
Does anyone have any examples of the correct PInvoke signature and
marshaling that I need to perform in order to achieve this?
BTW, I do not want to use .Nets gradient fill capabilities, and so far I
have this.
----------------------------------------------
Private Const GRADIENT_FILL_RECT_H As Long = &H0
Private Const GRADIENT_FILL_RECT_V As Long = &H1
<StructLayout(LayoutKind.Sequential)> _
Public Structure TRIVERTEX
Public x As Int32
Public y As Int32
Public Red As Int16
Public Green As Int16
Public Blue As Int16
Public Alpha As Int16
End Structure
<StructLayout(LayoutKind.Sequential)> _
Public Structure GRADIENT_RECT
Public UpperLeft As Integer
Public LowerRight As Integer
End Structure
<DllImport("msimg32.dll", EntryPoint:="GradientFill")> _
Public Shared Function GradientFillRect(ByVal hdc As IntPtr, _
ByVal pVertex As IntPtr, _
ByVal dwNumVertex As Integer, _
ByRef pMesh As GRADIENT_RECT, _
ByVal dwNumMesh As Integer, _
ByVal dwMode As Long) As Integer
End Function
Private Sub doGradientFill(ByVal iGraphics As Graphics, _
ByVal iSize As Size)
'//--------------------------------------------------
With iGraphics
Dim pTVxVert(2) As TRIVERTEX
Dim pGRtRect As GRADIENT_RECT
pTVxVert(0).x = 0
pTVxVert(0).y = 0
pTVxVert(0).Red = (Color.Green.R)
pTVxVert(0).Green = (Color.Green.G)
pTVxVert(0).Blue = (Color.Green.B)
pTVxVert(0).Alpha = (Color.Green.A)
pTVxVert(1).x = iSize.Width
pTVxVert(1).y = iSize.Height
pTVxVert(1).Red = (Color.Red.R)
pTVxVert(1).Green = (Color.Red.G)
pTVxVert(1).Blue = (Color.Red.B)
pTVxVert(1).Alpha = (Color.Red.A)
pGRtRect.UpperLeft = 0
pGRtRect.LowerRight = 1
Dim pIPrVert As IntPtr =
Marshal.AllocHGlobal(Marshal.SizeOf(pTVxVert))
Call Marshal.StructureToPtr(pTVxVert, pIPrVert, False)
Dim pIPrHDC As IntPtr = iGraphics.GetHdc()
GradientFillRect(pIPrHDC, pIPrVert, 2, pGRtRect, 1,
GRADIENT_FILL_RECT_H)
'GradientFillRect(pIPrHDC, pTVxVert, 2, pGRtRect, 1,
GRADIENT_FILL_RECT_V)
Call iGraphics.ReleaseHdc(pIPrHDC)
End With
End Sub
----------------------------------------------
Unfortunately this does not work as pTVxVert is an array and the size
cannot be calculated correctly. Any ideas?
Many thanks in advance.
Nick.