• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1486
  • Last Modified:

Capturing ALT-F4 and CTRL-F4 in MDI application

In an MDI application (also SDI) :

- when you press CTRL-F4 in a document, you receive an OnSysCommand message with parameter SC_CLOSE in the frame of the document (derived from CMDIChildWnd).

- when you press ALT-F4, you receive an OnSysCommand message with parameter SC_CLOSE in the main frame (derived from CMDIFrameWnd).

How is it posible to know if the OnSysCommand message with SC_CLOSE is send when pressing ALT-F4 (CTRL-F4) or when clicking on the system menu (Close) ?
0
mike_marquet
Asked:
mike_marquet
  • 6
  • 5
1 Solution
 
Vinayak KumbarSr Program ManagerCommented:
Hi,

Try trapping them in PreTranslateMessage()
like
if(pMsg->message == WM_SYSKEYDOWN && pMsg->wParam == VK_F4)
{
//pressed
}

Or try using GetKeyState() for Alt and F4 keys there.
VinExpert
0
 
mike_marquetAuthor Commented:
Ok, I will try it.
0
 
Vinayak KumbarSr Program ManagerCommented:
Hi,

For Trapping Alt+F4 trap it in the app class's PreTranslateMessage like

BOOL CTestApp::PreTranslateMessage(MSG* pMsg)
{
      if(pMsg->message == WM_SYSCOMMAND && (GetKeyState(VK_F4) & 0x8000))
      {
            //AfxMessageBox("Alt+F4");
//If U dont want that message to be processed then return -1
            return -1;
      }
      
      return CWinApp::PreTranslateMessage(pMsg);
}

And for trapping Ctrl+F4, trap it in each child views as

BOOL CNewFormView::PreTranslateMessage(MSG* pMsg)
{
      if(pMsg->message == WM_KEYDOWN && (GetKeyState(VK_F4) & 0x8000) && (GetKeyState(VK_CONTROL) & 0x8000))
      {
            //AfxMessageBox("Ctrl+F4");
            return -1;//dont want to process that message
      }      
      return CFormView::PreTranslateMessage(pMsg);
}

Hope this helps U.
VinExpert
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Vinayak KumbarSr Program ManagerCommented:
Hi,

Else do it in a single shot by placing both in the app class's PreTranslateMessage as

BOOL CTestApp::PreTranslateMessage(MSG* pMsg)
{
      if(pMsg->message == WM_KEYDOWN && (GetKeyState(VK_F4) & 0x8000) && (GetKeyState(VK_CONTROL) & 0x8000))
      {
            //AfxMessageBox("Ctrl+F4");
            return -1;//Dont want
      }
      
      if(pMsg->message == WM_SYSCOMMAND && (GetKeyState(VK_F4) & 0x8000))
      {
            //AfxMessageBox("Alt+F4");
            return -1;//Dont want
      }
      return CWinApp::PreTranslateMessage(pMsg);
}

Try it out.

VinExpert
0
 
mike_marquetAuthor Commented:
Thanks
0
 
mike_marquetAuthor Commented:
The CTRL-F4 doesn't work when you use an AfxMessageBox in the PreTranslateMessage function.

// THIS DON'T WORK

BOOL CNewFormView::PreTranslateMessage(MSG* pMsg)
  {
    if (pMsg->message == WM_KEYDOWN && (GetKeyState(VK_F4) & 0x8000) && (GetKeyState(VK_CONTROL) & 0x8000))
     {
      if (AfxMessageBox("CTRL-F4", MB_YESNO) == IDNO) return -1;
     }

    return CFormView::PreTranslateMessage(pMsg);
   }

When I click YES on the message box, the document is not closed !
0
 
mike_marquetAuthor Commented:
The same occured when you place the test in the CTestApp::PreTranslateMessage function.


-- It's OK for ALT-F4 --
0
 
mike_marquetAuthor Commented:
Correction :

BOOL CNewFormView::PreTranslateMessage(MSG* pMsg)
 {
  if (pMsg->message == WM_KEYDOWN &&
      (GetKeyState(VK_F4) & 0x8000) &&
      (GetKeyState(VK_CONTROL) & 0x8000))
   {
    if (AfxMessageBox("CTRL-F4", MB_YESNO) == IDNO)
     {
      return -1;
     }

    GetParent()->PostMessage(WM_SYSCOMMAND, SC_CLOSE, MAKELONG(0xFFF,0xFFF));
    return TRUE;
   }
      
  return CFormView::PreTranslateMessage(pMsg);
 }
0
 
Vinayak KumbarSr Program ManagerCommented:
Hi,

It will work. The response to the AfxMessageBox should be NO. I tested it. If any prob tell me.

VinExpert
0
 
Vinayak KumbarSr Program ManagerCommented:
Hi,

Is it working fine?

VinExpert
0
 
mike_marquetAuthor Commented:
When I don't add 'GetParent()->PostMessage(WM_SYSCOMMAND, SC_CLOSE, MAKELONG(0xFFF,0xFFF)); return TRUE;' after the message box, the document is not closed when I press YES on the message box.

Without these two lines, it works only if the CTRL key is down when I press YES.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now