PlgBlt(
HDC hdcDest, // handle of destination device context
CONST POINT * lpPoint, // vertices of destination parallelogram
HDC hdcSrc, // handle of source device context
int nXSrc, // x-coord. of upper-left corner of source rect.
int nYSrc, // y-coord. of upper-left corner of source rect.
int nWidth, // width of source rectangle
int nHeight, // height of source rectangle
HBITMAP hbmMask, // handle of bitmask
int xMask, // x-coord. of upper-left corner of bitmask rect.
int yMask // y-coord. of upper-left corner of bitmask
)
需2个Command Button 2个PictureBox,目的在Picture2画出旋转图
Option Explicit
Private Declare Function PlgBlt Lib "gdi32" (ByVal hdcDest As Long, _
lpPoint As POINTAPI, ByVal hdcSrc As Long, ByVal nXSrc As Long, _
ByVal nYSrc As Long, ByVal nWidth As Long, ByVal nHeight As Long, _
ByVal hbmMask As Long, ByVal xMask As Long, ByVal yMask As Long) As Long
Private Type POINTAPI
x As Long
y As Long
End Type
Const pi = 3.14159265358979
Dim dThetaDeg As Double
'以下只适用於NT
Private Sub Command1_Click()
Dim pt(1 To 3) As POINTAPI, p4 As POINTAPI
Dim dx As Long, dy As Long
Dim i As Long, offsetX As Long, offsetY As Long
Dim sida As Double
Dim MaxX As Long, MaxY As Long, MinX As Long, MinY As Long
sida = dThetaDeg * pi / 180
dx = Me.ScaleX(Picture1.Picture.Width, vbHimetric, vbPixels)
dy = Me.ScaleX(Picture1.Picture.Height, vbHimetric, vbPixels)
pt(1).x = dy * Sin(sida)
pt(1).y = dy - dy * Cos(sida)
pt(2).x = pt(1).x + dx * Cos(sida)
pt(2).y = pt(1).y + dx * Sin(sida)
pt(3).x = 0: pt(3).y = dy
i = PlgBlt(Picture2.hDC, pt(1), Picture1.hDC, 0, 0, dx, dy, 0, 0, 0)
End Sub
'这个方式不管95/NT都可行,来自vb5程式设计进阶指南一书,但速度慢
Private Sub Command2_Click()
Dim nX As Integer, nY As Integer
Dim nX1 As Integer, nY1 As Integer
Dim dX2 As Double, dY2 As Double
Dim dX3 As Double, dY3 As Double
Dim dThetaRad As Double
'Compute angle in radians
dThetaRad = -dThetaDeg * pi / 180
'Set scale modes to pixels
For nX = 0 To Picture2.ScaleWidth
nX1 = nX - Picture2.ScaleWidth \ 2
For nY = 0 To Picture2.ScaleHeight
nY1 = nY - Picture2.ScaleHeight \ 2
'Rotate picture by dThetaRad
dX2 = nX1 * Cos(-dThetaRad) + nY1 * Sin(-dThetaRad)
dY2 = nY1 * Cos(-dThetaRad) - nX1 * Sin(-dThetaRad)
'Translate to center of picture box
dX3 = dX2 + Picture1.ScaleWidth \ 2
dY3 = dY2 + Picture1.ScaleHeight \ 2
'If data point is in picOne, set its color in picTwo
If dX3 > 0 And dX3 < Picture1.ScaleWidth - 1 _
And dY3 > 0 And dY3 < Picture1.ScaleHeight - 1 Then
Picture2.PSet (nX, nY), Picture1.Point(dX3, dY3)
End If
Next nY
Next nX
End Sub
Private Sub Form_Load()
Picture1.ScaleMode = 3
Picture2.ScaleMode = 3
'设定旋转角度
dThetaDeg = 30
Set Picture1.Picture = LoadPicture("e:\cli.bmp")