Microsoft KB Archive/243455

{|
 * width="100%"|

INFO: How to Disable ALT+TAB So Single Application Always Has Focus

 * }

ID: Q243455

-

The information in this article applies to:


 * Microsoft Visual C++, 32-bit Editions, version 6.0

-

SUMMARY
In order to disable the ALT + TAB key combination and not lose focus to the other applications running, do the following:


 * 1) Create the application main window with the minimize button disabled.
 * 2) Disable all the windows in the system by calling EnumWindows. Use EnableWindow( .., FALSE) in the callback for this function.
 * 3) Deceive the operating system by running the application as a screen saver. This can be done by calling SystemParametersInfo(SPI_SCREENSAVERRUNNING,TRUE, ....);.
 * 4) In the WM_DESTROY handler of the main application window again call EnumWindows to enable all the windows in the system.
 * 5) Deceive the operating system by no longer running the the application as a screen saver. This can be done by again calling SystemParametersInfo(SPI_SCREENSAVERRUNNING,FALSE, ....);.

MORE INFORMATION
Sample code to disable the ALT+TAB key combination is shown below:

#define MAX_LOADSTRING 100

// Global Variables: HINSTANCE hInst;                     // current instance TCHAR szTitle[MAX_LOADSTRING];       // The title bar text TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text

typedef struct _DLONG {   LONG wParam; LONG lParam; } DLONG;

// This disables all of the windows in the system if called // after the application window is created and enables them // when the application window is destroyed. BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam) {   DLONG *pDlong = (DLONG *) lParam; BOOL bEnable = (BOOL)pDlong->lParam; if(hwnd != (HWND)pDlong->wParam) EnableWindow(hwnd,bEnable); return TRUE; }

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) {  HWND hWnd; hInst = hInstance; // Store instance handle in our global variable

hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW &      ~WS_MINIMIZEBOX, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL,       hInstance, NULL);

if (!hWnd) {     return FALSE; }

//Call EnumWindows to get the handles of all the windows in the system //and then disable them. BOOL old; DLONG dlong;

dlong.wParam = (WPARAM)hWnd; dlong.lParam = (LPARAM)FALSE;

EnumWindows(EnumWindowsProc, (LONG)&dlong);

//Deceive the OS by communicating that the application is a screen   //saver. //This disables the ALT + TAB /ESC keys. SystemParametersInfo(SPI_SCREENSAVERRUNNING,TRUE,&old,0);

ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd);

return TRUE; } In the WndProc function you can add the following code for WM_DESTROY:

 case WM_DESTROY: {      //Restore the original settings by enabling all the windows and //communication to the OS that the screen saver is not running any         //more. DLONG dlong; BOOL Old; dlong.lParam = (LPARAM)TRUE; dlong.wParam = (WPARAM)hWnd; EnumWindows(EnumWindowsProc,(LONG)&dlong); SystemParametersInfo(SPI_SCREENSAVERRUNNING,FALSE,&Old,0); PostQuitMessage(0); break; }

© Microsoft Corporation 1999, All Rights Reserved.

Contributions by Vidyanand Rajpathak, Microsoft Corporation

Additional query words: SystemParametersInfo, SPI_SCREENSAVERRUNNING

Keywords : kbScreenSaver kbWndw kbGrpMFCATL

Version : winnt:6.0

Platform : winnt

Issue type : kbinfo

Technology :