0

I have a form that that minimizes on deactivate but now the task bar button does not minimize the form. It goes away for an instance then comes back. There seems to be a race with the task bar function and the deactivate event where when I click the icon the form minimizes, then the task bar function sees that it is minimized and the form restores. I have the following code:

 public Form1()
 {
    InitializeComponent();
    Text = string.Empty;
    ControlBox = false;
    FormBorderStyle = FormBorderStyle.SizableToolWindow;
    Deactivate += lostFocus;
 }

 private void lostFocus(object o, EventArgs e)
 {
     WindowState = FormWindowState.Minimized;
 }

 protected override void WndProc(ref Message message)
 {
      const int WM_NCHITTEST = 0x0084;
      if (message.Msg == WM_NCHITTEST) return;
      base.WndProc(ref message);
 }

(Note that the form has no controls)

I was able to get it to kind of work with the following bandade:

 private void focusTimerEvent(object o, EventArgs e)
 {
      WindowState = FormWindowState.Minimized;
      focusTimer.Stop();
 }

 private void lostFocus(object o, EventArgs e)
 {
      focusTimer.Start();
 }

and setting the timer to 1000 in Form1() method

 focusTimer = new Timer();
 focusTimer.Interval = 1000;
 focusTimer.Tick += focusTimerEvent;

any suggestions would be appreciated

user1819780
  • 105
  • 11
  • 1
    This is all perfectly normal. The mouse-down on the taskbar button changes the focus to the taskbar. Your form sees this and minimizes itself. The mouse-up then operates the taskbar button. Which sees that the window for it is minimized so it restores the window. A Timer is a valid workaround, of sorts. It still won't work if you hold the button down for more than a second. Alternatives are very painful. Better to not just do this but you completely forgot explaining why you want to do this so we can't give better suggestions. – Hans Passant Jul 09 '13 at 09:09
  • What I want is when ever the mouse is pressed anywhere other than the form, I want the form to minimize; – user1819780 Jul 09 '13 at 09:22
  • 1
    Yeah, that was already obvious. It isn't obvious *why* it needs to work that way, clearly the user already has a very good way to minimize the window. Small popup windows like a ContextMenu use mouse capture instead. And of course they don't have a taskbar button, that solves the problem too. – Hans Passant Jul 09 '13 at 09:27

0 Answers0