Results 1 to 8 of 8

Thread: AppActivate & Windows 2000

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2000
    Posts
    11

    AppActivate & Windows 2000

    Does anybody know function (API?) replacement for AppActivate?
    In Win2000 AppActivate doesn't work

    I'm using VB5SP3 on Win2000

    Many thanx

  2. #2
    Frenzied Member Shawn N's Avatar
    Join Date
    Dec 2001
    Location
    Houston
    Posts
    1,631
    VB Code:
    1. 'Code by Mahbub ([email protected])
    2. 'This example requires two forms with on each for one command button.
    3. 'Both buttons should be named 'Command1'
    4.  
    5. 'In Form1
    6. Private Sub Command1_Click()
    7.     ' Make the window Form2 the active window for the program.  Note that
    8.     ' this function will not make the window the foreground window if the user is currently
    9.     ' working with a separate program.
    10.     Dim retval As Long  ' return value
    11.     retval = SetActiveWindow(Form2.hwnd)  ' set Form2 as the application's active window
    12. End Sub
    13. Private Sub Form_Load()
    14.     Form2.Show
    15. End Sub
    16.  
    17. 'In Form2:
    18. Private Sub Command1_Click()
    19.     Dim retval As Long  ' return value
    20.     retval = SetActiveWindow(Form1.hwnd)  ' set Form1 as the application's active window
    21. End Sub
    22.  
    23. 'In a module:
    24. Declare Function SetActiveWindow Lib "user32.dll" (ByVal hwnd As Long) As Long
    Please rate my post.

  3. #3
    Addicted Member
    Join Date
    Sep 2002
    Location
    Middle Earth
    Posts
    156
    If you want foreground window behavior also, then you may
    opt to try:

    void SwitchToThisWindow (HWND hWnd, BOOL bRestore); // User32.dll

    You'll have to write the VB Declare statement to port it over,
    but it will definately do the trick. BTW, use this function as a last
    resort because Microsoft does not support it.

  4. #4

    Thread Starter
    New Member
    Join Date
    Sep 2000
    Posts
    11
    Thanx for your answers, but i still have problems...

    SetActiveWindow returns 0
    SwitchToThisWindow does nothing

    Can you take a look at my prog? what's wrong?

    VB Code:
    1. Public Const GW_HWNDNEXT = 2
    2.  
    3. Public Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
    4. Public Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, _
    5.   ByVal wCmd As Long) As Long
    6. Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
    7.   (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    8.  
    9. Public Declare Function GetWindowThreadProcessId Lib "user32" _
    10.   (ByVal hwnd As Long, lpdwprocessid As Long) As Long
    11.  
    12. Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
    13.         (ByVal hwnd As Long, _
    14.         ByVal lpOperation As String, _
    15.         ByVal lpFile As String, _
    16.         ByVal lpParameters As String, _
    17.         ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    18.  
    19. Declare Function SetActiveWindow Lib "user32" (ByVal hwnd As Long) As Long
    20. Declare Function SwitchToThisWindow Lib "user32" (ByVal hwnd As Long, ByVal fAltTab As Boolean)
    21. Declare Function GetLastError Lib "kernel32" () As Long
    22.  
    23. Public Function ProcIDFromWnd(ByVal hwnd As Long) As Long
    24.    Dim idProc As Long
    25.  
    26.    ' Get PID for this HWnd
    27.    GetWindowThreadProcessId hwnd, idProc
    28.    ProcIDFromWnd = idProc
    29. End Function
    30.      
    31. Public Function GetWinHandle(hInstance As Long) As Long
    32.    Dim tempHwnd As Long
    33.      
    34.    ' Grab the first window handle that Windows finds:
    35.    tempHwnd = FindWindow(vbNullString, vbNullString)
    36.    
    37.    ' Loop until you find a match or there are no more window handles:
    38.    Do Until tempHwnd = 0
    39.       ' Check if no parent for this window
    40.       If GetParent(tempHwnd) = 0 Then
    41.          ' Check for PID match
    42.          If hInstance = ProcIDFromWnd(tempHwnd) Then
    43.             ' Return found handle
    44.             GetWinHandle = tempHwnd
    45.             ' Exit search loop
    46.             Exit Do
    47.          End If
    48.       End If
    49.    
    50.       ' Get the next window handle
    51.       tempHwnd = GetWindow(tempHwnd, GW_HWNDNEXT)
    52.    Loop
    53.  
    54. End Function
    55.  
    56. Private Sub Command1_Click()
    57.  
    58. progID = Shell("notepad.exe", vbNormalFocus)
    59.        
    60. For i = 1 To 500000
    61.     DoEvents
    62. Next i
    63.  
    64. progID = GetWinHandle(progID)
    65. tempID = SetActiveWindow(progID)
    66.  
    67. End Sub
    Last edited by krokodil; Feb 12th, 2003 at 06:47 AM.

  5. #5
    Frenzied Member Shawn N's Avatar
    Join Date
    Dec 2001
    Location
    Houston
    Posts
    1,631
    When I open notepad with vbNormalFocus, it already has focus.
    Please rate my post.

  6. #6

    Thread Starter
    New Member
    Join Date
    Sep 2000
    Posts
    11
    Originally posted by Shawn N
    When I open notepad with vbNormalFocus, it already has focus.
    Even if app which are you shelling from had not focus?

    Sorry for my english, i'm beginner

  7. #7
    Frenzied Member Shawn N's Avatar
    Join Date
    Dec 2001
    Location
    Houston
    Posts
    1,631
    notepad
    Please rate my post.

  8. #8

    Thread Starter
    New Member
    Join Date
    Sep 2000
    Posts
    11
    Originally posted by Shawn N
    notepad
    I mean, if the VB application which runs shell hadn't focus.

    Look:
    If MyVbApp have focus then notepad have focus
    If MyVbApp have not focus (e.g. desktop have focus), also the notepad haven't focus

    But If i run second shell immediately after notepad, i can set focus to this shell by API, but i can't set focus to the first shell (notepad).

    Hope you understand my english

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