' 發展單位:風禹科技驗證有限公司 ' 撰寫人:鄭子璉(Tzu-Lien, Cheng, 璉璉) ,成大水博肄,微軟最有價值專家 ' Web: http://tlcheng.twbbs.org/TLCheng/ E-Mail: qvb3377@ms5.hinet.net ' -------------------------------------------------------------------------------------- Module modWindowTool #Region "Windows API 宣告" ' 顯示視窗 _ Public Function ShowWindow( _ ByVal hWnd As IntPtr, _ ByVal nCmdShow As ShowWindowCommands _ ) As Integer End Function _ Private Function GetWindowDC( _ ByVal hWnd As IntPtr) As IntPtr End Function _ Private Function ReleaseDC( _ ByVal hWnd As IntPtr, _ ByVal hDC As IntPtr) As Integer End Function _ Private Function BitBlt( _ ByVal hDCofTarget As IntPtr, _ ByVal x As Integer, _ ByVal y As Integer, _ ByVal nWidth As Integer, _ ByVal nHeight As Integer, _ ByVal hDCofSource As IntPtr, _ ByVal xSrc As Integer, _ ByVal ySrc As Integer, _ ByVal opCode As Integer) As Integer End Function _ Public Function GetDesktopWindow() As IntPtr End Function _ Private Function GetWindowRect( _ ByVal hWnd As IntPtr, _ ByRef lpRect As RECT) As Boolean End Function _ Private Function GetWindowInfo( _ ByVal hWnd As IntPtr, _ ByRef pwi As WINDOWINFO) As Boolean End Function _ Private Function FindWindow( _ ByVal lpClassName As String, _ ByVal lpWindowName As String) As IntPtr End Function _ Public Structure RECT Public left As Int32 Public top As Int32 Public right As Int32 Public bottom As Int32 End Structure _ Public Structure WINDOWINFO Public cbSize As UInt32 Public rcWindow As RECT Public rcClient As RECT Public dwStyle As UInt32 Public dwExStyle As UInt32 Public dwWindowStatus As UInt32 Public cxWindowBorders As UInt32 Public cyWindowBorders As UInt32 Public atomWindowType As UInt16 Public wCreatorVersion As UInt16 End Structure Public Enum enuRasterOperationCode As Integer BLACKNESS = &H42 DSINVERT = &H550009 MERGECOPY = &HC000CA MERGEPAINT = &HBB0226 NOTSRCCOPY = &H330008 NOTSRCERASE = &H1100A6 PATCOPY = &HF00021 PATINVERT = &H5A0049 PATPAINT = &HFB0A09 SRCAND = &H8800C6 SRCCOPY = &HCC0020 SRCERASE = &H4400328 SRCINVERT = &H660046 SRCPAINT = &HEE0086 WHITENESS = &HFF0062 End Enum Public Enum ShowWindowCommands As Integer SW_HIDE = 0 SW_SHOWNORMAL = 1 SW_NORMAL = 1 SW_SHOWMINIMIZED = 2 SW_SHOWMAXIMIZED = 3 SW_MAXIMIZE = 3 SW_SHOWNOACTIVATE = 4 SW_SHOW = 5 SW_MINIMIZE = 6 SW_SHOWMINNOACTIVE = 7 SW_SHOWNA = 8 SW_RESTORE = 9 SW_SHOWDEFAULT = 10 SW_FORCEMINIMIZE = 11 SW_MAX = 11 ' Old ShowWindow() Commands HIDE_WINDOW = 0 SHOW_OPENWINDOW = 1 SHOW_ICONWINDOW = 2 SHOW_FULLSCREEN = 3 SHOW_OPENNOACTIVATE = 4 End Enum #End Region Public Function GetWindowSize(ByVal hWnd As IntPtr) As Size Dim pRect As RECT Dim pSize As Size GetWindowRect(hWnd, pRect) With pRect pSize.Width = .right - .left pSize.Height = .bottom - .top End With Return pSize End Function Public Function MyFindWindow(Optional ByVal lpClassName As String = Nothing, Optional ByVal lpWindowName As String = Nothing) As IntPtr Return FindWindow(lpClassName, lpWindowName) End Function Public Function GetScreenShot(ByVal hWnd As IntPtr, ByVal vLocation As Point, ByVal vSize As Size, Optional ByVal rocFlags As enuRasterOperationCode = enuRasterOperationCode.SRCCOPY) As Drawing.Image Dim myImage As Drawing.Image = New Drawing.Bitmap(vSize.Width, vSize.Height) Dim grfx As Drawing.Graphics = Drawing.Graphics.FromImage(myImage) Dim hDCofTar As IntPtr = grfx.GetHdc() Dim hDCofSrc As IntPtr = GetWindowDC(hWnd) ' TODO: throw exception BitBlt(hDCofTar, 0, 0, vSize.Width, vSize.Height, hDCofSrc, vLocation.X, vLocation.Y, rocFlags) ReleaseDC(hWnd, hDCofSrc) grfx.ReleaseHdc(hDCofTar) grfx.Dispose() Return myImage End Function End Module