Solved

Capturing ALT-F4 and CTRL-F4 in MDI application

Posted on 2000-02-23
11
1,446 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Database Solutions Engineer FAQs

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller single-server environments.

 
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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

623 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