在VB.NET中,LinearGradientBrush多容易实现。请问在VB中要多少Codes实现?
Dim myRectangle As Rectangle = e.Bounds
myRectangle.Inflate(0, 1)
Dim myBrush As LinearGradientBrush
If a = 1 Then
myBrush = New LinearGradientBrush(myRectangle, Color.FromArgb(200, 254, 230, 196), Color.FromArgb(150, 255, 183, 135), 90)
Else
myBrush = New LinearGradientBrush(myRectangle, Color.FromArgb(150, 255, 237, 212), Color.FromArgb(200, 251, 205, 174), 90)
End If
Try
e.Graphics.FillRectangle(myBrush, e.Bounds)
Finally
myBrush.Dispose()
End Try
Dim myRectangle As Rectangle = e.Bounds
myRectangle.Inflate(0, 1)
Dim myBrush As LinearGradientBrush
If a = 1 Then
myBrush = New LinearGradientBrush(myRectangle, Color.FromArgb(200, 254, 230, 196), Color.FromArgb(150, 255, 183, 135), 90)
Else
myBrush = New LinearGradientBrush(myRectangle, Color.FromArgb(150, 255, 237, 212), Color.FromArgb(200, 251, 205, 174), 90)
End If
Try
e.Graphics.FillRectangle(myBrush, e.Bounds)
Finally
myBrush.Dispose()
End Try
Gradient Colors in GDI+
In programming, gradient is a transition color from a start point to end point. In my past with VB6, I have to modify each pixel to make a gradient within a container. Now with VB2005, you can make a gradient on a surface with a few lines of codes.
这就是大家要赶快转型到.NET地原因之一。
在VB中大家纠缠在细枝末叶上,而.NET有强大的库。
如果作为学习者来说的话,的确VB.net更加结构化,库也很强大。再加上职位也多些
但是对于已经工作的人来说了,转型与否也是与目前的工程来说的。比我的工程就是一个从VB6到Vb.net的升级过程。原有VB6的东西还需要维护和完善,升级过程是一个缓慢的螺旋进展的过程。
而VB未经封装,所以你觉得多
你可以自己做一个相同效果的类,然后在程序里调用,那么你会发现,原来代码也这么“少”
你可以自己做一个相同效果的类
是的,可以。
但.NET是直接调用MS (Windows的娘家人)做的库,可靠性更高吧。
3年前,我每次重新安装WinXP后,安装Fish's .NET Code Library时总是要求我下载.NET Framework,现在我的电脑
再安装它时,提示已安装了!
AutoCAD 2008
Acrobat pro 8
RSLogix5000 V16
Siemens s7
Wonderware Intouch 10
Wincc
Photoshop
Norton 2008
我当然装了.net framework,但那是vb.net 2005附带的,如果你写的程序都是为了自己使用,不存在发布的问题,那么我说的情况你当然不必考虑。
Private Type COLORBYTES
BlueByte As Byte
GreenByte As Byte
RedByte As Byte
AlphaByte As Byte
End TypePrivate Type COLORLONG
longval As Long
End TypePrivate Type GdiplusStartupInput
GdiplusVersion As Long
DebugEventCallback As Long
SuppressBackgroundThread As Long
SuppressExternalCodecs As Long
End TypePrivate Enum WrapMode
WrapModeTile
WrapModeTileFlipX
WrapModeTileFlipY
WrapModeTileFlipXY
WrapModeClamp
End EnumPrivate Type RECTF ' aka RectF
Left As Single
Top As Single
Right As Single
Bottom As Single
End TypePrivate Enum LinearGradientMode
LinearGradientModeHorizontal ' 0
LinearGradientModeVertical ' 1
LinearGradientModeForwardDiagonal ' 2
LinearGradientModeBackwardDiagonal ' 3
End EnumPrivate Declare Function GdipDeleteBrush Lib "gdiplus" (ByVal Brush As Long) As Long
Private Declare Function GdiplusStartup Lib "gdiplus" (token As Long, inputbuf As GdiplusStartupInput, Optional ByVal outputbuf As Long = 0) As Long
Private Declare Sub GdiplusShutdown Lib "gdiplus" (ByVal token As Long)
Private Declare Function GdipCreateFromHDC Lib "gdiplus" (ByVal hDC As Long, Graphics As Long) As Long
Private Declare Function GdipDeleteGraphics Lib "gdiplus" (ByVal Graphics As Long) As Long
Private Declare Function GdipFillRectangle Lib "gdiplus" (ByVal Graphics As Long, ByVal Brush As Long, ByVal x As Single, ByVal y As Single, ByVal Width As Single, ByVal Height As Single) As Long
Private Declare Function GdipCreateLineBrushFromRect Lib "gdiplus" (Rect As RECTF, ByVal color1 As Long, ByVal color2 As Long, ByVal Mode As LinearGradientMode, ByVal WrapMd As WrapMode, lineGradient As Long) As Long
Public Sub DrawGradient(hDC As Long)
Dim token As Long
Dim GSI As GdiplusStartupInput
Dim Graphics As Long
Dim Brush As Long
Dim Rect As RECTF
Dim BlendColor(1) As Long
BlendColor(0) = ColorARGB(255, 255, 0, 34)
BlendColor(1) = ColorARGB(255, 0, 255, 123)
Rect.Left = 0
Rect.Top = 0
Rect.Right = 400
Rect.Bottom = 500
GSI.GdiplusVersion = 1
GdiplusStartup token, GSI
GdipCreateFromHDC hDC, Graphics
GdipCreateLineBrushFromRect Rect, BlendColor(0), BlendColor(1), LinearGradientModeBackwardDiagonal, WrapModeTile, Brush
GdipFillRectangle Graphics, Brush, 0, 0, 400, 500
GdipDeleteBrush Brush
GdipDeleteGraphics Graphics
GdiplusShutdown token
End SubPrivate Sub Command1_Click()
DrawGradient Me.hDC
Me.RefreshEnd SubPublic Function ColorARGB(ByVal alpha As Byte, ByVal Red As Byte, ByVal Green As Byte, ByVal Blue As Byte) As Long
Dim bytestruct As COLORBYTES
Dim result As COLORLONG With bytestruct
.AlphaByte = alpha
.RedByte = Red
.GreenByte = Green
.BlueByte = Blue
End With LSet result = bytestruct
ColorARGB = result.longval
End Function类似的代码,你自己改下就可以满足你的要求。
tRectDst As RECT, _
ByVal eBorder As BorderStyle, _
ByVal eFill As FillStyle, _
ByVal crBorder As Long, _
ByVal crFill1 As Long, _
Optional ByVal crFill2 As Long, _
Optional ByVal byFillAlpha As Byte = 255) As Boolean
Dim hDCSrc As Long, hDCMask As Long
Dim hBmpSrc As Long, hBmpOld1 As Long
Dim hBmpMask As Long, hBmpOld2 As Long
Dim hPen As Long, hPenOld As Long
Dim hBrush As Long, hBrushOld As Long
Dim tRect As RECT
Dim tPoint(2) As POINTAPI
Dim tBlend As BLENDFUNCTION
Dim lpBlend As Long
Dim tVertex(3) As TRIVERTEX
Dim tGRect(1) As GRADIENT_RECT
Dim nBorderWidth As Long
'边框
Select Case eBorder
Case BS_NONE
Case BS_RAISED, BS_SUNKEN
Call DrawEdge(hDCDst, tRectDst, eBorder, BF_RECT)
Case BS_ETCHED, BS_BUMP
Call DrawEdge(hDCDst, tRectDst, eBorder, BF_RECT)
nBorderWidth = nBorderWidth + 1
Case Else
hPen = CreatePen(Choose(eBorder And &HF, PS_SOLID, PS_DASH, PS_DOT), 1, crBorder)
hBrush = GetStockObject(NULL_BRUSH)
hPenOld = SelectObject(hDCDst, hPen)
hBrushOld = SelectObject(hDCDst, hBrush)
If CBool(eBorder And BS_ROUNDRECT) Then
Call RoundRect(hDCDst, tRectDst.Left, tRectDst.Top, tRectDst.Right, tRectDst.Bottom, 6, 6)
Else
Call Rectangle(hDCDst, tRectDst.Left, tRectDst.Top, tRectDst.Right, tRectDst.Bottom)
End If
Call DeleteObject(SelectObject(hDCDst, hPenOld))
Call SelectObject(hDCDst, hBrushOld)
End Select
nBorderWidth = nBorderWidth + 1
'填充 - - - - - - - - - - - - - - - - - - - - -
'创建目标 DC 副本
Call SetRect(tRect, 0, 0, tRectDst.Right - tRectDst.Left, tRectDst.Bottom - tRectDst.Top)
hDCSrc = CreateCompatibleDC(hDCDst)
hBmpSrc = CreateCompatibleBitmap(hDCDst, tRect.Right - tRect.Left, tRect.Bottom - tRect.Top)
hBmpOld1 = SelectObject(hDCSrc, hBmpSrc)
Call BitBlt(hDCSrc, 0, 0, tRect.Right - tRect.Left, tRect.Bottom - tRect.Top, hDCDst, tRectDst.Left, tRectDst.Top, vbSrcCopy)
'填充此副本
Call InflateRect(tRect, -nBorderWidth, -nBorderWidth)
hDCMask = CreateCompatibleDC(hDCDst)
hBmpMask = CreateCompatibleBitmap(hDCDst, tRect.Right - tRect.Left, tRect.Bottom - tRect.Top)
hBmpOld2 = SelectObject(hDCMask, hBmpMask)
Call OffsetRect(tRect, -nBorderWidth, -nBorderWidth)
Select Case eFill
Case FS_NONE
byFillAlpha = 0
Case FS_SOLIDCOLOR
hBrush = CreateSolidBrush(crFill1)
Call FillRect(hDCMask, tRect, hBrush)
Call DeleteObject(hBrush)
Case FS_GRADIENT_H, FS_GRADIENT_V
With tVertex(0)
.x = tRect.Left
.y = tRect.Top
.red = GetRedGB16(crFill1)
.green = GetRGreenB16(crFill1)
.blue = GetRGBlue16(crFill1)
.alpha = 0
End With
With tVertex(1)
.x = tRect.Right
.y = tRect.Bottom
.red = GetRedGB16(crFill2)
.green = GetRGreenB16(crFill2)
.blue = GetRGBlue16(crFill2)
.alpha = 0
End With
tGRect(0).UpperLeft = 0
tGRect(0).LowerRight = 1
Call GradientFill(hDCMask, tVertex(0), 2, tGRect(0), 1, Choose(eFill \ 2, GRADIENT_FILL_RECT_H, GRADIENT_FILL_RECT_V))
Case Else '双矩形填充
With tVertex(0)
.x = tRect.Left
.y = tRect.Top
.red = GetRedGB16(crFill1)
.green = GetRGreenB16(crFill1)
.blue = GetRGBlue16(crFill1)
.alpha = 0
End With
With tVertex(3)
.x = tRect.Right
.y = tRect.Bottom
.red = GetRedGB16(crFill1)
.green = GetRGreenB16(crFill1)
.blue = GetRGBlue16(crFill1)
.alpha = 0
End With
With tVertex(1)
'.x = (pending)
'.y = (pending)
.red = GetRedGB16(crFill2)
.green = GetRGreenB16(crFill2)
.blue = GetRGBlue16(crFill2)
.alpha = 0
End With
With tVertex(2)
'.x = (pending)
'.y = (pending)
.red = GetRedGB16(crFill2)
.green = GetRGreenB16(crFill2)
.blue = GetRGBlue16(crFill2)
.alpha = 0
End With
tGRect(0).UpperLeft = 0
tGRect(0).LowerRight = 1
tGRect(1).UpperLeft = 2
tGRect(1).LowerRight = 3
Select Case (eFill And Not FS_MIRROREFFECT)
Case FS_GRADIENT_H
tVertex(1).x = (tRect.Right - tRect.Left) \ 2
tVertex(1).y = tRect.Bottom
tVertex(2).x = (tRect.Right - tRect.Left) \ 2
tVertex(2).y = tRect.Top
Call GradientFill(hDCMask, tVertex(0), 4, tGRect(0), 2, GRADIENT_FILL_RECT_H)
Case FS_GRADIENT_V
tVertex(1).x = tRect.Right
tVertex(1).y = (tRect.Bottom - tRect.Top) \ 2
tVertex(2).x = tRect.Left
tVertex(2).y = (tRect.Bottom - tRect.Top) \ 2
Call GradientFill(hDCMask, tVertex(0), 4, tGRect(0), 2, GRADIENT_FILL_RECT_V)
End Select
End Select
Call OffsetRect(tRect, nBorderWidth, nBorderWidth)
With tBlend
.BlendOp = AC_SRC_OVER
.BlendFlags = 0
.SourceConstantAlpha = byFillAlpha
.AlphaFormat = 0
End With
Call CopyMemory(lpBlend, tBlend, 4)
Call AlphaBlend(hDCSrc, tRect.Left, tRect.Top, tRect.Right - tRect.Left, tRect.Bottom - tRect.Top, hDCMask, 0, 0, tRect.Right - tRect.Left, tRect.Bottom - tRect.Top, lpBlend)
Call DeleteObject(SelectObject(hDCMask, hBmpOld2))
Call DeleteDC(hDCMask)
Call InflateRect(tRect, nBorderWidth, nBorderWidth)
'位块传送到目标 DC
If CBool(eBorder And BS_ROUNDRECT) Then
'画 Mask 位图
hDCMask = CreateCompatibleDC(hDCDst)
hBmpMask = CreateCompatibleBitmap(hDCMask, tRect.Right - tRect.Left, tRect.Bottom - tRect.Top)
hBmpOld2 = SelectObject(hDCMask, hBmpMask)
hBrush = GetStockObject(BLACK_BRUSH)
Call FillRect(hDCMask, tRect, hBrush)
hPen = GetStockObject(BLACK_PEN)
hBrush = GetStockObject(WHITE_BRUSH)
Call SaveDC(hDCMask)
Call SelectObject(hDCMask, hPen)
Call SelectObject(hDCMask, hBrush)
Call RoundRect(hDCMask, 0, 0, tRect.Right - tRect.Left, tRect.Bottom - tRect.Top, 6, 6)
Call RestoreDC(hDCMask, -1)
Call SelectObject(hDCMask, hBmpOld2)
Call DeleteDC(hDCMask)
'调用 PlgBlt 用 Mask 位图控制透明部分
tPoint(0).x = tRectDst.Left: tPoint(0).y = tRectDst.Top
tPoint(1).x = tRectDst.Right: tPoint(1).y = tRectDst.Top
tPoint(2).x = tRectDst.Left: tPoint(2).y = tRectDst.Bottom
StyleDrawFill = PlgBlt(hDCDst, tPoint(0), hDCSrc, 0, 0, tRect.Right - tRect.Left, tRect.Bottom - tRect.Top, hBmpMask, 0, 0)
Call DeleteObject(hBmpMask)
Else
StyleDrawFill = BitBlt(hDCDst, tRectDst.Left, tRectDst.Top, tRectDst.Right - tRectDst.Left, tRectDst.Bottom - tRectDst.Top, hDCSrc, 0, 0, vbSrcCopy)
End If
'删除对象
Call DeleteObject(SelectObject(hDCSrc, hBmpOld1))
Call DeleteDC(hDCSrc)
' - - - - - - - - - - - - - - - - - - - - - - - -
End Function
你试试看在VB和.NET中,Gradient的效果好像不一样。差别在于Color.FromArgb (Object) VS ColorARGB (Long)???myBrush = New LinearGradientBrush(myRectangle, Color.FromArgb(200, 254, 230, 196), Color.FromArgb(150, 255, 183, 135), 90)