C# winform 中控件禁用,但是不变灰

来源:互联网 发布:内存卡恢复数据 编辑:程序博客网 时间:2024/05/29 07:30
 [System.Runtime.InteropServices.DllImport("user32.dll ")]
        public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int wndproc);
        [System.Runtime.InteropServices.DllImport("user32.dll ")]
        public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
 
        public const int GWL_STYLE = -16;
        public const int WS_DISABLED = 0x8000000;
 
        public static void SetControlEnabled(Control c, bool enabled)
        {
            if (enabled)
            { SetWindowLong(c.Handle, GWL_STYLE, (~WS_DISABLED) & GetWindowLong(c.Handle, GWL_STYLE)); }
            else
            { SetWindowLong(c.Handle, GWL_STYLE, WS_DISABLED + GetWindowLong(c.Handle, GWL_STYLE)); }
        }
 
        private void button2_Click(object sender, System.EventArgs e)
        {
            SetControlEnabled(this.button1, false);
            this.button1.ForeColor = Color.White;
        }
文章转载自:http://bbs.csdn.net/topics/350241687
原创粉丝点击