抗锯齿方式画圆!

抗锯齿方式画圆!

http://blog.csdn.net/laviewpbt/article/details/1568060

Private Declare Function GetPixel Lib "gdi32" (ByVal hDC As Long, ByVal X As Long, ByVal Y As Long) As Long
Private Declare Function SetPixel Lib "gdi32" (ByVal hDC As Long, ByVal X As Long, ByVal Y As Long, ByVal crColor As Long) As Long
'
Private Const PI = 3.1415926 / 180

Private Sub PsetEx(Pic As PictureBox, X As Single, Y As Single, Optional Color As Long = 0)
    Dim CenterX As Long, CenterY As Long, CenterColor As Long, CenterAlpha As Single
    Dim CenterRX As Long, CenterRY As Long, CenterRColor As Long, CenterRAlpha As Single
    Dim CenterBX As Long, CenterBy As Long, CenterBColor As Long, CenterBAlpha As Single
    Dim CenterRBX As Long, CenterRBY As Long, CenterRBColor As Long, CenterRBAlpha As Single
    Dim hDC As Long
    hDC = Pic.hDC
    CenterX = Int(X)
    CenterY = Int(Y)
    CenterColor = GetPixel(hDC, CenterX, CenterY)
    CenterAlpha = (CenterX - X + 1) * (CenterY - Y + 1)
    CenterRX = CenterX + 1
    CenterRY = Int(Y)
    CenterRColor = GetPixel(hDC, CenterRX, CenterRY)
    CenterRAlpha = (X - CenterRX + 1) * (CenterY - Y + 1)
    CenterBX = CenterX
    CenterBy = CenterY + 1
    CenterBColor = GetPixel(hDC, CenterBX, CenterBy)
    CenterBAlpha = (CenterX - X + 1) * (Y - CenterBy + 1)
    CenterRBX = CenterX + 1
    CenterRBY = CenterY + 1
    CenterRBColor = GetPixel(hDC, CenterRBX, CenterRBY)
    CenterRBAlpha = (X - CenterRX + 1) * (Y - CenterBy + 1)
    SetPixel hDC, CenterX, CenterY, BlendColor(CenterColor, Color, CenterAlpha)
    SetPixel hDC, CenterRX, CenterRY, BlendColor(CenterRColor, Color, CenterRAlpha)
    SetPixel hDC, CenterBX, CenterBy, BlendColor(CenterBColor, Color, CenterBAlpha)
    SetPixel hDC, CenterRBX, CenterRBY, BlendColor(CenterRBColor, Color, CenterRBAlpha)
End Sub
'

Private Sub GetRGB(Color As Long, Red As Long, Green As Long, Blue As Long)
    If Color <> 0 Then
        Red = Color And 255&
        Green = Color / 256 And 255
        Blue = Color / 65536
    End If
End Sub

 

Private Function BlendColor(ColorBase As Long, ColorBlend As Long, Alpha As Single) As Long
    Dim BaseR As Long, BaseG As Long, BaseB As Long
    Dim BlendR As Long, BlendG As Long, BlendB As Long
    GetRGB ColorBase, BaseR, BaseG, BaseB
    GetRGB ColorBlend, BlendR, BlendG, BlendB
    If Alpha > 1 Then Alpha = 1
    BaseR = Alpha * (BlendR - BaseR) + BaseR
    BaseG = Alpha * (BlendG - BaseG) + BaseG
    BaseB = Alpha * (BlendB - BaseB) + BaseB
    BlendColor = RGB(BaseR, BaseG, BaseB)
End Function
'

Private Sub CircleEx(Pic As PictureBox, CenterX As Long, CenterY As Long, Radius As Long, Optional Color As Long = vbRed)
    Dim i As Single
    Dim X As Single, Y As Single
    For i = 0 To 360 Step 60 / Radius
        X = Cos(i * PI)
        Y = Sin(i * PI)
        X = X * Radius + CenterX
        Y = Y * Radius + CenterY
        PsetEx Pic, X, Y, Color
    Next
End Sub


Private Sub Command1_Click()
    CircleEx Pic, 100, 100, 50, vbRed
    Pic.Refresh
End Sub
 

 

以这种方式画抗锯齿的直线时要注意对于斜率大于和小于1的情况要分开处理,这里不赘述!

如果你觉得画出来的圆颜色和指定的颜色有所区别,请适当修改PsetEx函数的最后四行的SetPixel的最后一个透明度的参数,比如都乘2!.

 


猜你喜欢

转载自blog.csdn.net/lihongmao5911/article/details/44031501