開機,Start Window...桌面出現了,天天看著這個單調的桌面會不會覺得無聊。換個桌布,還是沒勁。對了,把桌面倒過來瞧瞧?怎么才能倒過來呢?用VB5提供的PaintPicture函數就可以。
PaintPicture函數的功能能把指定Picturebox控件上的圖片復制到窗體上。
paintpicture函數格式如下:
object.PaintPicture picture, x1, y1, width1, height1, x2, y2, width2, height2, opcode
object 目的對象,如果省略object,則帶有焦點的 Form 對象缺省為 object
picture 要繪制到 object 上的圖形源,Form 或 PictureBox 必須是 Picture 屬性。
x1,y1 在指定 object 上繪制 picture 的目標坐標(x-軸和y-軸)
width1,height1 表示目標寬度和高度
x2,y2 指示 picture 內剪貼區的坐標
width2,heigh2 指示 picture 內剪貼區的源高度和源寬度
opcode 它用來定義在將 picture 繪制到 object 上時對 picture 執行的位操作 ,可省略。
假如我們要復制的圖片的長、寬分別為width和height時,將x1設為width,y1為0,width1為-width,height1等于height,其他不變,則復制后圖片將左右對調。同理我們可以將圖片上下對調或者上下左右都對調。
但是要讓屏幕翻起來,我們還要解決一個問題。因為PaintPicture函數只能對bmp圖進行操作,所以它不能直接對屏幕進行操作。那只好先將屏幕抓下來存成bmp圖,再對bmp圖進行操作。以下就是源程序,需要一個Picturebox 控件,將AutoRedraw屬性設為true,form的borderstyle屬性設為none
| Option Explicit Private Declare Function GetDC Lib “user32" (ByVal hwnd As Long) As Long Private 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 Private Declare Function DeleteDC Lib “gdi32" (ByVal hdc As Long) As Long Dim scrndc As Long Const SRCCOPY = &HCC0020 Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer) |
將窗體覆蓋在屏幕上
Me.Left = 0 Me.Top = 0 Me.Height = Screen.Height Me.Width = Screen.Width Select Case KeyCode |
Case vbKeyLeft 按左方向鍵屏幕左右對調
| PaintPicture Picture1, Picture1.Width, 0, -Picture1.Width, Picture1.Height, 0, 0, Picture1.Width, Picture1.Height, SRCCOPY |
Case vbKeyRight 按右方向鍵屏幕上下對調
| PaintPicture Picture1, 0, Picture1.Height, Picture1.Width, -Picture1.Height, 0, 0, Picture1.Width, Picture1.Height, SRCCOPY |
Case vbKeyUp 按上方向鍵屏幕復原
| PaintPicture Picture1, 0, 0, Picture1.Width, Picture1.Height, 0, 0, Picture1.Width, Picture1.Height, SRCCOPY |
Case vbKeyDown 按下方向鍵屏幕上下,左右都對調
| PaintPicture Picture1, Picture1.Width, Picture1.Height, -Picture1.Width, -Picture1.Height, 0, 0, Picture1.Width, Picture1.Height, SRCCOPY Case vbKeyEscape 按ESC鍵退出 Unload Me |
Case Else 按其他鍵顯示出錯信息
MsgBox “無效鍵", vbOKOnly End Select End Sub Private Sub Form_Load() Dim throw As Long |
最小化窗體
| Me.Top = 40 Me.Width = 40 |
得到屏幕圖像
| scrndc = GetDC(0) Picture1.Width = Screen.Width Picture1.Height = Screen.Height throw = BitBlt(Picture1.hdc, 0, 0, Picture1.ScaleWidth, Picture1.ScaleHeight, scrndc, 0, 0, SRCCOPY) SavePicture Picture1.Image, “c:emp.bmp" Set Picture1.Picture = LoadPicture(“c:emp.bmp") End Sub Private Sub Form_Unload(Cancel As Integer) DeleteDC scrndc Kill “c:emp.bmp" End Sub |
運行程序用方向鍵就能控制屏幕翻轉,ESC鍵退出。
以上程序在我的機子上運行有一點停頓,各位看官的機子一定不比我的差,運行起來自然流暢。其實如果要提高運行速度可以全部用API來做,這樣的話就會復雜一點,但還可以實現許多特殊效果,有機會我們下回分解。