Minimize, maximize, restore other window.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Minimize, maximize, restore other window.

Hybrid View

  1. #1
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,840

    Minimize, maximize, restore other window.

    Is there a way to make my application tell something else to minimize/maximize/restore itself.

    Example: My form commands Windows Media Player to minimize, but not just Media Player. I want to minimize/maximize/restore any window.

  2. #2
    Join Date
    Nov 2002
    Location
    Baby Land
    Posts
    646
    assuming you already knew the window handle, you can use SendMessage with WM_SYSCOMMAND message and SC_CLOSE, SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE constant

  3. #3
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,840

    Newbie.

    I am just a newbie, could you give me a rundown of each of those?

  4. #4
    Join Date
    Nov 2002
    Location
    Baby Land
    Posts
    646
    PHP Code:
    Private Const SW_MAXIMIZE As Long 3
    Private Const SW_MINIMIZE As Long 6
    Private Const SW_RESTORE As Long 9

    Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As StringByVal lpWindowName As String) As Long
    Private Declare Function ShowWindow Lib "user32.dll" (ByVal hwnd As LongByVal nCmdShow As Long) As Long


    Private Sub cmdMax_Click()
      
    Dim noteHwnd As Long
      
    'Find notepad window, class name can be found using Spy++
      noteHwnd = FindWindow("Notepad", vbNullString)
      
      '
    or if you know the exact window title
      noteHwnd 
    FindWindow(vbNullString"Untitled - Notepad")
      If 
    noteHwnd <> 0 Then
        
    'maximize the window
        ShowWindow noteHwnd, SW_MAXIMIZE
      End If
    End Sub 

  5. #5
    Join Date
    Jun 2013
    Posts
    1

    Re: Minimize, maximize, restore other window.

    Many thanks for posting this, Luthv! It works perfectly for my purposes, which was to find a previously opened window and bring it to the front, whether it is minimized or sits behind other windows. I modified the code to add restore, then maximize, to account for all issues.

    Private Declare Function GetForegroundWindow Lib "user32" () As Long
    Private Declare Function ShowWindow Lib "user32.dll" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long

    Private Const SW_MAXIMIZE As Long = 3
    Private Const SW_MINIMIZE As Long = 6
    Private Const SW_RESTORE As Long = 9


    Public Function FindAndMaximizeWindow(strWindowName) As Boolean
    Dim noteHwnd As Long
    'Find notepad window, class name can be found using Spy++
    noteHwnd = FindWindow(strWindowName, vbNullString)

    'or if you know the exact window title
    noteHwnd = FindWindow(vbNullString, strWindowName)
    If noteHwnd <> 0 Then
    'maximize the window
    ShowWindow noteHwnd, SW_RESTORE
    ShowWindow noteHwnd, SW_MAXIMIZE
    FindAndMaximizeWindow = True
    Else
    FindAndMaximizeWindow = False
    End If
    End Function

  6. #6
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,840

    Solved

    Thank you Luthv for the help. I have come up with the solution without specifying a certain program.

    Private Declare Function GetForegroundWindow Lib "user32" () As Long
    Private Declare Function ShowWindow Lib "user32.dll" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long

    Private Const SW_MAXIMIZE As Long = 3
    Private Const SW_MINIMIZE As Long = 6
    Private Const SW_RESTORE As Long = 9


    ' Enter this in Sub-Routine
    Ret = GetForegroundWindow()
    ShowWindow Ret, SW_MAXIMIZE

    This code will minimize whatever the active window is at the time.

  7. #7
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,875

    Re: Minimize, maximize, restore other window.

    12 years later???
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center