Click here to Skip to main content
15,504,146 members
Articles / Desktop Programming / Win32
Posted 2 Nov 2012


16 bookmarked

Re-Active Disabled Controls

Rate me:
Please Sign up or sign in to vote.
5.00/5 (6 votes)
2 Nov 2012CPOL3 min read
Re-Activate disabled windows controls
Image 1
Image 2


Have you ever had a grayed out control you just had to click.. Like a browse or back button. This Article describes how you can use the windows API to Re-activate the control..


I was uninstalling Visual Studio 2010 from a Virtual Machine and for some reason the browse button was disabled. The original install was on an additional virtual harddrive that had been removed and VS just refused to uninstall. In my mind all would be well if I could just change the Install Directory and for that to happen the browse button had to be enabled. I set out on a quest to re-enable that button no matter what.

Using the code

The code makes use of a wrapper class (Win32) that exposes the methods in the Windows user32.dll.
Its all good and well that we have this wrapper class but how do we use it?  

First we have to expose the correct methods in the dll

[return: MarshalAs(UnmanagedType.Bool)] //
public static extern bool EnableWindow(IntPtr hWnd, bool bEnable); 

To re-enable the control we have to pass the Enablewindow method the controls handle and the new status. 

Great.. So I know how to pass a bool but how do I get the controls handle. After all, its not in my code.. it's in an external application!!

Well, Its actually easier than you think.

We expose the WindowFromPoint method in the user32.dll. (This gets a window/control hanlde at a specific point.)

[DllImport("user32.dll")] //
public static extern IntPtr WindowFromPoint(Point pt); 

and call it using using our cursor's position. 

// capture the window under the cursor's position
IntPtr hWnd = Win32.WindowFromPoint(Cursor.Position); 

The hWnd variable now contains a pointer to that controls handle and with the handle we basically Pwn the control!  The following method does exactly that, it passes the controls hande and a status to the EnableWindow Method. 

/// <summary>
        /// Activate the Control
        /// </summary>
        /// <param name="status">Boolean to Enabled or Disabled</param>
        private void ReActiveControl(bool status)

            //Ask the WinApi to enable/disable the control/form
            Win32.EnableWindow(currentHandle, status);

            //Change the Controls style by removing/adding WM_Disabled if it exists in the style

            //Send the WM_Enable message to the given status
            Win32.SendMessage(currentHandle, (Int32)Win32.WindowMessages.WM_ENABLE, Convert.ToInt32(status), 0);

            //Post the Enabled Message to the Parent to refresh its child(ren)
            Win32.PostMessage(currentParent, (Int32)Win32.WindowMessages.WM_COMMAND, (IntPtr)Win32.WindowMessages.WM_ENABLE, currentHandle);
            Win32.PostMessage(currentHandle, (Int32)Win32.WindowMessages.WM_COMMAND, (IntPtr)Win32.WindowMessages.WM_ENABLE, IntPtr.Zero);

I found that DotNet controls don't act like other buttons do. Once you re-Activate them they look clickable but nothing happens when you click on them. For this I found one has to change the controls Style. 

The WinAPI allows us to get and set the style

[DllImport("User32.dll")] //
        public static extern WindowStyles GetWindowLong(IntPtr hWnd, int index);
[DllImport("user32.dll")] //
        public static extern int SetWindowLong(IntPtr hWnd, int nIndex, long dwNewLong);  

The code can be used via our Win32 wrapper class as follows: 

/// <summary>
<pre>        /// Dotnet controls dont act like other controls. Changing the style help here
        /// </summary>
        /// <param name="status"></param>
        private void ChangeStyle(bool status)
            //Get the current handles style (-16 = Retrieve the window styles)
            long Style = (long)Win32.GetWindowLong(currentHandle, -16);
            if (status)
                Style &= ~(long)Win32.WindowStyles.WS_DISABLED; //Remove the WS_Disabled Style
                Style |= (long)Win32.WindowStyles.WS_DISABLED;//Add if required
                //Check if the OS is Win7 x64 or higher
                if (Environment.Is64BitOperatingSystem && Environment.OSVersion.Version.Major >= 6)
                    //Win32.SetWindowLong(currentHandle, -16, Style); TODO: Find work around (Doesn't work on x64 Win 7+)
                    //Apperantly doesn't work on Win 7 64bit
                    Win32.SetWindowLong(currentHandle, -16, Style);
            catch (Exception ex)

Typical! This guys now skipped what the PostMessage method does!

 [DllImport("User32.dll")] //
public static extern int PostMessage(IntPtr hWnd, int uMsg, IntPtr wParam, IntPtr lParam); 

Well the PostMessage method used earlier in the ReActiveControl Method allows us to post a message to windows describing that its a command (WM_COMMAND) and that command is an enable command (WM_ENABLE).   

Points of Interest 

While playing with the code i decided to add some additional features like changing the text of a control. (Once you restart the application the text you change will be back to how it was, but its still fun.

For this I made use of the WinAPI's SendMessage method. 

Here we expose the different overloads of the method:

public static extern int SendMessage(IntPtr hWnd, int uMsg, IntPtr wParam, IntPtr lParam);
public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);
public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, StringBuilder lParam);   

To send the new text we use a StringBuilder to build up the new text and pass it as a parameter to the SendMessage method. 

//Build up some string to send to the control 

StringBuilder sbText = new StringBuilder(256);  

int length = Win32.SendMessage(currentHandle , (Int32)Win32.WindowMessages.WM_GETTEXTLENGTH, 0, 0);
//Send the password to the password field
Win32.SendMessage(currentHandle, (Int32)Win32.WindowMessages.WM_SETTEXT, length, sbText);  

While playing with the code I also discovered that my finder tool doesn't actually pick up the disabled controls.
The full solution code contains a method to find all the child controls of a parent. This is particularly handy to get to some of the hidden controls. 


This version still doesn't behave on Windows 7 x64. In future I will update it to support x64.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Architect Backbase
Netherlands Netherlands
Senior Solutions Architect.

I reject your code and substitute my own Smile | :) !

Comments and Discussions

GeneralGreat work ! Pin
Asgard2530-Jul-14 4:27
professionalAsgard2530-Jul-14 4:27 
AnswerFor x64 systems Pin
mcornella7-Aug-13 2:11
mcornella7-Aug-13 2:11 
GeneralRe: For x64 systems Pin
Pieter Alec Myburgh14-Aug-13 0:14
Pieter Alec Myburgh14-Aug-13 0:14 
GeneralWorks well Pin
xquiky24-Apr-13 9:56
xquiky24-Apr-13 9:56 
QuestionGood article... Pin
ahmet_uzun4-Nov-12 0:50
ahmet_uzun4-Nov-12 0:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.