资源描述:
《C#利用句柄操作窗口》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、C#写个类操作窗口(句柄操作)实现过程:过程一:找到当前鼠标位置的句柄您的使用2个WinAPI(俺喜欢自己封装下来用):ViewCode[DllImport("user32.dll",EntryPoint="GetCursorPos")]publicstaticexternboolGetCursorPos(outPointpt);[DllImport("user32.dll",EntryPoint="WindowFromPoint")]publicstaticexternIntPtrWindowFromPoint(Poi
2、ntpt);//鼠标位置的坐标publicstaticPointGetCursorPosPoint(){Pointp=newPoint();if(GetCursorPos(outp)){returnp;}returndefault(Point);}//////找到句柄//////坐标///publicstaticIntPtrGetHandle(Pointp){returnWindowFromPoint
3、(p);}过程二:改变窗口的Text您的使用1个WinAPI:ViewCode[DllImport("user32.dll",EntryPoint="SendMessage")]privatestaticexternintSendMessage(IntPtrhWnd,intMsg,IntPtrwParam,stringlParam);//////给窗口发送内容//////句柄///要发送的内容
4、publicstaticvoidSetText(IntPtrhWnd,stringlParam){SendMessage(hWnd,WM_SETTEXT,IntPtr.Zero,lParam);}privateconstintWM_SETTEXT=0x000C;通过这个方法就能改变Text的值了。思考:如果俺把这个窗口的句柄记录下来,下次不用鼠标获取,直接就能改变值不蛮好的嘛。例如:我有个桌面系统老叫我输入用户名,密码。我记录用户名和密码的窗口句柄,然后改变他们的输入值,这样多省事。(只是举例,不考虑安全
5、性)问题:你会告诉我,窗口句柄的每次重建会变的呀,咋办。回答:类名不变呀。过程三:您的准备一些工具吧,例如:句柄找类名呀,类名找句柄什么的等等,下面会用到一些WinAPIViewCode[DllImport("user32.dll",EntryPoint="FindWindow")]privatestaticexternIntPtrFindWindow(stringIpClassName,stringIpWindowName);[DllImport("user32.dll",EntryPoint="FindWindowE
6、x")]privatestaticexternIntPtrFindWindowEx(IntPtrhwndParent,IntPtrhwndChildAfter,stringlpszClass,stringlpszWindow);[DllImport("user32.dll",EntryPoint="GetParent")]publicstaticexternIntPtrGetParent(IntPtrhWnd);[DllImport("user32.dll",CharSet=CharSet.Auto)]publicsta
7、ticexternintGetClassName(IntPtrhWnd,StringBuilderlpClassName,intnMaxCount);//////找到句柄//////类名///publicstaticIntPtrGetHandle(stringIpClassName){returnFindWindow(IpClassName,null);}////
8、//子窗口句柄//////父窗口句柄///前一个同目录级同名窗口句柄///类名///