Disabling All Top Level Thread Windows | CodeGuru

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Apr 24, 1999
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.