0

I want a event handler when a window is minimized. I am reading different posts in stackoverflow but not finding a complete solution. My actual issue is described here

Similar to what is asked here, I want a handle of DefWindowProc in C#. So that I can check if message is WM_ACTIVATEAPP, if the app is activated/de-activated I want to do some business logic.

This answer is somwwhat helpful but doesn't explain completely - like how will check the message in my application. Do I need to override DefWindowProc ?

Any pointers will be helpful.

Community
  • 1
  • 1
Kevin Morocco
  • 179
  • 1
  • 2
  • 10
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Feb 26 '14 at 21:59
  • 1
    This page lists code that allows you to hook the window procedure of controls, maybe you can apply it to a Form too: http://mrpfister.com/programming/subclassing-net-compact-framework-controls/ – pasztorpisti Feb 26 '14 at 22:42

1 Answers1

1

What about checking for the Form change events:

A simple test form is 'minimized' by the (X) click and re-activated using TaskManager:

Load Form:
635291025619140000: Got Form1_Resize()
635291025619950000: Got Form1_Resize()
635291025620060000: Got Form1_Resize()
635291025620300000: Got Form1_Resize()
635291025620470000: Got Form1_Activated()
635291025620880000: Got Form1_GotFocus()

Click on (X) to minimize:
635291026059340000: Got Form1_Resize()
635291026059780000: Got Form1_Deactivate()
635291026059950000: Got Form1_LostFocus()

TaskManager SwitchTo:
635291026483820000: Got Form1_Resize()
635291026483910000: Got Form1_Activated()
635291026484010000: Got Form1_GotFocus()

Press Win-Symbol of Form (shows Start screen):
635291026724060000: Got Form1_Deactivate()
635291026724960000: Got Form1_LostFocus()

Click (X) in Start screen:
635291027163520000: Got Form1_Resize()
635291027163610000: Got Form1_Activated()
635291027163710000: Got Form1_GotFocus()

Click (X) of Form:
635291027673050000: Got Form1_Resize()
635291027673470000: Got Form1_Deactivate()
635291027673660000: Got Form1_LostFocus()

Re-Launched externally:
635291028243720000: Got Form1_Resize()
635291028243900000: Got Form1_Activated()
635291028244010000: Got Form1_GotFocus()

You see you always get Deactivated+LostFocus or Activated+GotFocus. You can use Deactivated+LostFocus to catch for the form will be minimized/hidden. Is that what you are looking for?

josef
  • 5,951
  • 1
  • 13
  • 24
  • Did that test app include controls on the form? I seemed to recall that a Form will not get focus event unless there are no control on it? – tcarvin Feb 28 '14 at 16:49
  • Right, the form is empty. If not empty, the default or last used element will get the focus message. – josef Mar 01 '14 at 06:30