雨滴式的顯示圖片 (cloud 轉貼) (轉)

worldblog發表於2007-12-02
雨滴式的顯示圖片 (cloud 轉貼) (轉)[@more@]雨滴式的顯示圖片
本範例是以一個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 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:素還真.bmp")
' ^^^^^^^^^^^^^^^^^^^^^^
' Load the picture we want to show
End Sub
返回

Shrinking Icons Down to Size
Abstract
You can use the Windows application programming interface ()
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
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 T31: "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 . 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:vbiconsmiscbinoculr.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和一個Commanutton控制元件,在Picture控制元件中加入一個位件,將下面程式碼加入其中:
Private Declare Function GetObject Lib "gdi32" Alias "GetObjectA" _
(ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) _
As Long
Private Declare Function GetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, _
ByVal dwCount As Long, lpBits As Any) As Long

Private Type BITMAP
bmType As Long
bmWidth As Long
bmHeight As Long
bmWidthBytes As Long
bmPlanes As Integer
bmBitsPixel As Integer
bmBits As Long
End Type

Private Sub Command1_Click()
Dim hBitmap As Long
Dim res As Long
Dim bmp As BITMAP
Dim byteAry() As Byte
Dim totbyte As Long, i As Long
hBitmap = Picture1.Picture.Handle

res = GetObject(hBitmap, Len(bmp), bmp) '取得BITMAP的結構

totbyte = bmp.bmWidthBytes * bmp.bmHeight '總共要多少BYTE來存圖
ReDim byteAry(totbyte - 1)
'將Picture1中的圖資訊存到ByteAry
res = GetBitmapBits(hBitmap, totbyte, byteAry(0))

De.Print "Total Bytes Copied :"; res
Debug.Print "bmp.bmBits "; bmp.bmBits
Debug.Print "bmp.bmBitsPixel "; bmp.bmBitsPixel '每相素位數
Debug.Print "bmp.bmHeight "; bmp.bmHeight '以相素計算圖象高度
Debug.Print "bmp.bmPlanes "; bmp.bmPlanes
Debug.Print "bmp.bmType "; bmp.bmType
Debug.Print "bmp.bmWidth "; bmp.bmWidth '以相素計算圖形寬度
Debug.Print "bmp.bmWidthBytes "; bmp.bmWidthBytes '以位元組計算的每掃描線長度?
End Sub
返回

放置“透明”的圖片
在 中,如果你試著把一隻有鳥的圖片放到背景的一棵樹上,你就會發現樹會被鳥遮住一個矩形的區域(即鳥的圖片矩形)。我們可以透過以下方法使圖片上非鳥的其它部分變透明:
我們可以利用一個 WinAPI BitBlt 對圖形進行一系列的位操作來達到此目的。
函式宣告:
Declare Function BitBlt Lib "gdi32" Alias "BitBlt" (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
引數解釋:
目標環境:hDestDC——目標裝置環境;x——左上角;y——頂端;nWidth——寬;nHeight——高
源環境:hSrcDC——源裝置環境;xSrc——源左上角;ySrc——源頂端;
dwRop——位處理操作,如 vbSrcAnd;vbSrcAnd;vbSrcCopy;vbSrcErase;vbSrcInvert 等
(目標環境或源環境只能是 Picture, Fo或 Printer 。各單位為象素。)
進行處理之前,我們需要對鳥的圖片進行處理:先複製一份相同的圖形,將其應該透明之處(鳥的背景)設定為黑色(設此圖為sPic),再將另一圖做以下處理:要複製的地方(鳥)設定為黑色,其餘地方設定(鳥的背景)為白色(設此圖為Mask)。
設樹的圖形為名dPic。
最後,請加入以下程式碼:
R=BitBlt(dPic.hdc,0,0,sPic.Width,sPic.Height,Mask.hdc,0,0,vbScrCopy)
R=BitBlt(dPic.hdc,0,0,sPic.Width,sPic.Height,sPic.hdc,0,0,vbScrInvert)
後記:
1、VB 中的 PaintPicture 方法提供類似功能,但速度不及此方法;
2、在此方法上稍微加入一些程式碼,就不難實現動畫的顯示。
3、VB 例子中的 CallDlls 就使用此方法。


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-987230/,如需轉載,請註明出處,否則將追究法律責任。

相關文章