Board logo

標題: [分享] 請問print screen的代碼如何寫?? (API運用) [打印本頁]

作者: Hsieh    時間: 2010-5-7 15:21     標題: 請問print screen的代碼如何寫?? (API運用)

我的要求是
打開檔案時即擷取一張屏幕圖
(截屏前須先將EXCEL視窗縮至最小以顯現要截屏的畫面)
並將其貼到A1
以後每小時擷取一張屏幕圖
(截屏前一樣須先將EXCEL視窗縮至最小
下張圖則貼至最適合之高度之行(緊接前一張圖)
例如第一張圖貼滿A1:K20
第二張貼到A21
不知如何達成
Emily_Li 回覆
  1. Declare Sub keybd_event Lib "user32.dll" (ByVal bVk As Byte, ByVal _
  2. bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
  3. Private Const VK_KEYUP = &H2
  4. Private Const VK_SNAPSHOT = &H2C
  5. Private Const VK_MENU = &H12
  6. Dim ws As Worksheet

  7. Declare Function apiShowWindow Lib "user32" Alias "ShowWindow" _
  8. (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long

  9. Global Const SW_MAXIMIZE = 3
  10. Global Const SW_SHOWNORMAL = 1
  11. Global Const SW_SHOWMINIMIZED = 2


  12. Sub Print_Screen()

  13. keybd_event VK_MENU, 0, 0, 0
  14. DoEvents

  15. keybd_event VK_SNAPSHOT, 0, 0, 0
  16. DoEvents

  17. keybd_event VK_SNAPSHOT, 0, VK_KEYUP, 0
  18. DoEvents

  19. keybd_event VK_MENU, 0, VK_KEYUP, 0
  20. DoEvents
  21. Set ws = ActiveSheet
  22. ws.Paste Destination:=ws.Range("A1")
  23. DoEvents
  24. End Sub
複製代碼
結果與要求有落差內  
這代碼是擷取excel的畫面
我的要求是
打開檔案時即擷取一張屏幕圖
(截屏前須先將EXCEL視窗縮至最小以顯現要截屏的畫面)
亦即是擷取另一個正於視窗最上層執行的程式畫面而不是擷取excel的畫面
並將其貼到A1
以後每小時擷取一張屏幕圖
(截屏前一樣須先將EXCEL視窗縮至最小)
不知能否改改
hsieh回覆:
依樣畫葫蘆
利用Emily_Li 前輩程式碼修改
  1. Declare Sub keybd_event Lib "user32.dll" (ByVal bVk As Byte, ByVal _
  2. bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
  3. Private Const VK_KEYUP = &H2
  4. Private Const VK_SNAPSHOT = &H2C
  5. Private Const VK_MENU = &H12
  6. Dim ws As Worksheet

  7. Declare Function apiShowWindow Lib "user32" Alias "ShowWindow" _
  8. (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long

  9. Global Const SW_MAXIMIZE = 3
  10. Global Const SW_SHOWNORMAL = 1
  11. Global Const SW_SHOWMINIMIZED = 2


  12. Sub Print_Screen()

  13. keybd_event VK_MENU, 0, 0, 0
  14. DoEvents
  15. Application.WindowState = xlMinimized
  16. Application.Wait Now + TimeValue("00:00:05")
  17. keybd_event VK_SNAPSHOT, 0, 0, 0
  18. DoEvents

  19. keybd_event VK_SNAPSHOT, 0, VK_KEYUP, 0
  20. DoEvents

  21. keybd_event VK_MENU, 0, VK_KEYUP, 0
  22. DoEvents
  23. Application.WindowState = xlMaximized
  24. Set ws = ActiveWorkbook.ActiveSheet
  25. ws.Paste Destination:=ws.Range("A1")
  26. DoEvents
  27. Application.OnTime Now + TimeValue("01:00:00"), "Print_Screen"
  28. End Sub
複製代碼





歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)