Disabling All Top Level Thread Windows

In my current project I found it nessassary to disable all the top level windows my application created before displaying any modal dialog boxes. This is because although all of these windows are "owned" by my main frame window, they are siblings and running a modal dialog does not prevent user input from reaching those windows. I came up with this brutally simple class that disables all the top level windows of a given thread when constructed and re-enables them when it is destroyed. I thought I would share this with you.


class DisableTaskWindows
{
public:
 DisableTaskWindows()
  { ::EnumWindows(EnumWindowsProc, FALSE); }
 ~DisableTaskWindows()
  { ::EnumWindows(EnumWindowsProc, TRUE); }

private:
 static BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam);
};

BOOL CALLBACK DisableTaskWindows::EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
 if (::GetWindowThreadProcessId(hwnd, NULL) == ::GetCurrentThreadId())
  ::EnableWindow(hwnd, lParam);
 return TRUE;
}

It becomes a little more complicated if you need to specify the thread ID:



class DisableTaskWindows
{
public:
 DisableTaskWindows(DWORD dwThreadID);
 ~DisableTaskWindows();

private:
 static BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam);

 BOOL m_bEnable;
 DWORD m_dwThreadID;
};

BOOL CALLBACK DisableTaskWindows::EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
 if (::GetWindowThreadProcessId(hwnd, NULL) == ((DisableTaskWindows*)lParam)->m_dwThreadID)
  ::EnableWindow(hwnd, ((DisableTaskWindows*)lParam)->m_bEnable);
 return TRUE;
}

DisableTaskWindows::DisableTaskWindows(DWORD dwThreadID)
 : m_dwThreadID(dwThreadID)
{
 m_bEnable = FALSE;
 ::EnumWindows(EnumWindowsProc, (LPARAM)this);
}

DisableTaskWindows::~DisableTaskWindows()
{
 m_bEnable = TRUE;
 ::EnumWindows(EnumWindowsProc, (LPARAM)this);
}

It's very simple to use:


CMyDialog dlg;
{
 DisableTaskWindows dtw;
 - or -
 DisableTaskWindows dtw(AfxGetThread()->m_nThreadID);
 dlg.DoModal();
} // Destroys dtw which re-enables the windows

You could also create a class derived from CDialog, override DoModal and add a DisableTaskWindows object before calling the base class. This would make it even more seemless:


class CDisablingDialog : public CDialog
{
public:
 CDisablingDialog(LPCTSTR lpszTemplateName, CWnd* pParentWnd = NULL)
  : CDialog(lpszTemplateName, pParentWnd) {}

 CDisablingDialog(UINT nIDTemplate, CWnd* pParentWnd = NULL)
  : CDialog(nIDTemplate, pParentWnd) {}

 virtual int DoModal()
 {
  DisableTaskWindows dtw;
  return CDialog::DoModal();
 }
};

There are lot's of combinations you could come up with, but I'll leave that as an excercise for the reader.

Date Last Updated: April 24, 1999



Comments

  • Hmm

    Posted by Legacy on 10/18/2001 12:00am

    Originally posted by: Hmm

    Well, I was searching for some etxra info on the procedure DisableTaskWindows from Borland's VCL. You might wnna credit them/it...

    Reply
  • Thanks

    Posted by Legacy on 04/28/1999 12:00am

    Originally posted by: Randy Pitz

    I've been looking for this type of functionality for a long time. Thanks!

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds