Results 1 to 13 of 13

Thread: [RESOLVED] Maximise active window

  1. #1

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Resolved [RESOLVED] Maximise active window

    Hi Guys,

    Anyone got any idea how to use an API to maximise whatever program currently has focus?
    This is what I have so far:

    vb.net Code:
    1. Private Declare Function GetForeGroundWindow Lib "user32" Alias "GetForegroundWindow" () As IntPtr
    2.  
    3. Private Declare Function ShowWindow Lib "user32" Alias "ShowWindow" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
    4.  
    5. Private Const SW_MAXIMIZE = 3
    Then I'm using it like this:
    vb.net Code:
    1. ShowWindow(GetForeGroundWindow, SW_MAXIMIZE)

    However, that just makes the active window dissapear for some reason... it doesnt close it, the process still remains running but it just hides the window

    Anyone got any better methods or point out what I'm doing wrong?

    Cheers
    Chris
    Last edited by chris128; Aug 1st, 2008 at 09:38 AM.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  2. #2
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Maximise active window

    That should work but your GetForegroundWindow API ends with " As IntPtr", the VB6 API viewer show it "As Long"

    Private Declare Function GetForegroundWindow Lib "user32" Alias "GetForegroundWindow" () As Long

  3. #3
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    Re: Maximise active window

    hai chris,
    actually you try to maximize the window or close it or what

  4. #4

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Maximise active window

    Quote Originally Posted by Fazi
    hai chris,
    actually you try to maximize the window or close it or what
    I am trying to maximize it... but when I use the code I posted, the window gets closed and that is not what I want.
    Last edited by chris128; Aug 1st, 2008 at 01:20 PM.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  5. #5
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    Re: Maximise active window

    sorry chris,
    i am vb6 and in vb6 that code look like ok

  6. #6

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Maximise active window

    Quote Originally Posted by Edgemeal
    That should work but your GetForegroundWindow API ends with " As IntPtr", the VB6 API viewer show it "As Long"

    Private Declare Function GetForegroundWindow Lib "user32" Alias "GetForegroundWindow" () As Long
    Actually that bit works fine as I've been using GetForegroundWindow in other projects and havent had a problem. Its just the ShowWindow function that isnt working as I want it to
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  7. #7

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Maximise active window

    no one else got any ideas?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  8. #8
    Fanatic Member TTn's Avatar
    Join Date
    Jul 2004
    Posts
    685

    Re: Maximise active window

    I'm pretty sure IntPtr is the problem.
    I don't even think it exists in Vb6. Try it with a Long, if this is vb6.
    It's probably assuming a variant type.

    In .net it exists but it's not really the correct data type to match the ShowWindow function parameter.
    Try all Longs as Integers, or Int32 instead.

    There are a couple of other API's that will do what you want.
    For example SetWindowPlacement, is the next one that comes to mind.

  9. #9

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Maximise active window

    Thanks I'll give that a go Oh and I'm using VB.NET btw, not VB6.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  10. #10
    Fanatic Member TTn's Avatar
    Join Date
    Jul 2004
    Posts
    685

    Re: Maximise active window

    Code:
    Const SW_MAXIMIZE As Int32 = 3 
    Private Declare Function apiGetForeGroundWindow Lib "user32" Alias "GetForegroundWindow" () As Int32
    Private Declare Function apiShowWindow Lib "user32" Alias "ShowWindow" (ByVal hwnd As Int32, ByVal nCmdShow As Int32) As Int32
    Code:
    apiShowWindow(apiGetForeGroundWindow, SW_MAXIMIZE)
    Works fine here.


    _

  11. #11

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Maximise active window

    Quote Originally Posted by TTn
    Code:
    Const SW_MAXIMIZE As Int32 = 3 
    Private Declare Function apiGetForeGroundWindow Lib "user32" Alias "GetForegroundWindow" () As Int32
    Private Declare Function apiShowWindow Lib "user32" Alias "ShowWindow" (ByVal hwnd As Int32, ByVal nCmdShow As Int32) As Int32
    Code:
    apiShowWindow(apiGetForeGroundWindow, SW_MAXIMIZE)
    Works fine here.


    _
    Works perfectly thanks!
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  12. #12
    Fanatic Member TTn's Avatar
    Join Date
    Jul 2004
    Posts
    685

    Re: [RESOLVED] Maximise active window

    Good.

    As you notice, I've put a prefix on the function call, so it is an actual Alias with a different name than the original function.
    I've also declared the data type for the constant.
    The data types match those, where they are being passed.

    Stick to those rules consistently, and you'll be fine most of the time.

  13. #13

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [RESOLVED] Maximise active window

    Will do
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


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