-
Jun 16th, 2017, 11:32 AM
#1
Thread Starter
Lively Member
Check if window is minimizing/restoring or fully minimized/restored
I'm using this code to capture screen every time the top window is minimized/restored or focus changed to other window.
Just create a picturebox, a timer, and paste this code:
Code:
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Declare Function BitBlt Lib "gdi32.dll" (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
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Const SRCCOPY As Long = &HCC0020
Dim lastforegroundWindow As Long
Dim DeskDC As Long
Private Sub Form_Load()
Picture1.Top = 0
Picture1.Left = 0
DeskDC = GetDC(GetDesktopWindow)
End Sub
Private Sub Form_Resize()
Picture1.Width = Me.Width
Picture1.Height = Me.Height
End Sub
Private Sub Timer1_Timer()
Dim foregroundWindow As Long, r As RECT
foregroundWindow = GetForegroundWindow()
Call GetWindowRect(foregroundWindow, r)
If foregroundWindow <> lastforegroundWindow Then
BitBlt Picture1.hDC, 0, 0, 1000, 1000, DeskDC, 0, 0, SRCCOPY
lastforegroundWindow = foregroundWindow
End If
End Sub
This code works fine if I switch to other windows without minimize/restore. But if i minimize a window (for example - notepad), my application capture the screen at the time notepad is minimizing/restoring which i don't want. I just want my application check/wait for notepad fully minimized/restored and capture the screen after that.
The only way i found to workaround is using Sleep api to wait for 1 second, but it's not a good idea . Is there any other method that i don't know?
Thank you all!
-
Jun 16th, 2017, 12:58 PM
#2
Re: Check if window is minimizing/restoring or fully minimized/restored
You can try to check the state of the foreground window? Thinking the WS_MAXIMIZE and WS_MINIMIZE window styles won't be set until the window is fully minimized or maximized. You get those values via GetWindowLong API. However, that doesn't address the situation where the window is animating to one state or another. Since you are not likely going to be subclassing every foreground window to determine whether it is receiving WM_SIZING messages, maybe a short delay is a simpler solution.
-
Jun 16th, 2017, 09:35 PM
#3
Thread Starter
Lively Member
Re: Check if window is minimizing/restoring or fully minimized/restored
Hi Lavolpe,
Thank you very much for reply on my thread!
I've tried to check WS_MINIMIZE flag returned by GetWindowLong(hwnd,GWL_STYLE) but it does not help me. It seems that WS_MINIMIZE is set immediately even the form is minimizing.
To check if any other flags from GetWindowLong can help me, I've tried to track the value returned by GetWindowLong(GWL_STYLE/GWL_EXSTYLE) before/after click on minimize button but i found nothing it can help.
Code:
Dim lastWLong as long
Private Sub Timer1_Timer()
wLong = GetWindowLong(hwnd, GWL_STYLE)
If lastWLong <> wLong Then
'Bitblt ...
debug.print "WindowLong changed:" & wLong
lastWLong = wLong
End if
End sub
I've also tried GetWindowPlacement, GetWindowRect but the same result.
I hope there is a better method left, but it seems that the only way to workaround without Sleep is subclassing, which may not always work because the foreground window's privilege may be higher than my application so i can't install a hook on it.
-
Jun 17th, 2017, 11:14 AM
#4
Re: Check if window is minimizing/restoring or fully minimized/restored
After additional thought, I think checking the window style bits wouldn't work anyway. The animation during minimize, restore, maximize may just be a buffer, capturing the window DC beforehand. Then Windows draws the animation on the desktop from that buffer. In other words, the actual window state may have been already set just before animation/transition begins. Just thinking out loud.
Gotta ask, why is necessary to get a screen capture the exact moment the foreground window is removed from the screen during minimize, vs. waiting couple hundred milliseconds or a full second or two? Granted, it won't be perfect, especially on very slow systems where animation may take longer than anticipated.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|