Results 1 to 4 of 4

Thread: Check if window is minimizing/restoring or fully minimized/restored

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2015
    Posts
    72

    Unhappy 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.

    Name:  notepad-minimizing.jpg
Views: 1321
Size:  43.0 KB

    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!

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2015
    Posts
    72

    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.

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width