帮我看看这个代码有什么问题?关于改变当前窗口标题的程序。(100分)

  • 主题发起人 satanmonkey
  • 开始时间
S

satanmonkey

Unregistered / Unconfirmed
GUEST, unregistred user!
我想写一个程序,把当前窗口的标题最后加上几个字符。<br>可是写出来以后,只有我写的这个程序是当前窗口的时候运行有效果。<br>其他窗口是当前窗口的时候GetForegroundWindow老返回0。<br>不知道是什么原因。请各位高手帮我看一下。<br><br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>handle:integer;<br>title,right: string;<br>count,EditLong,TitleLong:integer;<br>begin<br>&nbsp; handle:=GetForegroundWindow; &nbsp;//得到当前窗口句柄<br>&nbsp; TitleLong:=getwindowtextlength(handle);//得到当前窗口标题长度<br>&nbsp; setlength(title,TitleLong);<br>&nbsp; handle:=GetActiveWindow();<br>&nbsp; GetWindowText(handle,pchar(title),256););//得到当前窗口标题<br>&nbsp; EditLong:=length(edit1.Text);<br>&nbsp; right:=rightstr(title,EditLong);<br>&nbsp; if (TitleLong&lt;EditLong) or (strcomp(pchar(right),pchar(edit1.text))&lt;&gt;0) then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; setwindowtext(handle,pchar(title+'-'+edit1.Text)));//设置当前窗口标题<br>&nbsp; &nbsp; end;<br>end;
 
你点击按钮的时候当前窗体一定是你的,呵呵,你肯定得不到其他的窗体<br>做成hook 吧
 
我把这段代码放到Ttimer里去了,不断的调用button1.click。可惜没用,只要当前窗口不是是我写的这个程序,得到的句柄都是0<br>如果是自己写的这个程序的窗口就很正常。应该是哪里有点问题。<br>谁知道GetForegroundWindow是不是只对进程内的窗口有用?<br>我换成getactivewindow也不行啊。<br>太奇怪了。<br><br>另外,人在昆明<br>看你的ID你应该和我是一个城市的哦,多交流一下。
 
The GetActiveWindow function retrieves the window handle to the active window associated with the thread that calls the function. <br>所以不能用handle:=GetActiveWindow();这一句的<br>只要把这一句注释掉就可以了
 
sendmessage(hwnd,wm_settext,0,LPARAM(LPCTSTR(@pd)));<br>这样鼠标指着的窗口标题就换成了pd.
 
shenloqi:不用GetActiveWindow()我该用什么函数取得当前窗口的句柄?<br>nick4309:我现在的问题是得不到hwnd这个参数<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setwindowtext(handle,pchar(title+'-'+edit1.Text)));//设置当前窗口标题<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 这一句相当于你告诉我的那句的功能。<br>
 
问题不在GetForegroundWindow以下代码可以的呀!<br>procedure TForm1.Timer1Timer(Sender: TObject);<br>var<br>handle:integer;<br>title,right: string;<br>count,EditLong,TitleLong:integer;<br>begin<br>&nbsp; handle:=GetForegroundWindow; &nbsp;//得到当前窗口句柄<br>&nbsp; setwindowtext(handle,pchar(edit1.Text));//设置当前窗口标题<br>{ &nbsp;TitleLong:=getwindowtextlength(handle);//得到当前窗口标题长度<br>&nbsp; setlength(title,TitleLong);<br>&nbsp; handle:=GetActiveWindow();<br>&nbsp; GetWindowText(handle,pchar(title),256);//得到当前窗口标题<br>&nbsp; EditLong:=length(edit1.Text);<br>&nbsp; right:=rightstr(title,EditLong);<br>&nbsp; if (TitleLong&lt;EditLong) or (strcomp(pchar(right),pchar(edit1.text))&lt;&gt;0) then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; setwindowtext(handle,pchar(title+'-'+edit1.Text));//设置当前窗口标题<br>&nbsp; &nbsp; end;}<br>end;<br>
 
我调试了只要注释掉下面这一句就可以了<br>handle:=GetActiveWindow();
 
我终于发现,原来是我忘记注释<br>handle:=GetActiveWindow();<br>这句,我真是粗心,shenloqi告诉我了,我还没发现。<br><br>
 
顶部