Solved

Detect "Switch to" in Task manager

Posted on 2006-06-15
3
286 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

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.

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

813 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