开机,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:\temp.bmp" Set Picture1.Picture = LoadPicture(“c:\temp.bmp") End Sub Private Sub Form_Unload(Cancel As Integer) DeleteDC scrndc Kill “c:\temp.bmp" End Sub |
运行程序用方向键就能控制屏幕翻转,ESC键退出。
以上程序在我的机子上运行有一点停顿,各位看官的机子一定不比我的差,运行起来自然流畅。其实如果要提高运行速度可以全部用API来做,这样的话就会复杂一点,但还可以实现许多特殊效果,有机会我们下回分解。
|