专注于互联网--专注于架构

最新标签
网站地图
文章索引
Rss订阅

首页 »VB教程 » vb图片:VB实现雨滴式图片效果 »正文

vb图片:VB实现雨滴式图片效果

来源: 发布时间:星期四, 2008年9月25日 浏览:93次 评论:0

本范例是以一个stdPicture物件来存图形,之後於PictureBox中以特殊效果来显示。
因为我们想显示的只有一个图,所以不想多用另一个PictureBox来存原始图,而後再画到另一个PictureBox上,那只有用StdPicture 物件来取代PictureBox(存来源图),但是BitBlt这个绘图函式需来源与目的的hDc,而StdPicture物件没有hDc,它只有一个Handle值,以本例来说,这Handle值便是图形的hBitmap值。所以我们只好使用MemoryDC的方式来做,产生一MemoryDc後将BitMap图放於其上,之後便可以使用BitBlt来绘图了。´需求一个PictureBox( Named picture2),一个Command按键)
Option Explicit
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 CreateCompatibleDC Lib \"gdi32\" _
(ByVal hdc As Long) As Long
Private Declare Function SelectObject Lib \"gdi32\" _
(ByVal hdc As Long, ByVal hObject As Long) As Long
Private Declare Function DeleteDC Lib \"gdi32\" (ByVal hdc As Long) As Long
Const SRCCOPY = &HCC0020
Private Picture1 As New StdPicture

Private Sub Command1_Click()
Dim i As Long
Dim j As Long
Dim height5 As Long, width5 As Long
Dim hMemDc As Long

´stdPicture物件的度量单位是Himetric所以要转换成Pixel
height5 = ScaleY(Picture1.Height, vbHimetric, vbPixels)
If height5 > Picture2.ScaleHeight Then
height5 = Picture2.ScaleHeight
End If
width5 = ScaleX(Picture1.Width, vbHimetric, vbPixels)
If width5 > Picture2.ScaleWidth Then
width5 = Picture2.ScaleWidth
End If
´Create Memory DC
hMemDc = CreateCompatibleDC(Picture2.hdc)
´将Picture1的BitMap图指定给hMemDc
Call SelectObject(hMemDc, Picture1.Handle)
For i = height5 To 1 Step -1
Call BitBlt(Picture2.hdc, 0, i, width5, 1, _
hMemDc, 0, i, SRCCOPY)
For j = i - 1 To 1 Step -1
Call BitBlt(Picture2.hdc, 0, j, width5, 1, _
hMemDc, 0, i, SRCCOPY)
Next j
Next
Call DeleteDC(hMemDc)
End Sub

Private Sub Form_Load()
Dim i As Long
Picture2.ScaleMode = 3 ´设定成Pixel的度量单位
´设定待Display的图
Set Picture1 = LoadPicture(\"c:\\windows\\素还真.bmp\")
´ ^^^^^^^^^^^^^^^^^^^^^^
´ Load the picture we want to show
End Sub
返回

Shrinking Icons Down to Size
Abstract
You can use the Windows application programming interface (API)
BitBlt function to modify the size of an icon. This article explains
how to enlarge or shrink an icon.

Modifying an Icon´s Size
You can use the Windows application programming interface (API)
BitBlt function to create an icon that is smaller or larger than the
original icon. The BitBlt function copies a memory device context to[Page]
another memory device context. (A memory device context is a block of
memory that represents a display surface, such as an Image or Picture
Box control. See Tip 31: \"Creating the Windows Wallpaper Effect for a
complete explanation of the BitBlt function.)

In the example program below, we first load an icon into an Image
control. Then we modify the Image control´s Height and Width
properties so the icon becomes 75 percent smaller than its original
size. The BitBlt function is then used to copy the icon stored in the
Image control to the Picture Box control.

Example Program
1. Create a new project in Visual Basic. Form1 is created by default.
2. Add the following Constant and Declare statements to the General
Declarations section of Form1 (note that the Declare statement
must be typed as a single line of code):

Private Declare Function BitBlt Lib \"GDI\" (ByVal hDestDC As Integer,
ByVal X As Integer, ByVal Y As Integer, ByVal nWidth As Integer,
ByVal nHeight As Integer, ByVal hSrcDC As Integer,
ByVal XSrc As Integer, ByVal YSrc As Integer,
ByVal dwRop As Long) As Integer
Const SRCCOPY = &HCC0020

3. Add a Command Button control to Form1. Command1 is created by
default. Set its Caption property to \"Shrink Icon\".
4. Add the following code to the Click event for Command1:

Private Sub Command1_Click()
Dim X As Integer
Dim Y As Integer
Dim W As Integer
Dim H As Integer
Dim Ret As Integer

Image1 = LoadPicture(\"c:\\vb\\icons\\misc\\binoculr.ico\")
Image1.Width = 0.75 * Image1.Width
Image1.Height = 0.75 * Image1.Height
Picture1.Width = Image1.Width
Picture1.Height = Image1.Height

X = Image1.Left / Screen.TwipsPerPixelX
Y = Image1.Top / Screen.TwipsPerPixelY

W = Picture1.Width / Screen.TwipsPerPixelX
H = Picture1.Height / Screen.TwipsPerPixelY

Ret = BitBlt(Picture1.hDC, 0, 0, W, H, Form1.hDC, X, Y, SRCCOPY)
Picture1.Refresh
End Sub

5. Add an Image control to Form1. Image1 is created by default. Set
its Stretch property to True.
6. Add a Picture Box control to Form1. Picture1 is created by
default. Set its AutoRedraw property to True.
返回

获得位图文件的信息
在Form中添加一个Picture控件和一个CommandButton控件,在Picture控件中加入一个位图文件,将下面代码加入其中:

相关文章

读者评论

  • 共0条 分0页

发表评论

  • 昵称:
  • 内容: