How do I intercept a close event?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • MrNobody

    How do I intercept a close event?

    How do I change a window's behavior so when someone clicks the little 'X' in
    the upper right corner it actually hides the dialog, instead of disposing it?
  • Peter Jausovec

    #2
    Re: How do I intercept a close event?

    Hi,

    In Closing event set e.Cancel to true and WindowState to
    FormWindowState .Minimized.

    --
    Regards,
    Peter Jausovec
    (http://blog.jausovec.net)
    "MrNobody" <MrNobody@discu ssions.microsof t.com> je napisal v sporoèilo
    news:730480C3-5668-4504-959C-FEF74807FCB5@mi crosoft.com ...[color=blue]
    > How do I change a window's behavior so when someone clicks the little 'X'
    > in
    > the upper right corner it actually hides the dialog, instead of disposing
    > it?[/color]


    Comment

    • Greg Bacchus

      #3
      Re: How do I intercept a close event?

      One note on that though. You should first check if the computer is
      being shut down, otherwise the form will not automatically be closed
      and the computer will not turn off.


      private void MainForm_Closin g(object sender,
      System.Componen tModel.CancelEv entArgs e)
      {
      if( !Environment.Ha sShutdownStarte d )
      {
      e.Cancel = true;
      this.Hide();
      }
      }

      Comment

      • Tinius

        #4
        Re: How do I intercept a close event?

        I cannot get Environment.Has ShutdownStarted to work.
        I placed it in;
        private void MainForm_Closin g(object sender,
        System.Componen tModel.CancelEv entArgs e)

        but it always returns false, even when shutting down WindowsXP prof.

        I placed this MessageBox in the same routine;
        MessageBox.Show (Environment.Ha sShutdownStarte d.ToString(),"S hutting Down?");
        which always returns FALSE.

        I'm at a loss with how to differentiate between the User closing the app
        down and Windows closing the app down

        I even wrote a second very simple app and placed the
        Environment.Has ShutdownStarted in the form1_closing event. Still always
        returns false.

        I must be missing something obvious?

        Tinius


        "Greg Bacchus" wrote:
        [color=blue]
        > One note on that though. You should first check if the computer is
        > being shut down, otherwise the form will not automatically be closed
        > and the computer will not turn off.
        >
        >
        > private void MainForm_Closin g(object sender,
        > System.Componen tModel.CancelEv entArgs e)
        > {
        > if( !Environment.Ha sShutdownStarte d )
        > {
        > e.Cancel = true;
        > this.Hide();
        > }
        > }
        >
        >[/color]

        Comment

        • Branimir Giurov [C# MVP]

          #5
          Re: How do I intercept a close event?

          Hi Tinius,

          I think that the easiest way of capturing the close event of the form, is
          in fact to capture the WM_CLOSE message that's being send to the form through
          the message loop of the OS. Here's an easy way for doing that

          //insert the following code inside your main form class:

          const int WM_CLOSE = 16;

          protected override void WndProc(ref Message m)
          {
          base.WndProc (ref m);

          if ( WM_CLOSE == m.Msg )
          {
          //closing
          Trace.WriteLine (Application.Ex ecutablePath + " is exiting !!!");
          }
          }

          Cheers,
          Branimir

          Branimir Giurov
          C# MVP, MCSD, MCDBA, MCT
          CTO, BSH Ltd.
          Langkah Aman: Bermain di Situs Slot Online Legal

          [color=blue]
          > I cannot get Environment.Has ShutdownStarted to work.
          > I placed it in;
          > private void MainForm_Closin g(object sender,
          > System.Componen tModel.CancelEv entArgs e)
          > but it always returns false, even when shutting down WindowsXP prof.
          >
          > I placed this MessageBox in the same routine;
          > MessageBox.Show (Environment.Ha sShutdownStarte d.ToString(),"S hutting
          > Down?"); which always returns FALSE.
          >
          > I'm at a loss with how to differentiate between the User closing the
          > app down and Windows closing the app down
          >
          > I even wrote a second very simple app and placed the
          > Environment.Has ShutdownStarted in the form1_closing event. Still
          > always returns false.
          >
          > I must be missing something obvious?
          >
          > Tinius
          >
          > "Greg Bacchus" wrote:
          >[color=green]
          >> One note on that though. You should first check if the computer is
          >> being shut down, otherwise the form will not automatically be closed
          >> and the computer will not turn off.
          >>
          >> private void MainForm_Closin g(object sender,
          >> System.Componen tModel.CancelEv entArgs e)
          >> {
          >> if( !Environment.Ha sShutdownStarte d )
          >> {
          >> e.Cancel = true;
          >> this.Hide();
          >> }
          >> }[/color][/color]


          Comment

          • Tinius

            #6
            Re: How do I intercept a close event?

            Hi Branimir and thanks for the suggestion

            I tried what you suggested and its works fine for detecting when the User
            closes the app. The WM_CLOSE is received directly after MainForm_Closin g gets
            executed.
            I still am in the situation where I cannot find a way of determining when
            the App is closed via Shutdown.

            Tinius

            "Branimir Giurov [C# MVP]" wrote:
            [color=blue]
            > Hi Tinius,
            >
            > I think that the easiest way of capturing the close event of the form, is
            > in fact to capture the WM_CLOSE message that's being send to the form through
            > the message loop of the OS. Here's an easy way for doing that
            >
            > //insert the following code inside your main form class:
            >
            > const int WM_CLOSE = 16;
            >
            > protected override void WndProc(ref Message m)
            > {
            > base.WndProc (ref m);
            >
            > if ( WM_CLOSE == m.Msg )
            > {
            > //closing
            > Trace.WriteLine (Application.Ex ecutablePath + " is exiting !!!");
            > }
            > }
            >
            > Cheers,
            > Branimir
            >
            > Branimir Giurov
            > C# MVP, MCSD, MCDBA, MCT
            > CTO, BSH Ltd.
            > www.sofiadev.org
            >[/color]

            Comment

            • Sean Hederman

              #7
              Re: How do I intercept a close event?

              How about WM_QUERYENDSESS ION?

              "Tinius" <Tinius@discuss ions.microsoft. com> wrote in message
              news:F48AA025-971A-42FC-9BED-34AFA7A13515@mi crosoft.com...[color=blue]
              > Hi Branimir and thanks for the suggestion
              >
              > I tried what you suggested and its works fine for detecting when the User
              > closes the app. The WM_CLOSE is received directly after MainForm_Closin g
              > gets
              > executed.
              > I still am in the situation where I cannot find a way of determining when
              > the App is closed via Shutdown.
              >
              > Tinius
              >
              > "Branimir Giurov [C# MVP]" wrote:
              >[color=green]
              >> Hi Tinius,
              >>
              >> I think that the easiest way of capturing the close event of the form, is
              >> in fact to capture the WM_CLOSE message that's being send to the form
              >> through
              >> the message loop of the OS. Here's an easy way for doing that
              >>
              >> //insert the following code inside your main form class:
              >>
              >> const int WM_CLOSE = 16;
              >>
              >> protected override void WndProc(ref Message m)
              >> {
              >> base.WndProc (ref m);
              >>
              >> if ( WM_CLOSE == m.Msg )
              >> {
              >> //closing
              >> Trace.WriteLine (Application.Ex ecutablePath + " is exiting !!!");
              >> }
              >> }
              >>
              >> Cheers,
              >> Branimir
              >>
              >> Branimir Giurov
              >> C# MVP, MCSD, MCDBA, MCT
              >> CTO, BSH Ltd.
              >> www.sofiadev.org
              >>[/color]
              >[/color]


              Comment

              • Branimir Giurov [C# MVP]

                #8
                Re: How do I intercept a close event?

                Good call :)

                Cheers,
                Branimir

                Branimir Giurov
                C# MVP, MCSD, MCDBA, MCT
                CTO, BSH Ltd.
                Langkah Aman: Bermain di Situs Slot Online Legal

                [color=blue]
                > How about WM_QUERYENDSESS ION?
                >
                > "Tinius" <Tinius@discuss ions.microsoft. com> wrote in message
                > news:F48AA025-971A-42FC-9BED-34AFA7A13515@mi crosoft.com...
                >[color=green]
                >> Hi Branimir and thanks for the suggestion
                >>
                >> I tried what you suggested and its works fine for detecting when the
                >> User
                >> closes the app. The WM_CLOSE is received directly after
                >> MainForm_Closin g
                >> gets
                >> executed.
                >> I still am in the situation where I cannot find a way of determining
                >> when
                >> the App is closed via Shutdown.
                >> Tinius
                >>
                >> "Branimir Giurov [C# MVP]" wrote:
                >>[color=darkred]
                >>> Hi Tinius,
                >>>
                >>> I think that the easiest way of capturing the close event of the
                >>> form, is
                >>> in fact to capture the WM_CLOSE message that's being send to the
                >>> form
                >>> through
                >>> the message loop of the OS. Here's an easy way for doing that
                >>> //insert the following code inside your main form class:
                >>>
                >>> const int WM_CLOSE = 16;
                >>>
                >>> protected override void WndProc(ref Message m)
                >>> {
                >>> base.WndProc (ref m);
                >>> if ( WM_CLOSE == m.Msg )
                >>> {
                >>> //closing
                >>> Trace.WriteLine (Application.Ex ecutablePath + " is exiting !!!");
                >>> }
                >>> }
                >>> Cheers,
                >>> Branimir
                >>> Branimir Giurov
                >>> C# MVP, MCSD, MCDBA, MCT
                >>> CTO, BSH Ltd.
                >>> www.sofiadev.org[/color][/color][/color]


                Comment

                • Tinius

                  #9
                  Re: How do I intercept a close event?

                  Hi Sean and Branimir

                  Thanks, it works now exactly as I wanted it to.

                  But I'm surprised by the C# environment lacking a high level command to deal
                  with such a basic issue.

                  I previously had code in the MainForm_Closin g event as I was using e.cancel
                  to prevent actual close down. I moved this code to the WM_CLOSE intercept,
                  and didn't pass on the WM_CLOSE back to the system, thus reproducing e.cancel.

                  I used the WM_QUERYENDSESS ION to detect Windows Shutdown to do some final
                  tidying up.

                  I come from a VB background which has can detect the following 5 ways that
                  may be causing an app to close down.
                  vbFormControlMe nu 0 The user chose the Close command from the Control menu
                  on the form.
                  vbFormCode 1 The Unload statement is invoked from code.
                  vbAppWindows 2 The current Microsoft Windows operating environment session
                  is ending.
                  vbAppTaskManage r 3 The Microsoft Windows Task Manager is closing the
                  application.
                  vbFormMDIForm 4 An MDI child form is closing because the MDI form is
                  closing.
                  vbFormOwner 5 A form is closing because its owner is closing.

                  I see now that I can do all this by intercepting the appropriate messages
                  but hope that in the next C# upgrade, we will be given the high level
                  funtions to perform these.

                  Thanks once again

                  Tinius

                  "Branimir Giurov [C# MVP]" wrote:
                  [color=blue]
                  > Good call :)
                  >
                  > Cheers,
                  > Branimir
                  >
                  > Branimir Giurov
                  > C# MVP, MCSD, MCDBA, MCT
                  > CTO, BSH Ltd.
                  > www.sofiadev.org
                  >[color=green]
                  > > How about WM_QUERYENDSESS ION?
                  > >[/color][/color]

                  Comment

                  Working...