Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Detect "Switch to" in Task manager

Posted on 2006-06-15
3
Medium Priority
?
291 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 2000 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

Independent Software Vendors: 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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Suggested Courses

722 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