Solved

Capturing ALT-F4 and CTRL-F4 in MDI application

Posted on 2000-02-23
11
1,400 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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
 

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 AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Generic progress indicator 6 117
format the code in java 6 93
ffmpeg - "rtsp://...... Operation not permitted" 4 80
Annoying "thing" blocks my view 4 100
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
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.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

777 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