Link to home
Start Free TrialLog in
Avatar of peterchamberlin
peterchamberlin

asked on

GetWindowPlacement !!

I've been trying to use GetWindowPlacement to determine whether a window of known hWnd is on the screen or not (SW_HIDE or SW_SHOW). For some reason the thing just keeps returning 0 (error) and won't return the necessary information. The M$ KB article is for VB3 with a DLL call to user, which I don't even have on my Win95 system. I basically need a quick demo of a working call to do the above. Laughable, there is a link on the M$ page to another kb article which gives info on getting repeat 0 back from the call, naturally a 404 error :(

Thanks to anyone who can help.
Avatar of AzraSound
AzraSound
Flag of United States of America image

be sure that when you pass the structure to the api function you initiate the size of the variable.

i.e.,

Dim win As WINDOWREPLACEMENT
win.Length = Len(win)
Avatar of peterchamberlin
peterchamberlin

ASKER

Actually turned out to be M$'s example getting me confused, I had used their structures which contained integers, the actual items should have all been longs, which is why it didn't work so well when converting to user32.dll. Anyway, everything's working now. In a way your answer is correct, as the length before was not right due to wrong structure being defined :)
I managed to solve this by getting the latest structures and calls from API Viewer, the M$ example is WAY out of date and badly needs fixing.
This question has a deletion request Pending
This question no longer is pending deletion
dont mean to be a nuisance but you think you can post what you did?  i went and threw together some code real quick but it doesn't seem to be working:


Private Sub Command1_Click()
    Dim hwnd As Long
    Dim retval As Long
    Dim win As WINDOWPLACEMENT
    win.Length = Len(win)
    hwnd = FindWindow(vbNullString, "Microsoft Word")
    retval = GetWindowPlacement(hwnd, win)
    If retval = 0 Then
        Debug.Print "Function failed"
        Exit Sub
    End If
    Select Case win.showCmd
        Case SW_HIDE
            MsgBox "Window is hidden."
        Case Else
            MsgBox "Window is not hidden."
    End Select
End Sub
AzraSound, your code is all right. I tried the exact same code with the only change being which window to look for. Are you sure FindWindow is returning the hwnd?
yes it returns a valid handle...that is why I am so confused.  i've tried with word and notepad after hiding them with showwindow api and SW_HIDE parameter but my msgbox keeps telling me the window is not hidden
I didn't try to look for the SW_HIDE state when I tested the code.
Perhaps you can set SW_HIDE when using SetWindowPlacement, but not retrieve it using GetWindowPlacement. A bit odd, but that's M$! Maybe you have to use something like IsVisible(?) to find that one out.
In my code I was actually looking for SW_SHOW and SW_HIDE to be found. The showCmd values seem to be quite unreliable, as in my code when I set it to SW_HIDE, the next time of checking the value is other than SW_HIDE, although the window is no longer on-screen. I'd advise using API Viewer and checking through all the other SW_ parameters and see which ones the call returns, I suspect that multiple ones can mean that the Window is visible. Let me know what you come up with !

Declare Function IsWindowVisible Lib "user32" Alias "IsWindowVisible" (ByVal hwnd As Long) As Long

From MSDN:
---------------------------------------
IsWindowVisible

The IsWindowVisible function retrieves the visibility state of the specified window.

BOOL IsWindowVisible(
  HWND hWnd   // handle to window
);
 
Parameters

hWnd:
Handle to the window to test.

Return Values:
If the specified window, its parent window, its parent's parent window, and so forth, have the WS_VISIBLE style, the return value is nonzero. Otherwise, the return value is zero.

Because the return value specifies whether the window has the WS_VISIBLE style, it may be nonzero even if the window is totally obscured by other windows.

Remarks:
The visibility state of a window is indicated by the WS_VISIBLE style bit. When WS_VISIBLE is set, the window is displayed and subsequent drawing into it is displayed as long as the window has the WS_VISIBLE style.

Any drawing to a window with the WS_VISIBLE style will not be displayed if the window is obscured by other windows or is clipped by its parent window.

QuickInfo:
Windows NT/2000: Requires Windows NT 3.1 or later.
Windows 95/98: Requires Windows 95 or later.
Windows CE: Requires version 1.0 or later.
Thanks wsh2, that's most useful, and much more reliable that using GetWindowPlacement, now I can simply use that in conjuction with SetWindowPlacement and the whole thing is much simpler and works reliably :)
ASKER CERTIFIED SOLUTION
Avatar of wsh2
wsh2

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for extra state of windows, I'd forgotten about that one :) I have a curious thing with SetWindowPlacement, if I make the window hidden by setting SW_HIDE whilst the window is original maximised, when I used SW_SHOW to restore the window it puts it back in its restored state, not the previous maximised state. Am I doing something wrong here? Or should I copy across the original showCmd from GetWindowPlacement then restore the window to this?

PS> You'll get your points when I get the code here working satisfactorily :) Which shouldn't be too long to wait.
http://www.thescarms.com/vbasic/AltTab.htm
http://www.mvps.org/vb/index2.html?samples.htm


two samples i thought you might be interested in...at the second link check out the project called forcefore.zip

Note: There's no need to check for the iconic state, as IsVisible also returns non zero for when windows are minimised. After testing around some more, and using a temp variable to help previous showCmd, the whole project seems to be working fine now. Thanks for your help.
Maximized?.. <smile>

Declare Function IsZoomed Lib "user32" Alias "IsZoomed" (ByVal hwnd As Long) As Long

The IsZoomed function determines whether a window is maximized.



Yep, and Maximised as well, I came across IsZoomed whilst looking through MSDN links at bottom too :)