用VB实现屏幕阴暗操作
http://www.tongyi.net 作者:燕义怀 出处:中国电脑教育报 点击:1452
使用过Windows 9x的用户都应该知道,在关闭Windows系统时,整个屏幕会暗下来,那么,如何在自己开发的应用程序中也实现这一效果呢?我们通过调用一组API函数就可以实现,如果你有兴趣,就跟我来吧!这一组函数是:
GetDC :用来获取屏幕的设备号;
ReleaseDC :释放指定的设备;
CreatePatternBrush :创建图形刷子;
PatBlt: 利用创建的图形刷子填充指定设备;
DeleteObject :删除创建的对象(与CreatePatternBrush创建的刷子对象呼应);
CreateBitmap :创建BMP对象;
SelectObject :选择当前操作对象;
InvalidateRect :使操作无效,即恢复变暗前的状态。
基本思路
首先创建一个位图对象,然后,利用此位图对象创建一个图案刷子,选择指定设备为当前设备,利用已经创建的图案刷子填充当前设备,删除创建的刷子对象即可使当前设备变暗。调用InvalidateRect函数就可使指定设备恢复到变暗前的状态。
步 骤
1、创建一个Form1窗体,设置2个Command按钮,将Command1和Command2的Caption属性分别设置为:“变暗”和“恢复”。
2、在窗体代码模块中声明API函数和相应的类型,代码如下:
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long
Private Declare Function CreatePatternBrush Lib "gdi32" (ByVal hBitmap As Long) As Long
Private Declare Function PatBlt Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal dwRop As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Declare Function CreateBitmap Lib "gdi32" (ByVal nWidth As Long, ByVal nHeight As Long, ByVal nPlanes As Long, ByVal nBitCount As Long, lpBits As Any) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
Private Declare Function InvalidateRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT, ByVal bErase As Long) As Long
3、编写代码实现程序功能,由于版面所限,具体代码将放到CCE网站(http://www.cce.com.cn)。
以上就是屏幕变暗的方法。如果要使窗体、图片框等对象变暗,只要使窗体或图片框等对象获取句柄即可,修改Command1_Click过程和Command2_Click过程中的以下语句即可实现窗体变暗。
hdc5=Form1.Hdc;
Width5=Form1.width;
height5=Form1.height;
ReleaseDC(Form1.hwnd,hdct)
aa=InvalidateRect (Form1.hwnd,bottom,1)。
以上程序在VB6.中文版,Windwos 98 环境中运行通过。有兴趣的朋友可以去试一试,看到这种效果是不是觉得很惊奇呢!