Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 292
  • Last Modified:

Detect "Switch to" in Task manager

I can detect that task manager tries to "End Task" my app by using:  If UnloadMode = 3

How can I have my app detect if task manager tries to switch to it by using "Switch to"
In other words, If the user uses task manager, selects my app and chooses "Switch to", how can I have my app know that.
My app's form is hidden.  I want the user to be able to show it only by using "Switch to" in task manager so I need some way for my app to know that.
I've tried the gotfocus and Activate events, but niether of them run.
Thanks...

Cheers!
ahammar
0
ahammar
Asked:
ahammar
  • 2
1 Solution
 
appariCommented:
you have to use subclassing for this purpose.
try something like this

AS THE following code uses windows subclassing if you run it from IDE dont press stop button from the debug menu. if you do it it will crash your IDE. its better to build the exe and then test.

add the following code to a module
'************************** Module
' API declerations
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

' Constants
Public Const GWL_WNDPROC = (-4)
Public Const WM_ACTIVATEAPP = &H1C
Public Const WM_ACTIVATE = &H6


' Variables
Public lngOldProc As Long ' This is for preserving the old address of the callback



Public Function WindowProc(ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Select Case Msg
    Case WM_ACTIVATEAPP
        If wParam Then
             Form1.Show
        End If
    End Select
           
   
    ' The CallWindowProc API will execute a callback function when given its location.
    ' Here we pass the lngOldProc, the address for the old callback function. We also pass the message and parameters.
    ' So basicly, this just performs what would normally happen.
    WindowProc = CallWindowProc(lngOldProc, hWnd, Msg, wParam, lParam)
End Function



'************************** Module END

in form1 add two buttons button1 and button2 and add the following code to form1,

'*************************** Form1 code

Option Explicit

Private Sub Command1_Click()
    Me.Hide
End Sub

Private Sub Command2_Click()
SetWindowLong hWnd, GWL_WNDPROC, lngOldProc

End Sub

Private Sub Form_Load()
    lngOldProc = SetWindowLong(Me.hWnd, GWL_WNDPROC, AddressOf WindowProc)
    ' lngOldProc now holds the old addrss of the callback function.
    ' SetWindowLongA is the API that writes the new value.
    ' GWL_WNDPROC this constant defines what we are changing.
    ' AddressOf WindowProc, this gets the memory location of our callback function.
End Sub


Private Sub Form_Unload(Cancel As Integer)
      SetWindowLong hWnd, GWL_WNDPROC, lngOldProc

End Sub

'*************************** Form1 code End

make exe and run it
0
 
ahammarAuthor Commented:
Thanks appari,
It works fine.  I'm not sure what button 2 is for except to simply stop it from working.  I added a button 3 and put the form_load code in it so if I click button 2, then button 3 it still works.  So I assume button 2 just stops it from working.  I shouldn't need that part, but it's nice to know for in the futere.
I really didn't think any one was going to be able to answer this after doing a search and found others with the same question and they all concluded that it couldn't be done, so I tip my hat to you.  Thanks a bunch!  Good Work!

Cheers!
ahammar
0
 
appariCommented:
button2 i used for stopping the subclassing. the same code i called from unload also.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now