Solved

Capturing ALT-F4 and CTRL-F4 in MDI application

Posted on 2000-02-23
11
1,387 Views
Last Modified: 2013-11-20
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
Comment
Question by:mike_marquet
  • 6
  • 5
11 Comments
 
LVL 8

Expert Comment

by:VinExpert
ID: 2550241
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
 

Author Comment

by:mike_marquet
ID: 2550322
Ok, I will try it.
0
 
LVL 8

Accepted Solution

by:
VinExpert earned 50 total points
ID: 2550334
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
 
LVL 8

Expert Comment

by:VinExpert
ID: 2550348
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
 

Author Comment

by:mike_marquet
ID: 2550369
Thanks
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:mike_marquet
ID: 2550418
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
 

Author Comment

by:mike_marquet
ID: 2550431
The same occured when you place the test in the CTestApp::PreTranslateMessage function.


-- It's OK for ALT-F4 --
0
 

Author Comment

by:mike_marquet
ID: 2550489
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
 
LVL 8

Expert Comment

by:VinExpert
ID: 2550503
Hi,

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

VinExpert
0
 
LVL 8

Expert Comment

by:VinExpert
ID: 2550512
Hi,

Is it working fine?

VinExpert
0
 

Author Comment

by:mike_marquet
ID: 2550943
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ADO Memory leak with DELPHI 2007 37 171
windows 10 how make picture as desktop background 2 58
Doc'in system (example?) BA 7 89
centeredAverage challenge 8 132
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now