Solved

GetWindowPlacement !!

Posted on 2000-04-29
18
837 Views
Last Modified: 2008-03-10
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.
0
Comment
Question by:peterchamberlin
  • 8
  • 5
  • 3
  • +1
18 Comments
 
LVL 28

Expert Comment

by:AzraSound
ID: 2762738
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)
0
 

Author Comment

by:peterchamberlin
ID: 2762787
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 :)
0
 

Author Comment

by:peterchamberlin
ID: 2762789
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.
0
 

Author Comment

by:peterchamberlin
ID: 2762790
This question has a deletion request Pending
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2762841
This question no longer is pending deletion
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2762842
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
0
 
LVL 5

Expert Comment

by:KDivad
ID: 2763330
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?
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2763339
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
0
 
LVL 5

Expert Comment

by:KDivad
ID: 2763566
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.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:peterchamberlin
ID: 2763706
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 !
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2763834

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

Author Comment

by:peterchamberlin
ID: 2763845
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 :)
0
 
LVL 14

Accepted Solution

by:
wsh2 earned 50 total points
ID: 2763859
Am not sure what your application is doing.. but another state to watch for is:

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

From MSDN:
--------------------------------------
IsIconic

The IsIconic function determines whether the specified window is minimized (iconic).

BOOL IsIconic(
  HWND hWnd   // handle to window
);
 
Parameters:

hWnd:
Handle to the window to test.

Return Values:
If the window is iconic, the return value is nonzero.

If the window is not iconic, the return value is zero.

--------------------------------------



If you want to accept a comment as an answer.. on the right of the comment bar should be a link stating in effect "Accept Comment as Answer".. <smile>.
0
 

Author Comment

by:peterchamberlin
ID: 2764213
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.
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2764216
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

0
 

Author Comment

by:peterchamberlin
ID: 2764254
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.
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2764725
Maximized?.. <smile>

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

The IsZoomed function determines whether a window is maximized.



0
 

Author Comment

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

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now