Solved

Capturing ALT-F4 and CTRL-F4 in MDI application

Posted on 2000-02-23
11
1,365 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Login Script to Copy Folders 12 21
Windows Drag & Drop Location 2 85
while loop over for loop 7 79
Fibonacci challenge 11 82
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…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 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

14 Experts available now in Live!

Get 1:1 Help Now