Solved

GetWindowPlacement never returns minimize

Posted on 2004-04-19
15
795 Views
Last Modified: 2007-12-19
GetWindowPlacement returns showCmd = 3 when the target app is maximized and 1 if it is normal. So far so good, but it returns 1 even if it is minimized (should return 2). If I click the min button on the target app, then it is minimized. If I force normal state then the target app is restored to normal, however the min button on the target app is disabled. Why SW_SHOWMINIMIZED is being ignored by GetWindowPlacement?

S
0
Comment
Question by:Shauli
  • 8
  • 5
15 Comments
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 10860551
Try use IsIconic to determine whether a window is minimized and IsZoomed to determine whether a window is maximized ?

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

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

Author Comment

by:Shauli
ID: 10860644
Same thing. IsIconic returns 0, as if the app is not minimized (or iconic) :(

S
0
 
LVL 19

Author Comment

by:Shauli
ID: 10860668
ps. I should add that it works fine with most of the app I test. Its just with some it doesnt. What could be the reason?

S
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 50

Expert Comment

by:Ryan Chong
ID: 10861277
Are you trying to get other Application's WindowState (minimized, normal, maximized) ? Can you post some your codes here if possible?

regards
0
 
LVL 19

Author Comment

by:Shauli
ID: 10861328
Here (in module level):

Option Explicit

Public Type RECT
  Left    As Long
  Top     As Long
  Right   As Long
  Bottom  As Long
End Type

Public Type POINTAPI
  x       As Long
  y       As Long
End Type

Public Type WINDOWPLACEMENT
  Length            As Long
  flags             As Long
  showCmd           As Long
  ptMinPosition     As POINTAPI
  ptMaxPosition     As POINTAPI
  rcNormalPosition  As RECT
End Type

Public Const SW_SHOWNORMAL = 1
Public Const SW_SHOWMINIMIZED = 2
Public Const SW_SHOWMAXIMIZED = 3
Public Const SW_SHOWNOACTIVATE = 4

Public Declare Function BringWindowToTop Lib "user32" _
  (ByVal hwnd As Long) As Long

Public Declare Function FindWindow Lib "user32" _
   Alias "FindWindowA" _
  (ByVal lpClassName As String, _
   ByVal lpWindowName As String) As Long
   
Public Declare Function GetWindowPlacement Lib "user32" _
   (ByVal hwnd As Long, lpwndpl As WINDOWPLACEMENT) As Long

Public Declare Function SetForegroundWindow Lib "user32" _
   (ByVal hwnd As Long) As Long

Public Declare Function SetWindowPlacement Lib "user32" _
   (ByVal hwnd As Long, lpwndpl As WINDOWPLACEMENT) As Long
   
Public Sub RestoreWindow(sWindowTitle As String)
Dim hWndCtlApp As Long
Dim currWinP As WINDOWPLACEMENT
   
AppRunning = False

  'obtain the handle to the control app
    hWndCtlApp = FindWindow(vbNullString, sWindowTitle)
    If hWndCtlApp Then
     'prepare the WINDOWPLACEMENT type
        currWinP.Length = Len(currWinP)
        If GetWindowPlacement(hWndCtlApp, currWinP) > 0 Then
        'determine the window state
        If currWinP.showCmd = SW_SHOWMINIMIZED Then
           'minimized, so restore
            currWinP.Length = Len(currWinP)
            currWinP.flags = 0&
            currWinP.showCmd = SW_SHOWNORMAL
            Call SetWindowPlacement(hWndCtlApp, currWinP)
            AppRunning = True
         Else
           'on screen, so assure visible
            Call SetForegroundWindow(hWndCtlApp)
            Call BringWindowToTop(hWndCtlApp)
            AppRunning = True
         End If
      End If
   End If
End Sub

It works fine with most of the applications. However, with some app, it returns a value of normal, even though they are minimized.

S
0
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 10861424
Hi Shauli,

>>It works fine with most of the applications. However, with some app, it returns a value of normal, even though they are minimized.
Do you have any specific applications that it Not working? Is this purpose of module is to restore an Application windowstate to restored (normal) ?
0
 
LVL 19

Author Comment

by:Shauli
ID: 10861529
With both of your questions, the answer is yes. I want to restore a specific app, written by someone else, not in vb, to normal if it was minimied, or to bring it to the front if it is not minimized, but not on top.

S
0
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 10866351
I try your code, it works for me.. Do you have a "Specified Window Caption" are you testing?

Here is what i testing so far, using additional ShowWindow, PutFocus API to test it, but basically your function is working for me.

VERSION 5.00
Begin VB.Form frmHwnd
   BorderStyle     =   1  'Fixed Single
   Caption         =   "frmHwnd"
   ClientHeight    =   4845
   ClientLeft      =   45
   ClientTop       =   330
   ClientWidth     =   7500
   LinkTopic       =   "Form1"
   MaxButton       =   0   'False
   MinButton       =   0   'False
   ScaleHeight     =   4845
   ScaleWidth      =   7500
   StartUpPosition =   2  'CenterScreen
   Begin VB.CommandButton Command4
      Caption         =   "Refresh"
      Height          =   375
      Left            =   120
      TabIndex        =   15
      Top             =   2760
      Width           =   1215
   End
   Begin VB.ListBox List2
      Height          =   2595
      Left            =   120
      TabIndex        =   13
      Top             =   120
      Width           =   3375
   End
   Begin VB.CommandButton Command3
      Caption         =   "Restore Window"
      Height          =   375
      Left            =   1560
      TabIndex        =   12
      Top             =   3840
      Width           =   1935
   End
   Begin VB.OptionButton Option1
      Caption         =   "Set Focus"
      Height          =   375
      Index           =   5
      Left            =   5520
      TabIndex        =   11
      Top             =   1560
      Width           =   1695
   End
   Begin VB.CommandButton Command2
      Caption         =   "Get Hwnd"
      Height          =   375
      Left            =   5520
      TabIndex        =   9
      Top             =   600
      Width           =   1575
   End
   Begin VB.CommandButton Command1
      Caption         =   "Close Window"
      Height          =   375
      Left            =   5520
      TabIndex        =   8
      Top             =   120
      Width           =   1575
   End
   Begin VB.OptionButton Option1
      Caption         =   "Normal"
      Height          =   375
      Index           =   4
      Left            =   3720
      TabIndex        =   7
      Top             =   2040
      Width           =   1695
   End
   Begin VB.ListBox List1
      Height          =   1035
      Left            =   3720
      TabIndex        =   6
      Top             =   2520
      Width           =   3615
   End
   Begin VB.TextBox Text1
      Height          =   375
      Left            =   120
      TabIndex        =   5
      Top             =   3240
      Width           =   3375
   End
   Begin VB.CommandButton cmdDisplay
      Caption         =   "Display"
      Height          =   375
      Left            =   3720
      TabIndex        =   4
      Top             =   3840
      Width           =   1575
   End
   Begin VB.OptionButton Option1
      Caption         =   "Maximize"
      Height          =   375
      Index           =   3
      Left            =   3720
      TabIndex        =   3
      Top             =   1560
      Width           =   1695
   End
   Begin VB.OptionButton Option1
      Caption         =   "Hide"
      Height          =   375
      Index           =   2
      Left            =   3720
      TabIndex        =   2
      Top             =   1080
      Width           =   1695
   End
   Begin VB.OptionButton Option1
      Caption         =   "Minimize"
      Height          =   375
      Index           =   1
      Left            =   3720
      TabIndex        =   1
      Top             =   600
      Width           =   1695
   End
   Begin VB.OptionButton Option1
      Caption         =   "Show"
      Height          =   375
      Index           =   0
      Left            =   3720
      TabIndex        =   0
      Top             =   120
      Width           =   1695
   End
   Begin VB.Label Label2
      Appearance      =   0  'Flat
      BackColor       =   &H80000005&
      BorderStyle     =   1  'Fixed Single
      ForeColor       =   &H80000008&
      Height          =   375
      Left            =   120
      TabIndex        =   14
      Top             =   4320
      Width           =   7335
   End
   Begin VB.Label Label1
      Height          =   375
      Left            =   5520
      TabIndex        =   10
      Top             =   1080
      Width           =   1575
   End
End
Attribute VB_Name = "frmHwnd"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long

Private Const SW_HIDE = 0
Private Const SW_SHOW = 5
Private Const SW_MAXIMIZE = 3
Private Const SW_MINIMIZE = 6
Private Const SW_NORMAL = 1

Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Const WM_CLOSE = &H10

Private Declare Function Putfocus Lib "user32" Alias "SetFocus" (ByVal hwnd As Long) As Long
Private Declare Function BringWindowToTop Lib "user32" (ByVal hwnd As Long) As Long

Private Const GW_HWNDFIRST = 0
Private Const GW_HWNDLAST = 1
Private Const GW_HWNDNEXT = 2
Private Const GW_HWNDPREV = 3

Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function IsWindowVisible Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function IsWindowEnabled Lib "user32" (ByVal hwnd As Long) As Long

Private Type POINTAPI
   X As Long
   Y As Long
End Type

Private Declare Function GetCursorPos Lib "user32" _
  (lpPoint As POINTAPI) As Long

Private Declare Function LBItemFromPt Lib "comctl32" _
  (ByVal hwnd As Long, _
   ByVal ptx As Long, _
   ByVal pty As Long, _
   ByVal bAutoScroll As Long) As Long

Dim pt As POINTAPI

Private Sub loadWindows(ctrl As Object)
    Dim hwnd As Variant
    Dim txt As String
    Dim i As Integer
   
    ctrl.Clear
   
    'get first window
    hwnd = GetWindow(Me.hwnd, GW_HWNDFIRST)
    Do
    If GetParent(hwnd) = 0 And IsWindowVisible(hwnd) <> 0 And IsWindowEnabled(hwnd) <> 0 Then
        txt = String(256, 0)
        GetWindowText hwnd, txt, 256
        ctrl.AddItem Left$(txt, InStr(txt, Chr(0)) - 1)
        Debug.Print i & ": " & hwnd & ": " & Left$(txt, InStr(txt, Chr(0)) - 1)
        i = i + 1
    End If
   
    'get the next window from the task list
    hwnd = GetWindow(hwnd, GW_HWNDNEXT)
   
    'keep looping until there's no more app's in the task list
    Loop Until hwnd = 0
End Sub

Private Sub cmdDisplay_Click()
    Dim RetVal As Long, retval1 As Long
    Dim s As String
    List1.Clear
    s = Space(255)
    RetVal = FindWindow(vbNullString, Text1.Text)
    retval1 = GetClassName(RetVal, s, 255)
    List1.AddItem "Window name:  " & Text1.Text
    List1.AddItem "Window Hwnd:  " & RetVal
    List1.AddItem "Classname:  " & s
    List1.AddItem "Characters:  " & retval1
End Sub

Private Sub Command1_Click()
    Dim myHwnd As Long
    myHwnd = FindWindow(vbNullString, Text1.Text)
    i = PostMessage(myHwnd, WM_CLOSE, 0&, 0&)
End Sub

Private Sub Command2_Click()
    Dim RetVal As Long
    s = Space(255)
    RetVal = FindWindow(vbNullString, Text1.Text)
    Label1.Caption = RetVal
End Sub

Private Sub Command3_Click()
    RestoreWindow Text1
End Sub

Private Sub Command4_Click()
    loadWindows List2
End Sub

Private Sub Form_Load()
    Text1.Text = Me.Caption
    cmdDisplay_Click
    Command1.Caption = "Close Window"
    loadWindows List2
End Sub

Private Sub List2_Click()
    Text1 = List2.Text
    cmdDisplay_Click
End Sub

Private Sub List2_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    'list2.ToolTipText = list2.
    GetCursorPos pt
    Label2.Caption = List2.List(LBItemFromPt(List2.hwnd, pt.X, pt.Y, False))
End Sub

Private Sub Option1_Click(index As Integer)
    Dim RetVal As Long, retval1 As Long, retval2 As Long
    Dim iform As Form
    s = Space(255)
    RetVal = FindWindow(vbNullString, Text1.Text)
    retval1 = GetClassName(RetVal, s, 255)
    If Option1.Item(0) Then
        retval2 = ShowWindow(RetVal, SW_SHOW)
    End If
    If Option1.Item(1) Then
        retval2 = ShowWindow(RetVal, SW_MINIMIZE)
    End If
    If Option1.Item(2) Then
        retval2 = ShowWindow(RetVal, SW_HIDE)
        DoEvents
        'Sleep 2000
        'retval2 = ShowWindow(RetVal, SW_SHOW)
        'Putfocus RetVal
    End If
    If Option1.Item(3) Then
        retval2 = ShowWindow(RetVal, SW_MAXIMIZE)
    End If
    If Option1.Item(4) Then
        retval2 = ShowWindow(RetVal, SW_NORMAL)
    End If
    If Option1.Item(5) Then
        retval2 = BringWindowToTop(RetVal)
    End If
End Sub
0
 
LVL 19

Author Comment

by:Shauli
ID: 10868695
Hi ryancys,
Thanks for your reply. It is a specific app that gives me the headache. It is an app written by the association we are members of, to check credits of its members, while applying to our products.
My code works with any other program I test. Just with this one, I cannot restore to NORAML from minimize. I can do almost anything else. I can maximize, bring to front etc.., just not to normal. I'm looking for the reason for such a behaviour. Maybe if I know the reason, I can solve the problem.

Thanks,

S
0
 
LVL 50

Accepted Solution

by:
Ryan Chong earned 100 total points
ID: 10871102
Hi Shauli,

Try call ShowWindow(RetVal, SW_NORMAL) after your RestoreWindow function? Hmm, actually it's hard for me to guess what's the exact problem. Btw, do you have rights, code for that application that cannot restore to Normal? Did you check if the problem come for that exe?

Sorry, can't help much here..

regards
0
 
LVL 19

Author Comment

by:Shauli
ID: 10871173
Unfortunately, I dont have any code, and yes, I do have rights, as I said, I do communicate with this app. The only thing I can't do is to restore it to normal.
Anyway, thanks for the effort, I appreciate it.

S
0
 
LVL 6

Expert Comment

by:LunaSkye
ID: 10872032
Hi,
I dont have an answer .. Yet.. but try this, just to see what happens..

Try filling in a windowplacement structure, and setting the .ShowCmd = SW_MINIMIZE manually, then use that structure in conjunction with SetWindowPlacement.  Force the application into minimized state.  At that point, are you able to read the application as minimized?

I am trying to see if this application is aversed in general to being minimized.  

-Andrew
0
 
LVL 19

Author Comment

by:Shauli
ID: 10872249
Already tried to force normal and minimize. The result is loosing minimize capabilities, which the app has initially.

S
0
 
LVL 19

Author Comment

by:Shauli
ID: 10877064
ryancys,

Thanks for the effort.

S
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

773 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