如何获得WIN32 窗体中控件的样式属性(C#)

这阵子弄C# 调用WIN32 API互操作,想从Winform中查询外部WIN32程序窗体中的某个按钮是否可用,就是灰色或者黑色显示,找了些资料,解决了,特记录如下:

代码:

public partial class Form1 : Form
    {

        [DllImport("User32.dll", EntryPoint = "FindWindow")]
        private static extern IntPtr FindWindow(string lpClassName,string lpWindowName);
        [DllImport("User32.dll", EntryPoint = "SendMessage")]
        private static extern int SendMessage(IntPtr hWnd,int Msg, IntPtr wParam, string lParam);
        [DllImport("user32.DLL")]
        static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
        [DllImport("user32.dll", SetLastError = true)]
        static extern int GetWindowLong(IntPtr hWnd, int nIndex);//获得所属句柄窗体的样式函数
 
/// <summary>
        /// Changes an attribute of the specified window. The function also sets the 32-bit (long) value at the specified offset into the extra window memory.
        /// </summary>
        /// <param name="hWnd">A handle to the window and, indirectly, the class to which the window belongs..</param>
        /// <param name="nIndex">The zero-based offset to the value to be set. Valid values are in the range zero through the number of bytes of extra window memory, minus the size of an integer. To set any other value, specify one of the following values: GWL_EXSTYLE, GWL_HINSTANCE, GWL_ID, GWL_STYLE, GWL_USERDATA, GWL_WNDPROC </param>
        /// <param name="dwNewLong">The replacement value.</param>
        /// <returns>If the function succeeds, the return value is the previous value of the specified 32-bit integer.
        /// If the function fails, the return value is zero. To get extended error information, call GetLastError. </returns>
        [DllImport("user32.dll")]
        static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

	//窗体样式 Window Styles 
        const int WS_OVERLAPPED = 0;
        const int WS_POPUP = 0x80000000;
        const int WS_CHILD = 0x40000000;
        const int WS_MINIMIZE = 0x20000000;
        const int WS_VISIBLE = 0x10000000;
        const int WS_DISABLED = 0x8000000;//这个就是是否灰色显示
        const int WS_CLIPSIBLINGS = 0x4000000;
        const int WS_CLIPCHILDREN = 0x2000000;
        const int WS_MAXIMIZE = 0x1000000;
        const int WS_CAPTION = 0xC00000;      // WS_BORDER or WS_DLGFRAME  
        const int WS_BORDER = 0x800000;
        const int WS_DLGFRAME = 0x400000;
        const int WS_VSCROLL = 0x200000;
        const int WS_HSCROLL = 0x100000;
        const int WS_SYSMENU = 0x80000;
        const int WS_THICKFRAME = 0x40000;
        const int WS_GROUP = 0x20000;
        const int WS_TABSTOP = 0x10000;
        const int WS_MINIMIZEBOX = 0x20000;
        const int WS_MAXIMIZEBOX = 0x10000;
        const int WS_TILED = WS_OVERLAPPED;
        const int WS_ICONIC = WS_MINIMIZE;
        const int WS_SIZEBOX = WS_THICKFRAME;

        // Extended Window Styles 
        const int WS_EX_DLGMODALFRAME = 0x0001;
        const int WS_EX_NOPARENTNOTIFY = 0x0004;
        const int WS_EX_TOPMOST = 0x0008;
        const int WS_EX_ACCEPTFILES = 0x0010;
        const int WS_EX_TRANSPARENT = 0x0020;
        const int WS_EX_MDICHILD = 0x0040;
        const int WS_EX_TOOLWINDOW = 0x0080;
        const int WS_EX_WINDOWEDGE = 0x0100;
        const int WS_EX_CLIENTEDGE = 0x0200;
        const int WS_EX_CONTEXTHELP = 0x0400;
        const int WS_EX_RIGHT = 0x1000;
        const int WS_EX_LEFT = 0x0000;
        const int WS_EX_RTLREADING = 0x2000;
        const int WS_EX_LTRREADING = 0x0000;
        const int WS_EX_LEFTSCROLLBAR = 0x4000;
        const int WS_EX_RIGHTSCROLLBAR = 0x0000;
        const int WS_EX_CONTROLPARENT = 0x10000;
        const int WS_EX_STATICEDGE = 0x20000;
        const int WS_EX_APPWINDOW = 0x40000;
        const int WS_EX_OVERLAPPEDWINDOW = (WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE);
        const int WS_EX_PALETTEWINDOW = (WS_EX_WINDOWEDGE | WS_EX_TOOLWINDOW | WS_EX_TOPMOST);
        const int WS_EX_LAYERED = 0x00080000;
        const int WS_EX_NOINHERITLAYOUT = 0x00100000; // Disable inheritence of mirroring by children
        const int WS_EX_LAYOUTRTL = 0x00400000; // Right to left mirroring
        const int WS_EX_COMPOSITED = 0x02000000;
        const int WS_EX_NOACTIVATE = 0x08000000;




         public Form1()
        {
            InitializeComponent();
        }

         private void button1_Click(object sender, EventArgs e)
         {

             IntPtr test = FindWindow("#32770", "网站.doc 属性");//一个示例顶层窗体,右键点击某个文件属性选项出现的
             IntPtr yingyong = FindWindowEx(test, IntPtr.Zero, "Button", "应用(&A)");//弹出的文件属性对话框中“应用”按钮句柄 当时是灰色显示
             if (yingyong != IntPtr.Zero)
             {
                 MessageBox.Show("我找到了" + yingyong.ToString("X"));//显示找到的应用按钮句柄地址  0x91A2C     
                 int temp = GetWindowLong(yingyong,-16);        
                 MessageBox.Show(temp.ToString("X"));//获得应用按钮的总样式数值 0x58010000
                 if ((temp & WS_DISABLED) != 0)
                 {
                     MessageBox.Show("有WS_DISABLED");//如果想判断该控件是否具有此项样式,就用获得的总样式和这个样式值进行与操作,如果为0证明无此属性,如果不为0,证明有此样式属性。
                 }
 
		int settemp = temp &= ~(WS_DISABLED);//取反设置反向属性,然后与总样式值与
                 MessageBox.Show(settemp.ToString("X"));
                 int ok=SetWindowLong(yingyong, GWL_STYLE, settemp);//设置新属性
                 MessageBox.Show(ok.ToString());                 
                 
             }
             else
             {
                 MessageBox.Show("没有找到句柄");
             }
         }
}


截图如下:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值