Solved

Detect "Switch to" in Task manager

Posted on 2006-06-15
3
289 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
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…

759 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