Solved

Detect "Switch to" in Task manager

Posted on 2006-06-15
3
284 Views
Last Modified: 2013-12-03
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
Comment
Question by:ahammar
  • 2
3 Comments
 
LVL 39

Accepted Solution

by:
appari earned 500 total points
ID: 16918353
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
 
LVL 23

Author Comment

by:ahammar
ID: 16924039
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
 
LVL 39

Expert Comment

by:appari
ID: 16924363
button2 i used for stopping the subclassing. the same code i called from unload also.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

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

13 Experts available now in Live!

Get 1:1 Help Now