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. Trouble while trying to exit a dialog MFC App

Trouble while trying to exit a dialog MFC App

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestionannouncement
5 Posts 4 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.
  • A Offline
    A Offline
    andreir23
    wrote on last edited by
    #1

    I have a small dialog-type MFC App In order to not exit the app if I press ESC or Enter I wrote this: void CVariantDlg::OnOK(){} void CVariantDlg::OnCancel(){} In other words I overloaded the functions OnOK() and OnCancel(). It worked! Now I can press ESC or Enter and the app won't close. My problem is that even if I hit ALT-F4 or right-click it on the taskbar and choose close or hit the x button in the right side of the title bar it won't close! This sucks. I guess I should check to see what are the latest keys pressed and choose from that point on. BTW: How did you call the original function from the overloaded version of it? Anyways... what do you recommend me in order to have the application exit on: a) X button from title bar b) ALT-F4 c) Close from the system menu / taskbar rightclick and "Close" But not: a) ESC b) ENter ? TIA! ;-)

    J R R 3 Replies Last reply
    0
    • A andreir23

      I have a small dialog-type MFC App In order to not exit the app if I press ESC or Enter I wrote this: void CVariantDlg::OnOK(){} void CVariantDlg::OnCancel(){} In other words I overloaded the functions OnOK() and OnCancel(). It worked! Now I can press ESC or Enter and the app won't close. My problem is that even if I hit ALT-F4 or right-click it on the taskbar and choose close or hit the x button in the right side of the title bar it won't close! This sucks. I guess I should check to see what are the latest keys pressed and choose from that point on. BTW: How did you call the original function from the overloaded version of it? Anyways... what do you recommend me in order to have the application exit on: a) X button from title bar b) ALT-F4 c) Close from the system menu / taskbar rightclick and "Close" But not: a) ESC b) ENter ? TIA! ;-)

      J Offline
      J Offline
      John R Shaw
      wrote on last edited by
      #2

      Let the user use ESC or Enter, after all that is what most user will expect. Otherwise you could probably use GetKeyState() to determine which key was pressed, so you can decide when to allow box to close. INTP

      A 1 Reply Last reply
      0
      • A andreir23

        I have a small dialog-type MFC App In order to not exit the app if I press ESC or Enter I wrote this: void CVariantDlg::OnOK(){} void CVariantDlg::OnCancel(){} In other words I overloaded the functions OnOK() and OnCancel(). It worked! Now I can press ESC or Enter and the app won't close. My problem is that even if I hit ALT-F4 or right-click it on the taskbar and choose close or hit the x button in the right side of the title bar it won't close! This sucks. I guess I should check to see what are the latest keys pressed and choose from that point on. BTW: How did you call the original function from the overloaded version of it? Anyways... what do you recommend me in order to have the application exit on: a) X button from title bar b) ALT-F4 c) Close from the system menu / taskbar rightclick and "Close" But not: a) ESC b) ENter ? TIA! ;-)

        R Offline
        R Offline
        rrrado
        wrote on last edited by
        #3

        Try to leave OnOK and OnCancel like before, just overload OnClose function like this : OnClose { CDialog::OnOK(); }


        rrrado

        1 Reply Last reply
        0
        • A andreir23

          I have a small dialog-type MFC App In order to not exit the app if I press ESC or Enter I wrote this: void CVariantDlg::OnOK(){} void CVariantDlg::OnCancel(){} In other words I overloaded the functions OnOK() and OnCancel(). It worked! Now I can press ESC or Enter and the app won't close. My problem is that even if I hit ALT-F4 or right-click it on the taskbar and choose close or hit the x button in the right side of the title bar it won't close! This sucks. I guess I should check to see what are the latest keys pressed and choose from that point on. BTW: How did you call the original function from the overloaded version of it? Anyways... what do you recommend me in order to have the application exit on: a) X button from title bar b) ALT-F4 c) Close from the system menu / taskbar rightclick and "Close" But not: a) ESC b) ENter ? TIA! ;-)

          R Offline
          R Offline
          Robert A T Kaldy
          wrote on last edited by
          #4

          Override a PreTranslateMessage:

          BOOL CVariantDlg::PreTranslateMessage(MSG* pMsg)
          {
          if (pMsg->message == WM_KEYDOWN)
          {
          WORD wKeyCode = LOWORD(pMsg->wParam);
          if ((wKeyCode == 13) || (wKeyCode == VK_ESCAPE))
          return TRUE;
          }
          return CDialog::PreTranslateMessage(pMsg);
          }

          Robert-Antonio

          1 Reply Last reply
          0
          • J John R Shaw

            Let the user use ESC or Enter, after all that is what most user will expect. Otherwise you could probably use GetKeyState() to determine which key was pressed, so you can decide when to allow box to close. INTP

            A Offline
            A Offline
            andreir23
            wrote on last edited by
            #5

            THANKS! It worked! If GetKeyState(VK_ESCAPE) is -127 or -128 then the user pressed ESC and I will NOT exit the app. Otherwise yes.

            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