如何获得WIN32 窗体中控件的样式属性(C#)
2012-02-21 01:42
453 查看
这阵子弄C#调用WIN32API互操作,想从Winform中查询外部WIN32程序窗体中的某个按钮是否可用,就是灰色或者黑色显示,找了些资料,解决了,特记录如下:
代码:
截图如下:
代码:
publicpartialclassForm1:Form { [DllImport("User32.dll",EntryPoint="FindWindow")] privatestaticexternIntPtrFindWindow(stringlpClassName,stringlpWindowName); [DllImport("User32.dll",EntryPoint="SendMessage")] privatestaticexternintSendMessage(IntPtrhWnd,intMsg,IntPtrwParam,stringlParam); [DllImport("user32.DLL")] staticexternIntPtrFindWindowEx(IntPtrhwndParent,IntPtrhwndChildAfter,stringlpszClass,stringlpszWindow); [DllImport("user32.dll",SetLastError=true)] staticexternintGetWindowLong(IntPtrhWnd,intnIndex);//获得所属句柄窗体的样式函数
///<summary>
///Changesanattributeofthespecifiedwindow.Thefunctionalsosetsthe32-bit(long)valueatthespecifiedoffsetintotheextrawindowmemory.
///</summary>
///<paramname="hWnd">Ahandletothewindowand,indirectly,theclasstowhichthewindowbelongs..</param>
///<paramname="nIndex">Thezero-basedoffsettothevaluetobeset.Validvaluesareintherangezerothroughthenumberofbytesofextrawindowmemory,minusthesizeofaninteger.Tosetanyothervalue,specifyoneofthefollowingvalues:GWL_EXSTYLE,GWL_HINSTANCE,GWL_ID,GWL_STYLE,GWL_USERDATA,GWL_WNDPROC</param>
///<paramname="dwNewLong">Thereplacementvalue.</param>
///<returns>Ifthefunctionsucceeds,thereturnvalueisthepreviousvalueofthespecified32-bitinteger.
///Ifthefunctionfails,thereturnvalueiszero.Togetextendederrorinformation,callGetLastError.</returns>
[DllImport("user32.dll")]
staticexternintSetWindowLong(IntPtrhWnd,intnIndex,intdwNewLong);
//窗体样式WindowStyles
constintWS_OVERLAPPED=0;
constintWS_POPUP=0x80000000;
constintWS_CHILD=0x40000000;
constintWS_MINIMIZE=0x20000000;
constintWS_VISIBLE=0x10000000;
constintWS_DISABLED=0x8000000;//这个就是是否灰色显示
constintWS_CLIPSIBLINGS=0x4000000;
constintWS_CLIPCHILDREN=0x2000000;
constintWS_MAXIMIZE=0x1000000;
constintWS_CAPTION=0xC00000;//WS_BORDERorWS_DLGFRAME
constintWS_BORDER=0x800000;
constintWS_DLGFRAME=0x400000;
constintWS_VSCROLL=0x200000;
constintWS_HSCROLL=0x100000;
constintWS_SYSMENU=0x80000;
constintWS_THICKFRAME=0x40000;
constintWS_GROUP=0x20000;
constintWS_TABSTOP=0x10000;
constintWS_MINIMIZEBOX=0x20000;
constintWS_MAXIMIZEBOX=0x10000;
constintWS_TILED=WS_OVERLAPPED;
constintWS_ICONIC=WS_MINIMIZE;
constintWS_SIZEBOX=WS_THICKFRAME;
//ExtendedWindowStyles
constintWS_EX_DLGMODALFRAME=0x0001;
constintWS_EX_NOPARENTNOTIFY=0x0004;
constintWS_EX_TOPMOST=0x0008;
constintWS_EX_ACCEPTFILES=0x0010;
constintWS_EX_TRANSPARENT=0x0020;
constintWS_EX_MDICHILD=0x0040;
constintWS_EX_TOOLWINDOW=0x0080;
constintWS_EX_WINDOWEDGE=0x0100;
constintWS_EX_CLIENTEDGE=0x0200;
constintWS_EX_CONTEXTHELP=0x0400;
constintWS_EX_RIGHT=0x1000;
constintWS_EX_LEFT=0x0000;
constintWS_EX_RTLREADING=0x2000;
constintWS_EX_LTRREADING=0x0000;
constintWS_EX_LEFTSCROLLBAR=0x4000;
constintWS_EX_RIGHTSCROLLBAR=0x0000;
constintWS_EX_CONTROLPARENT=0x10000;
constintWS_EX_STATICEDGE=0x20000;
constintWS_EX_APPWINDOW=0x40000;
constintWS_EX_OVERLAPPEDWINDOW=(WS_EX_WINDOWEDGE|WS_EX_CLIENTEDGE);
constintWS_EX_PALETTEWINDOW=(WS_EX_WINDOWEDGE|WS_EX_TOOLWINDOW|WS_EX_TOPMOST);
constintWS_EX_LAYERED=0x00080000;
constintWS_EX_NOINHERITLAYOUT=0x00100000;//Disableinheritenceofmirroringbychildren
constintWS_EX_LAYOUTRTL=0x00400000;//Righttoleftmirroring
constintWS_EX_COMPOSITED=0x02000000;
constintWS_EX_NOACTIVATE=0x08000000;
publicForm1()
{
InitializeComponent();
}
privatevoidbutton1_Click(objectsender,EventArgse)
{
IntPtrtest=FindWindow("#32770","网站.doc属性");//一个示例顶层窗体,右键点击某个文件属性选项出现的
IntPtryingyong=FindWindowEx(test,IntPtr.Zero,"Button","应用(&A)");//弹出的文件属性对话框中“应用”按钮句柄当时是灰色显示
if(yingyong!=IntPtr.Zero)
{
MessageBox.Show("我找到了"+yingyong.ToString("X"));//显示找到的应用按钮句柄地址0x91A2C
inttemp=GetWindowLong(yingyong,-16);
MessageBox.Show(temp.ToString("X"));//获得应用按钮的总样式数值0x58010000
if((temp&WS_DISABLED)!=0)
{
MessageBox.Show("有WS_DISABLED");//如果想判断该控件是否具有此项样式,就用获得的总样式和这个样式值进行与操作,如果为0证明无此属性,如果不为0,证明有此样式属性。
}
intsettemp=temp&=~(WS_DISABLED);//取反设置反向属性,然后与总样式值与
MessageBox.Show(settemp.ToString("X"));
intok=SetWindowLong(yingyong,GWL_STYLE,settemp);//设置新属性
MessageBox.Show(ok.ToString());
}
else
{
MessageBox.Show("没有找到句柄");
}
}
}
截图如下:
相关文章推荐
- C# Winform 如何获得窗体上控件相对于屏幕的位置
- 用C#如何遍历一个窗体中的某一种控件
- 【C#基础】之WinForm窗体常用控件的属性、方法及事件简介 .
- c#中如何跨线程调用windows窗体控件?
- 在C#中子线程如何操作主线程中窗体上控件
- C# winform中一个类中如何调用另一个窗体的控件或方法
- C#里WinForm开发中如何实现控件随窗体大小的改变而自动适应其改变(转)
- C#里WinForm开发中如何实现控件随窗体大小的改变而自动适应其改变
- 如何获得控件的属性
- C#主窗体和子窗体之间如何相互操作对方的控件
- C#里WinForm开发中如何实现控件随窗体大小的改变而自动适应其改变
- c#中如何跨线程调用windows窗体控件?
- 用C#如何遍历一个窗体中的某一种控件
- C#如何在webBrowser1控件通过TagName,Name查找元素(没有ID时)遍历窗体元素
- C# winform中一个类中如何调用另一个窗体的控件或方法
- c# 给窗体添加MouseEnter事件,可鼠标移到任意控件均会产生MouseEnter事件,如何识别是哪个产生的呢?
- HTML控件ID和NAME属性的区别,以及如何在asp.net页面的.CS文件中获得.ASPX页面中HTML控件的值
- C# 控件如何随窗体变大而变大
- C# 改变窗体中所有TextBox控件的属性
- [Gabriel的专栏] 用C#如何遍历一个窗体中的某一种控件