问题描述
我想用鼠标点击任何窗体,都能获得当前窗体上的标题!
解决方案
解决方案二:
1、是SetCapture,以便鼠标在窗口外的点击也能发送到本窗口,2、是用WindowFromPoint找到目标窗口,3、也就是GetWindowText了。简单例子如下:publicpartialclassForm1:Form{publicForm1(){InitializeComponent();this.button1.Text="StartCapture";}privatevoidbutton1_Click(objectsender,EventArgse){this.button1.Text="Capturing...";this.Text="Nowclickonthetargetwindow";this.Capture=true;}protectedoverridevoidOnMouseDown(MouseEventArgse){base.OnMouseDown(e);this.button1.Text="StartCapture";if(this.Capture){IntPtrtarget=WindowFromPoint(this.PointToScreen(e.Location));if(target!=IntPtr.Zero){StringBuildersb=newStringBuilder(256);GetWindowTextW(target,sb,256);this.Text=sb.ToString();}}}[DllImport("User32")]staticexternIntPtrWindowFromPoint(Pointpoint);[DllImport("User32",CharSet=CharSet.Unicode)]staticexternintGetWindowTextW(IntPtrhWnd,StringBuildersb,intmaxCount);}