快速移動圖像

 

API函數聲明:

Module API
    
Declare Function CreateCompatibleDC Lib "gdi32.dll" (ByVal hdc As IntPtr) As IntPtr
    
Declare Function SelectObject Lib "gdi32.dll" (ByVal hdc As IntPtr, ByVal hgdiobj As IntPtr) As IntPtr
    
Declare Function ReleaseDC Lib "gdi32.dll" (ByVal hWnd As IntPtr, ByVal hDC As IntPtr) As Integer
    
Declare Function GetStretchBltMode Lib "gdi32.dll" (ByVal hdc As IntPtr) As Integer
    
Declare Function SetStretchBltMode Lib "gdi32" (ByVal hdc As IntPtr, ByVal iStretchMode As IntegerAs Integer
    
Declare Function DeleteObject Lib "gdi32.dll" (ByVal hObject As IntPtr) As IntPtr
    
Declare Function DeleteDC Lib "gdi32.dll" (ByVal hDc As IntPtr) As IntPtr


    
Declare Function BitBlt Lib "GDI32.DLL" ( _
    
ByVal hdcDest As IntPtr, _
    
ByVal nXDest As Integer, _
    
ByVal nYDest As Integer, _
    
ByVal nWidth As Integer, _
    
ByVal nHeight As Integer, _
    
ByVal hdcSrc As IntPtr, _
    
ByVal nXSrc As Integer, _
    
ByVal nYSrc As Integer, _
    
ByVal dwRop As TernaryRasterOperations) As Boolean


    
Declare Function StretchBlt Lib "gdi32.dll" ( _
    
ByVal hdcDest As IntPtr, _
    
ByVal nXOriginDest As Integer, _
    
ByVal nYOriginDest As Integer, _
    
ByVal nWidthDest As Integer, _
    
ByVal nHeightDest As Integer, _
    
ByVal hdcSrc As IntPtr, _
    
ByVal nXOriginSrc As Integer, _
    
ByVal nYOriginSrc As Integer, _
    
ByVal nWidthSrc As Integer, _
    
ByVal nHeightSrc As Integer, _
    
ByVal dwRop As TernaryRasterOperations) As Boolean


    
Enum TernaryRasterOperations As Integer
        SRCCOPY 
= &HCC0020
        SRCPAINT 
= 15597702    'dest = source OR dest
        SRCAND = 8913094       'dest = source AND dest
        SRCINVERT = 6684742    'dest = source XOR dest
        SRCERASE = 4457256     'dest = source AND (NOT dest )
        NOTSRCCOPY = 3342344   'dest = (NOT source)
        NOTSRCERASE = 1114278  'dest = (NOT src) AND (NOT dest) 
        MERGECOPY = 12583114   'dest = (source AND pattern)
        MERGEPAINT = 12255782  'dest = (NOT source) OR dest
        PATCOPY = 15728673     'dest = pattern
        PATPAINT = 16452105    'dest = DPSnoo
        PATINVERT = 5898313    'dest = pattern XOR dest
        DSTINVERT = 5570569    'dest = (NOT dest)
        BLACKNESS = 66         'dest = BLACK
        WHITENESS = 16711778   'dest = WHITE
    End Enum

End Module

Public Class Form1
    
Dim offset As Point
    
Dim MousePressPoint As Point

    
Dim Gr As Graphics
    
Dim srcBitmap As Bitmap
    
Dim srcHDC, desHDC As IntPtr
    
Dim HBitmapSrc As IntPtr

    
Private Sub Panel1_DoubleClick(ByVal sender As ObjectByVal e As System.EventArgs) Handles Panel1.DoubleClick
        
Dim cd As New OpenFileDialog
        cd.Filter 
= "bmp文件 (*.bmp)|*.bmp|jpg文件 (*.jpg)|*.jpg|gif文件 (*.gif)|*.gif|所有文件|*.*"
        
If cd.ShowDialog = Windows.Forms.DialogResult.OK Then
            srcBitmap 
= New Bitmap(cd.FileName)
            offset 
= New Point(00)
            Panel1.Invalidate()
        
End If
    
End Sub


    
Private Sub PictureBox1_MouseDown(ByVal sender As ObjectByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown
        MousePressPoint 
= e.Location
    
End Sub


    
Private Sub PictureBox1_MouseMove(ByVal sender As ObjectByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseMove
        
If e.Button = Windows.Forms.MouseButtons.Left Then
            Windows.Forms.Cursor.Current 
= Cursors.Hand

            offset.X 
+= e.X - MousePressPoint.X
            offset.Y 
+= e.Y - MousePressPoint.Y

            MousePressPoint 
= e.Location

            
Call Me.MyReDraw(offset.X, offset.Y)
        
End If
    
End Sub


    
Private Sub PictureBox1_Paint(ByVal sender As ObjectByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
        
Call Me.MyReDraw(offset.X, offset.Y)
    
End Sub


    
Sub MyReDraw(ByVal x As IntegerByVal y As Integer)
        
If srcBitmap Is Nothing Then Exit Sub

        
If srcHDC.Equals(IntPtr.Zero) Then
            srcHDC 
= CreateCompatibleDC(IntPtr.Zero)
            HBitmapSrc 
= srcBitmap.GetHbitmap()
            SelectObject(srcHDC, HBitmapSrc)
        
End If

        
If desHDC.Equals(IntPtr.Zero) Then
            
If IsNothing(Gr) Then Gr = Panel1.CreateGraphics
            desHDC 
= Gr.GetHdc()
            
'SetStretchBltMode(desHDC, 3)
        End If

        
Call BitBlt(desHDC, 00, Panel1.ClientSize.Width, Panel1.ClientSize.Height, srcHDC, -x, -y, TernaryRasterOperations.SRCCOPY)
        Gr.ReleaseHdc(desHDC)
        desHDC 
= Nothing
    
End Sub


    
Private Sub Form1_FormClosing(ByVal sender As ObjectByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        
If Not IsNothing(srcBitmap) Then srcBitmap.Dispose()
        
If Not srcHDC.Equals(IntPtr.Zero) Then DeleteDC(srcHDC)
        
If Not desHDC.Equals(IntPtr.Zero) Then DeleteDC(desHDC)
        
If Not HBitmapSrc.Equals(IntPtr.Zero) Then DeleteObject(HBitmapSrc)
        
If Not IsNothing(Gr) Then Gr.Dispose()
        GC.Collect()
    
End Sub

End Class

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章