Solved

Keyboard Interface to Esc Key in the SDK

Posted on 1998-11-07
3
501 Views
Last Modified: 2013-12-03
I have an SDK program where I am trying to set things up so that if the user hits the ESC the program terminates.

The program is one large parent window with several smaller child window controls.  The parent window never really has the keyboard focus.  Whenever the parent window gets the keyboard focus it gives it to one of the child windows.

The user then uses the TAB key to make his/her way around the controls in a circular pattern.

To do this I have subclassed the childwindows ( gave them their own private WndProc() for keyboard processing ).

It is from this special WndProc ( SwitchFocusProc() ) that I send the destroy message.  Below is a truncated version of my code to highlight what I am doing?  Is it possible that my SendMessage() call is out of scope?.  If it is.  How can I end my program from SwitchFocusProc()?

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{

     //< Snip >
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
     //<snip>

     switch(iMsg)
     {
     case WM_CREATE:
     //<sniP>
     // Subclass Controls To SwitchFocusProc
      for( b = 0; b < nNumFocusedControls; b++ )
      {
      fnOldProc[b] = (WNDPROC)SetWindowLong(                                          ChildWindow[b + 5].hwnd,                           GWL_WNDPROC,                                     (LONG) SwitchFocusProc);            }//end for
//<snip>

LRESULT CALLBACK SwitchFocusProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
      int i, iNextWindow;
      i = GetWindowLong( hwnd, GWL_ID);

    // If The Stop Button Has The Focus Set Vars To Give
    // It To ListBox For Difficulty
   if( i == IDC_OWNERDRAW_BUTTON_STOP )
    {
     iNextWindow = IDC_LB_LEVEL;
    }
    else
    {
      iNextWindow = i + 1;
    }

    switch(iMsg)
   {
   case WM_KEYDOWN:
     if( wParam == VK_TAB )
     {
     SetFocus( ChildWindow[iNextWindow].hwnd );
     }
     else if( wParam == VK_ESCAPE )
     {
      SendMessage(hwnd, WM_DESTROY, NULL, NULL );
     }                  
     break;            
   }//end switch
 return CallWindowProc(fnOldProc[ i - 5], hwnd, iMsg, wParam, lParam);
}
0
Comment
Question by:ibc_web
  • 2
3 Comments
 
LVL 23

Accepted Solution

by:
chensu earned 100 total points
ID: 1415830
Use
PostMessage(hwnd, WM_CLOSE, 0, 0);
instead of
SendMessage(hwnd, WM_DESTROY, NULL, NULL);

WM_DESTROY is sent by Windows to notify you that the window is being destroyed.
0
 

Author Comment

by:ibc_web
ID: 1415831
It didn't work.  Instead of shutting down the parent window it shut down the child window that happened to have the keyboard focus at the time.

My SwitchWindowProc() handles windows messages for the childwindow controls in my parent window.  However the only messages it handles are for keyboard processing.  Thus when I hit "ESC" the keyboard foucus is always possesed by a child window.

The parent window never really has the keyboard focus ( as soon as it gets it, it passes it to a control ).  

For this reason I would like the ability to have ESC destroy the program from the child windows ( SwitchFocusProc() )

Thanks for the help.......at least I got something destroyed though *smile*.

Do you think "hwnd" in SwitchFocusProc() might be out of scope for "hwnd" in WindProc()?
0
 

Author Comment

by:ibc_web
ID: 1415832
See the comment above.  It didn't work.  My hunch about the scope of hwnd was right.  Once I made the hwnd of the parent window global the destroy calls worked.

The calls were made from a second wndproc(), which took in its own hwnd.

Thanks for getting back to me though
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

747 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

13 Experts available now in Live!

Get 1:1 Help Now