【Winform】禁用控件但不变灰

https://blog.csdn.net/Cool2Feel/article/details/81018722

        [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.label, false);
            this.label.ForeColor = Color.White;
        }

 

posted @ 2023-03-01 17:44  不溯流光  阅读(93)  评论(0编辑  收藏  举报