取得Microsoft Word影像的方法

請準備Form1, Command1, List1, Picture1, Moudle1

Form1程式碼:

Option Explicit
Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long

Private Sub Command1_Click()
CallBackDemo
End Sub

Private Sub Form_Load()
Me.ScaleMode = 3
Picture1.ScaleMode = 3
Picture1.AutoRedraw = True
End Sub

Private Sub List1_Click()
On Error Resume Next
Dim W As String
SetWindowPos Val(Left(List1.List(List1.ListIndex), 12)), -1, 0, 0, 0, 0, 3

DoEvents

Dim wdc As Long
wdc = GetWindowDC(Val(Left(List1.List(List1.ListIndex), 12)))
BitBlt Picture1.hdc, 0, 0, Picture1.Width, Picture1.Height, wdc, 0, 0, vbSrcCopy
ReleaseDC Val(Left(List1.List(List1.ListIndex), 12)), wdc
Picture1.Refresh

SetWindowPos Val(Left(List1.List(List1.ListIndex), 12)), -2, 0, 0, 0, 0, 3
SetForegroundWindow Me.hwnd
End Sub

Moudle1程式碼:

Option Explicit

Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Any) As Long
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Public Declare Function IsWindowVisible Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long
Public 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

Public EWin As String

Private Function CallBackFunction(ByVal hwnd As Long) As Long
Dim ShWnd As String * 12
ShWnd = Str(hwnd)
If IsWindowVisible(hwnd) Then
If InStr(GetTitle(hwnd), "Microsoft Word") <> 0 Then
Form1.List1.AddItem ShWnd & GetTitle(hwnd)
End If
End If
CallBackFunction = True
End Function

Public Sub CallBackDemo()
Form1.List1.Clear
EnumWindows AddressOf CallBackFunction, 0&
End Sub

Public Function GetTitle(hwnd As Long) As String
Dim Title As String * 255
Dim TitleLength As Long
TitleLength = GetWindowText(hwnd, Title, 255)
GetTitle = Left(Title, TitleLength)
End Function

執行後按Command1,然後在List1點選Microsoft Word的視窗,Microsoft Word的影像便會印在Picture1了!


上一頁