⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 snap.frm

📁 很好的教程原代码!
💻 FRM
字号:
VERSION 5.00
Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
Begin VB.Form frmSnap 
   Caption         =   "抓取屏幕软件"
   ClientHeight    =   4860
   ClientLeft      =   165
   ClientTop       =   735
   ClientWidth     =   7950
   LinkTopic       =   "Form1"
   ScaleHeight     =   4860
   ScaleWidth      =   7950
   StartUpPosition =   3  'Windows Default
   Begin MSComDlg.CommonDialog CommonDialog1 
      Left            =   4320
      Top             =   4200
      _ExtentX        =   847
      _ExtentY        =   847
      _Version        =   393216
      CancelError     =   -1  'True
   End
   Begin VB.PictureBox Picture1 
      AutoSize        =   -1  'True
      Height          =   3975
      Left            =   0
      ScaleHeight     =   3915
      ScaleWidth      =   6555
      TabIndex        =   0
      Top             =   0
      Width           =   6615
   End
   Begin VB.Menu mnuFile 
      Caption         =   "文件"
      Begin VB.Menu mnuFileSnap 
         Caption         =   "抓取屏幕"
         Shortcut        =   ^S
      End
      Begin VB.Menu mnuFileSave 
         Caption         =   "保存图像"
         Enabled         =   0   'False
      End
      Begin VB.Menu mnuFileSep 
         Caption         =   "-"
      End
      Begin VB.Menu mnuFileExit 
         Caption         =   "退出"
      End
   End
End
Attribute VB_Name = "frmSnap"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Private bIsSnap As Boolean  ' 如果正在抓取则为真

Private Sub Form_Load()
    ScaleMode = vbPixels
    AutoRedraw = True
    Me.Picture1.Left = 0
    Me.Picture1.Top = 0
    Me.Picture1.Width = Me.ScaleWidth
    Me.Picture1.Height = Me.ScaleHeight
End Sub

' 释放捕捉
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    Unload Me
End Sub

' 释放内存空间
Private Sub Form_Unload(Cancel As Integer)
    Set frmSnap = Nothing
End Sub

' 这里开始抓取
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, _
                x As Single, y As Single)
                
    Dim spLeft As Long
    Dim spTop As Long
    Dim spRight As Long
    Dim spBottom As Long
    If (Button And vbLeftButton) Then
        ' 如果bIsSnap为真
        If bIsSnap And Screen.MousePointer = vbCrosshair Then
            Dim r As RECT
            Dim pt As POINTAPI
            
            ' 恢复抓取标志
            bIsSnap = False
            ' 设置抓取开始点
            pt.x = x
            pt.y = y
            
            '调用CaptureRect函数开始区域抓取
            r = CaptureRect(Me.hwnd, pt)
                                       
            ' 获取抓取区域范围
            spLeft = r.Left
            spTop = r.Top
            spRight = r.Right
            spBottom = r.Bottom
            ScrnCap spLeft, spTop, spRight, spBottom
            Me.WindowState = 0
            Me.Picture1.Picture = Clipboard.GetData
            Me.mnuFileSave.Enabled = True
        End If
    End If
End Sub

Private Sub Form_MouseUp(Button As Integer, Shift As Integer, _
        x As Single, y As Single)
        
    '恢复光标
    If (Button And vbLeftButton) Then
        Screen.MousePointer = vbNormal
    End If
End Sub

Private Sub mnuFileExit_Click()
    End
End Sub

Private Sub mnuFileSnap_Click()
    If Not bIsSnap Then
        bIsSnap = True
        ' 将光标改为十字型
        Screen.MousePointer = vbCrosshair
        ' 设置抓取,使得本窗体可以接收所有窗体的鼠标事件
        SetCapture Me.hwnd
        ' 最小化本窗体
     '   Me.WindowState = 1
    End If
End Sub

Private Sub mnuFileSave_click()
    Dim FileName As String
    On Error Resume Next
    CommonDialog1.DialogTitle = "保存"
    ' cdlOFNHideReadOnly隐藏只读选择框
    ' cdlOFNOverwritePrompt当保存的文件存在时给于是否替换的提示
    CommonDialog1.Flags = cdlOFNHideReadOnly Or cdlOFNOverwritePrompt
    CommonDialog1.Filter = "位图文件(*.bmp)|*.bmp"
    CommonDialog1.ShowSave
    If Err = 32755 Then Exit Sub    ' 用户选择了Cancel
    FileName = CommonDialog1.FileName
    If FileName <> "" Then
        SavePicture Picture1.Picture, FileName
    End If
End Sub

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -