Solved

Giving Focus to an App with this hWND

Posted on 2000-03-16
6
911 Views
Last Modified: 2008-02-26
I have used the GetForegroundWindow() fn. to obtain the handle to the window who currently has focus.  Now i am planning on moving the focus from this app to another via the AppActivate() fn.  What i want to know is, is there an function i can call that will allow me to set the original app as having focus again once i am done?  Specifically, is there a SetForegroundWindow() fn so that i may pass the hWnd that i got from GetForegroundWindow()?

Thanks
0
Comment
Question by:aniston
  • 3
  • 3
6 Comments
 
LVL 10

Expert Comment

by:caraf_g
ID: 2623477
SetForegroundWindow()

Guess what. Yes.
0
 
LVL 14

Accepted Solution

by:
mcrider earned 20 total points
ID: 2623481
I already posted this in your previous question but here it is again:

Add the following to a module:

    ' ShowWindow() Commands
    Public Const SW_HIDE = 0
    Public Const SW_SHOWNORMAL = 1
    Public Const SW_NORMAL = 1
    Public Const SW_SHOWMINIMIZED = 2
    Public Const SW_SHOWMAXIMIZED = 3
    Public Const SW_MAXIMIZE = 3
    Public Const SW_SHOWNOACTIVATE = 4
    Public Const SW_SHOW = 5
    Public Const SW_MINIMIZE = 6
    Public Const SW_SHOWMINNOACTIVE = 7
    Public Const SW_SHOWNA = 8
    Public Const SW_RESTORE = 9
    Public Const SW_SHOWDEFAULT = 10
    Public Const SW_MAX = 10
     
    Declare Function SetFocusAPI Lib "user32" Alias "SetFocus" (ByVal hwnd As Long) As Long
    Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
    Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long


Then you can do:

   SetFocusAPI iVal
   SetForegroundWindow iVal

You may also want to show the window if it's minimized:

   ShowWindow iVal, SW_SHOWNORMAL



Cheers!®©
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 2623482
Huh? You've already had this answered in your other question.
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 14

Expert Comment

by:mcrider
ID: 2623487
By the way, if you want to know if the window is minimized and you need to show it, add the following to a module:

    Public Const WS_MINIMIZE = &H20000000
    Public Const GWL_STYLE = (-16)
    Declare Function GetWindowLong Lib "user32" _
        Alias "GetWindowLongA" (ByVal hWnd As Long, _
        ByVal nIndex As Long) As Long
    Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
     

Then in a subroutine, you can do:

   Dim lWinfo As Long
   Dim lhWnd As Long

   lhWnd = FindWindow(vbNullString, "Win32api.txt - WordPad")
   lWinfo = GetWindowLong(lhWnd, GWL_STYLE)
   If (lWinfo And WS_MINIMIZE) = WS_MINIMIZE Then
       Debug.Print "minimized"
   Else
       Debug.Print "not minimized"
   End If



By the way, I am using the FindWindow API above to find the window handle... To use it, all you have to do is pass the title of the window as the 2nd argument...


Cheers!®©
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 2623548
Cool. Behold the "Answer Grade" at the top of the thread. I only just spotted it now. So now you can get a clue whether an answer is worth looking at!
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2623567
Thanks for the points! Glad I could help!


Cheers!®©
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MsgBox 2 59
Saving history changes to sub form 4 48
Add and format columns in vb6 7 63
Using an encrypted  value to decrypt and display contents in vb6 9 55
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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

861 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