Home > Windows API > Inside EnableWindow, IsWindowEnabled and how to use WM_ENABLE?

Inside EnableWindow, IsWindowEnabled and how to use WM_ENABLE?

Recently a friend of mine was asking why is WM_ENABLE not working, control is grayed out but it remains enabled.

So what should be done first do is modify style of control and add WS_DISABLED style and then call SendMessage with event set as WM_ENABLE and wParam value set as FALSE in order to disable a control and vice versa for enabling.

So inside EnableWindow something like this should be going on…

void EnableWindow( HWND hWnd, const BOOL Enable )
{
   LONG Style = GetWindowLong( hWnd, GWL_STYLE );
   if( Enable )
   {
      Style &= ~WS_DISABLED; // Remove style from existing style
   }
   else
   {
      Style |= WS_DISABLED; // Add style to existing style
   }

   // Set new style, if not set window will remain enabled
   SetWindowLong( hWnd, GWL_STYLE, Style );

   // If not called window look and feel won't change, i.e. graying out controls
   ::SendMessage( hWnd, WM_ENABLE, Enable, 0 );
}// End EnableWindow

So WS_DISABLED and WM_ENABLE works in tandem. Now you should be know why WM_ENABLE didn’t work for my dear friend. 😛

To summarize we can say that WS_DISABLED controls the behavior and WM_ENABLE does the painting stuff.

So what  about ::IsWindowEnabled ?, well it simply checks for the WS_DISABLED flag, could look somewhat like this…

BOOL IsWindowEnabled( HWND hWnd )
{
   return !(( ::GetWindowLong( hWnd, GWL_STYLE ) & WS_DISABLED ) == WS_DISABLED );
}
Advertisements
  1. ionic
    February 24, 2010 at 9:19 am

    thanks for code.

  2. anh
    July 17, 2009 at 2:53 pm

    cannot convert parameter 1 from ‘const int’ to ‘struct HWND__ *’!!!

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: