What is the problem of .NET with the EnableWindow?

EnableWindow function enables or disables the mouse and keyboard input to the specified control. You can use this function from user32.dll as the follows;

[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern bool EnableWindow(HandleRef hWnd, bool enable);

[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern bool EnableWindow(IntPtr hWnd, bool enable);


But .NET Framework’s Control class, it is the base class to create a custom control in .NET, manages its Enable property a little bit different from the unmanaged Controls and Windows. Normally we can understand a control’s enable status from its Window Styles. If it contains WS_DISABLED flag this means that the given control is disabled otherwise it is enabled. But .NET Framework’s Control class does not use this WS_DISABLED flag. Instead it uses its own mechanism to understand that it is Enabled or not? So when we use EnableWindow function on a .NET Control it just enables the window and set its flags. But .NET still uses its own state flag which we can only change it by using Enabled property of that class. So .NET Framework Control still things that it is not enabled.
Therefore it is painted as disabled and does not accept mouse inputs. Actually default window procedure of this control started to accept mouse messages but again Control class’s own mechanism checks that is this control Enabled property before processing the mouse message.
Although Control which is enabled by EnableWindow function does not accept mouse messages, it begins to accept keyboard messages. Because there is no limitation in .NET Control class for the keyboard messages like mouse messages.

Here is the .NET's codes;
private void WmMouseDown(ref Message m, MouseButtons button, int clicks)
{
       MouseButtons buttons1 = Control.MouseButtons;
       SetState(0x8000000, true);
       if (!GetStyle(ControlStyles.UserMouse))
       {
             DefWndProc(ref m);
       }
       else
       {
             if ((button == MouseButtons.Left) && GetStyle(ControlStyles.Selectable))
             {
                    FocusInternal();
             }
       }
       if (buttons1 == Control.MouseButtons)
       {
             if (!GetState2(0x10))
             {
                    CaptureInternal = true;
             }
             // Here checks Enabled property to process mouse events
             if ((buttons1 == Control.MouseButtons) && Enabled)
             {
                    OnMouseDown(new MouseEventArgs(button, clicks, NativeMethods.Util.SignedLOWORD(m.LParam), NativeMethods.Util.SignedHIWORD(m.LParam), 0));
             }
       }
}

public bool Enabled
{
       get
       {
             // uses 3rd bit of the state flag to indicate the Enabled value
             if (!GetState(4))
             {
                    return false;
             }
             if (ParentInternal == null)
             {
                    return true;
             }
             return ParentInternal.Enabled;
       }
       set
       {
             bool value = Enabled;
             // sets the 3rd bit of the state flag
             SetState(4, value);
             if (value != value)
             {
                    if (!value)
                    {
                           SelectNextIfFocused();
                    }
                    OnEnabledChanged(EventArgs.Empty);
             }
       }
}

internal bool GetState(int flag)
{
       // state is a field
       return (state & flag) != 0;

}

Code extracted by Xenocode Fox .NET Decompiler

Related posts

Add comment


 

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

November 20. 2008 01:48 AM


Search

Calendar

<<  November 2008  >>
MonTueWedThuFriSatSun
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567
View posts in large calendar

Disclaimer

© 2007 - 2008
Ozcan DEGIRMENCI
All rights reserved. The content can be used elsewhere given that the source is properly acknowledged.