用VB5制作家庭影集
你想擁有自己的電子家庭影集嗎?其實用VB就能實現。方法如下:
準備:家庭普通照片經掃描后儲存。
構想:照片一張接一張出現在屏幕中間,出現時的方式采取動態切換,上方一行標題從左向右移過,標題與照片的背景色隨機變化。
關鍵:調用Bitblt Windows API函數實現照片的動態切換。
內容:工程由Form1和modlue1構成。
Form1中的內容如下:
| Const bmpfilemax = 11 ′照片數目常量 Dim bmpfile(bmpfilemax) As String ′照片的文件組 Dim drawbmpmode(bmpfilemax) As Integer '照片的切換方式 Dim bmpnum, movestep, xmax, ymax, endmax, lleft, r, n As Integer ′照片的序號、步進參數等 Dim kxy As Single ′x、y方向的比例 Private Sub Exit_Click() End End Sub Private Sub Form_Load() Label1.Left = 0 Label1.Caption = ″Family Album″ Picture1.AutoSize = True Picture1.Visible = False bmpfile(0) = App.Path + ″ son1.jpg″ bmpfile(1) = App.Path + ″mom_son1.jpg″ bmpfile(2) = App.Path + ″daddy-son.jpg″ bmpfile(3) = App.Path + ″yu99yantai.jpg″ bmpfile(4) = App.Path + ″yu98singap2.jpg″ bmpfile(5) = App.Path + ″yu98singapore.jpg″ bmpfile(6) = App.Path + ″mom_son2.jpg″ bmpfile(7) = App.Path + ″yu99yan2.jpg″ bmpfile(8) = App.Path + ″family.jpg″ bmpfile(9) = App.Path + ″fan-yantai.jpg″ bmpfile(10) = App.Path + ″yu99yan3.jpg″ drawbmpmode(bmpnum) = 1 + Int(Rnd() * 4) movestep = 0 ′步進參數 xmax = Form1.ScaleWidth / ymax = Form1.ScaleHeight / kxy = ymax / xmax Picture1.Picture = LoadPicture(bmpfile(bmpnum)) Timer1.Interval = 30 End Sub Private Sub Timer1_Timer() m = Form1.ScaleWidth / - Picture1.Width / ′照片顯示結束時的X方向居中定位 n = Form1.ScaleHeight / - Picture1.Height / ′照片顯示結束時的Y方向居中定位 hDestDC = Form1.hDC hSrcDC = Picture1.hDC drawflag = drawbmpmode(bmpnum) ′照片顯示時的切換方式 Select Case drawflag Case 1 ′切換方式為從左右向中間進行 Timer1.Interval = 30 endmax = xmax w = movestep h = Picture1.Height i = BitBlt(hDestDC, 0 + m, 0 + n, w, h, hSrcDC, 0, 0, SRCCOPY) X1 = Picture1.Width - movestep i = BitBlt(hDestDC, X1 + m, 0 + n, w, h, hSrcDC, X1, 0, SRCCOPY) Case 2 ′切換方式為從中間向四周擴散進行 Timer1.Interval = 30 endmax = xmax X1 = xmax - movestep w = movestep * 2 Y1 = CInt(ymax - movestep * kxy) h = CInt(2 * movestep) i = BitBlt(hDestDC, X1 + m, Y1 + n, w, h, hSrcDC, X1, Y1, SRCCOPY) Case 3 ′切換方式為柵欄翻轉進行 Timer1.Interval = 200 ednmax = CInt(2 * xmax / 10) tempi = CInt(2 * xmax / 10) w = movestep h = Picture1.ScaleHeight For ij = 0 To 9 i = BitBlt(hDestDC, tempi * ij + m, 0 + n, w, h, hSrcDC, tempi * ij, 0, SRCCOPY) Next ij Case 4 ′切換方式為從左向右進行 Timer1.Interval = 30 endmax = xmax w = movestep * 2 h = Form1.ScaleHeight i = BitBlt(hDestDC, 0 + m, 0 + n, w, h, hSrcDC, X1, Y1, SRCCOPY) End Select Form1.Refresh movestep = movestep + 4 Label1.Left = movestep If movestep > endmax + 60 Then bmpnum = bmpnum + 1 If bmpnum >= bmpfilemax Then bmpnum = 0 End If Cls movestep = 0 Picture1.Picture = LoadPicture(bmpfile(bmpnum)) drawbmpmode(bmpnum) = 1 + Int(Rnd() * 4) BackColor = QBColor(Rnd * 15) Label1.ForeColor = QBColor(Rnd * 10) If BackColor = Label1.ForeColor Then Label1.ForeColor = vbBlack End If Label1.Caption = ″Family Album″ Label1.Top = Picture1.Top End If End Sub Modlue1中的內容如下: Option Explicit Public Const SRCCOPY = &HCC0020 ' (DWORD) dest = source Declare Function BitBlt Lib ″gdi32″ (ByVal hDestDC As _ Long, ByVal x As Long, ByVal Y As Long, ByVal nWidth _ As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, _ ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long |