Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Modal dialog - Close - ESC

Modal dialog - Close - ESC

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
37 Posts 10 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V vallikumar

    Hi While pressing ESC key by default ID_CANCEL(OnCancel) message is calling. So write code inside OnCancel() to prevent this. Check the keypressed and if it is ESC key then dont call CDialog::OnCancel(); regards Vallikumar A

    O Offline
    O Offline
    Owner drawn
    wrote on last edited by
    #4

    vallikumar wrote:

    Check the keypressed and if it is ESC key then dont call CDialog::OnCancel()

    OnCancel will be called when Esc is pressed or the Cancel button with the id IDCANCEL is clicked.

    Jesus Loves:rose:

    --Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:

    1 Reply Last reply
    0
    • S Sarvan AL

      How to prevent a modal dialog from closing when we hit ESC key? Thanks in advance, Sarvan AL

      S Offline
      S Offline
      Stephen Hewitt
      wrote on last edited by
      #5

      The IsDialogMessage API provides this functionality. You would have to filter the messages passed to it - this will not be possible for dialog in general as you have not got access to the message pump. An alternative, if you can't use this technique, would be to use hooks (SetWindowsHookEx). Steve

      O 1 Reply Last reply
      0
      • S Sarvan AL

        How to prevent a modal dialog from closing when we hit ESC key? Thanks in advance, Sarvan AL

        S Offline
        S Offline
        Stephen Hewitt
        wrote on last edited by
        #6

        The other guys have a point - My solution was like killing a gnat with a sledgehammer. Perhaps just use the GetAsyncKeyState to see if the escape key is down in a OnCancel overide.

        S 1 Reply Last reply
        0
        • S Sarvan AL

          How to prevent a modal dialog from closing when we hit ESC key? Thanks in advance, Sarvan AL

          R Offline
          R Offline
          Rajesh R Subramanian
          wrote on last edited by
          #7

          The simplest solution is to override the PreTranslateMessage()! Within the handler, write this code...if(pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_ESCAPE) wParam = NULL; //yeah you read it right!
          Simple as pie :) Regards, Rajesh R. Subramanian You have an apple and me too. We exchange those and We have an apple each. You have an idea and me too. We exchange those and We have two ideas each.

          S P 2 Replies Last reply
          0
          • S Stephen Hewitt

            The IsDialogMessage API provides this functionality. You would have to filter the messages passed to it - this will not be possible for dialog in general as you have not got access to the message pump. An alternative, if you can't use this technique, would be to use hooks (SetWindowsHookEx). Steve

            O Offline
            O Offline
            Owner drawn
            wrote on last edited by
            #8

            :omg::wtf:

            Jesus Loves:rose:

            --Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:

            1 Reply Last reply
            0
            • R Rajesh R Subramanian

              The simplest solution is to override the PreTranslateMessage()! Within the handler, write this code...if(pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_ESCAPE) wParam = NULL; //yeah you read it right!
              Simple as pie :) Regards, Rajesh R. Subramanian You have an apple and me too. We exchange those and We have an apple each. You have an idea and me too. We exchange those and We have two ideas each.

              S Offline
              S Offline
              Stephen Hewitt
              wrote on last edited by
              #9

              Ladies and gentlemen, we have a winner. It's one of those solutions that once you hear, you feel stupid for not thinking of (assuming MFC is beging used). Steve

              R 1 Reply Last reply
              0
              • S Stephen Hewitt

                Ladies and gentlemen, we have a winner. It's one of those solutions that once you hear, you feel stupid for not thinking of (assuming MFC is beging used). Steve

                R Offline
                R Offline
                Rajesh R Subramanian
                wrote on last edited by
                #10

                LOL man. that made you dig deep to set a system-wide hook? It does happen, but. Regards, Rajesh R. Subramanian. You have an apple and me too. We exchange those and We have an apple each. You have an idea and me too. We exchange those and We have two ideas each.

                S 1 Reply Last reply
                0
                • R Rajesh R Subramanian

                  LOL man. that made you dig deep to set a system-wide hook? It does happen, but. Regards, Rajesh R. Subramanian. You have an apple and me too. We exchange those and We have an apple each. You have an idea and me too. We exchange those and We have two ideas each.

                  S Offline
                  S Offline
                  Stephen Hewitt
                  wrote on last edited by
                  #11

                  I would have only set a thread local hook - But regardless your solution is still the way to go. Steve

                  P 1 Reply Last reply
                  0
                  • S Sarvan AL

                    How to prevent a modal dialog from closing when we hit ESC key? Thanks in advance, Sarvan AL

                    M Offline
                    M Offline
                    Michael Dunn
                    wrote on last edited by
                    #12

                    See the FAQ 7.4 How do I prevent a dialog from closing when the user presses Enter or Esc?[^] --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | NEW!! PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Shots do not hurt other players... yet

                    R 1 Reply Last reply
                    0
                    • R Rajesh R Subramanian

                      The simplest solution is to override the PreTranslateMessage()! Within the handler, write this code...if(pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_ESCAPE) wParam = NULL; //yeah you read it right!
                      Simple as pie :) Regards, Rajesh R. Subramanian You have an apple and me too. We exchange those and We have an apple each. You have an idea and me too. We exchange those and We have two ideas each.

                      P Offline
                      P Offline
                      Prakash Nadar
                      wrote on last edited by
                      #13

                      Have you ever made a pie before?


                      -Prakash

                      1 Reply Last reply
                      0
                      • S Stephen Hewitt

                        I would have only set a thread local hook - But regardless your solution is still the way to go. Steve

                        P Offline
                        P Offline
                        Prakash Nadar
                        wrote on last edited by
                        #14

                        no hooks are needed to do this.


                        -Prakash

                        S 1 Reply Last reply
                        0
                        • S Stephen Hewitt

                          The other guys have a point - My solution was like killing a gnat with a sledgehammer. Perhaps just use the GetAsyncKeyState to see if the escape key is down in a OnCancel overide.

                          S Offline
                          S Offline
                          S Douglas
                          wrote on last edited by
                          #15

                          Going after a gnat with a sledgehammer is always fun! :)


                          ZeePain! wrote:

                          This seems like one of those programs that started small, grew incrementally, building internal pressure, and finally barfed all over its source code sneakers. Or something.

                          thedailywtf.com[^]

                          1 Reply Last reply
                          0
                          • P Prakash Nadar

                            no hooks are needed to do this.


                            -Prakash

                            S Offline
                            S Offline
                            Stephen Hewitt
                            wrote on last edited by
                            #16

                            I think we've established that - PreTranslateMessage was the best solution - Assuming we're using MFC. Steve

                            P 1 Reply Last reply
                            0
                            • S Stephen Hewitt

                              I think we've established that - PreTranslateMessage was the best solution - Assuming we're using MFC. Steve

                              P Offline
                              P Offline
                              Prakash Nadar
                              wrote on last edited by
                              #17

                              PreTranslateMessage is good but did you consider http://www.codeproject.com/script/comments/forums.asp?msg=1338886&forumid=1647#xx1338886xx[^]


                              -Prakash

                              T S 2 Replies Last reply
                              0
                              • P Prakash Nadar

                                PreTranslateMessage is good but did you consider http://www.codeproject.com/script/comments/forums.asp?msg=1338886&forumid=1647#xx1338886xx[^]


                                -Prakash

                                T Offline
                                T Offline
                                toxcct
                                wrote on last edited by
                                #18

                                much better, and very simple !!! thanks to mickael...


                                TOXCCT >>> GEII power
                                [toxcct][VisualCalc 2.20][VCalc 3.0 soon...]

                                P R 2 Replies Last reply
                                0
                                • T toxcct

                                  much better, and very simple !!! thanks to mickael...


                                  TOXCCT >>> GEII power
                                  [toxcct][VisualCalc 2.20][VCalc 3.0 soon...]

                                  P Offline
                                  P Offline
                                  Prakash Nadar
                                  wrote on last edited by
                                  #19

                                  toxcct wrote:

                                  thanks to mickael...

                                  no doubt. :)


                                  -Prakash

                                  1 Reply Last reply
                                  0
                                  • P Prakash Nadar

                                    PreTranslateMessage is good but did you consider http://www.codeproject.com/script/comments/forums.asp?msg=1338886&forumid=1647#xx1338886xx[^]


                                    -Prakash

                                    S Offline
                                    S Offline
                                    Stephen Hewitt
                                    wrote on last edited by
                                    #20

                                    That approach seemed to stop the dialog box from begin closed by any means so I ruled it out. I don't actually remember him saying he uses MFC, but that seems to be assumed in this forum. I try to steer clear of MFC in my own projects although I use it extensively at work. The MFC source code is a valuable resource however. Steve

                                    P 1 Reply Last reply
                                    0
                                    • S Stephen Hewitt

                                      That approach seemed to stop the dialog box from begin closed by any means so I ruled it out. I don't actually remember him saying he uses MFC, but that seems to be assumed in this forum. I try to steer clear of MFC in my own projects although I use it extensively at work. The MFC source code is a valuable resource however. Steve

                                      P Offline
                                      P Offline
                                      Prakash Nadar
                                      wrote on last edited by
                                      #21

                                      Stephen Hewitt wrote:

                                      That approach seemed to stop the dialog box from begin closed by any means so I ruled it out.

                                      you dont put for both onok and oncancel, just the oncancel will do in this case.


                                      -Prakash

                                      S R 2 Replies Last reply
                                      0
                                      • P Prakash Nadar

                                        Stephen Hewitt wrote:

                                        That approach seemed to stop the dialog box from begin closed by any means so I ruled it out.

                                        you dont put for both onok and oncancel, just the oncancel will do in this case.


                                        -Prakash

                                        S Offline
                                        S Offline
                                        Stephen Hewitt
                                        wrote on last edited by
                                        #22

                                        When I test just OnCancel and stopped it calling CDialog::OnCancel the dialog can't be closed by any means other the pressing "OK". This includes the "x", the system menu and escape. Steve

                                        1 Reply Last reply
                                        0
                                        • P Prakash Nadar

                                          Stephen Hewitt wrote:

                                          That approach seemed to stop the dialog box from begin closed by any means so I ruled it out.

                                          you dont put for both onok and oncancel, just the oncancel will do in this case.


                                          -Prakash

                                          R Offline
                                          R Offline
                                          Rajesh R Subramanian
                                          wrote on last edited by
                                          #23

                                          I knew this other solution well before I had posted the PreTranslateMessage handling solution. But it will just change the functionality of OnCancel() too. If i write my code in OnCancel(), not to close, then on clicking the cancel button too, the dialog won't close. Don't tell me you did not know this please. I might want to do something else on OnOk() too. Handling the pretranslatemessage is not a big job. And its powerful than any other way when you work with MFC. I cannot think of any other easier and powerful way to do this. If you know something else which is REALLY APPROPRIATE and RELEVANT, please let us know too. Thanks in advance. Regards, Rajesh R. Subramanian You have an apple and me too. We exchange those and We have an apple each. You have an idea and me too. We exchange those and We have two ideas each.

                                          P 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups