Solved

Giving Focus to an App with this hWND

Posted on 2000-03-16
6
896 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
Comment Utility
SetForegroundWindow()

Guess what. Yes.
0
 
LVL 14

Accepted Solution

by:
mcrider earned 20 total points
Comment Utility
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
Comment Utility
Huh? You've already had this answered in your other question.
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 14

Expert Comment

by:mcrider
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for the points! Glad I could help!


Cheers!®©
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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

743 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

12 Experts available now in Live!

Get 1:1 Help Now