300分求解,如何执行一个外部程序,并得到Handle,以便SetForegroundWindow.(300分)

  • 主题发起人 主题发起人 newnob
  • 开始时间 开始时间
稍微帮你改了一下, 能用了:<br>function ShowChildWinID( WinCaption:string; CtrlClass:string ):boolean;<br>var<br>&nbsp; cnt: integer;<br><br>&nbsp; function EnumChildProc(Hwnd:THandle;lParam:LParam):boolean; stdcall;<br>&nbsp; var<br>&nbsp; &nbsp; WindowCaption,WindowClass:array[0..254] of Char;<br>&nbsp; begin<br>&nbsp; &nbsp; GetClassName(Hwnd,WindowClass,255);<br>&nbsp; &nbsp; if Pos('EDIT',UpperCase(StrPas(WindowClass))) &gt; 0 then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; //Inc(cnt); 不能访问这个变量的!<br>&nbsp; &nbsp; &nbsp; SendMessage(Hwnd,WM_SETTEXT,0,LongInt(PChar('IntToStr(cnt)')));<br>&nbsp; &nbsp; &nbsp; //if cnt = ChildWinID then<br>&nbsp; &nbsp; &nbsp; &nbsp; //SendMessage(Hwnd,WM_SETTEXT,0,LongInt(PChar(s)));<br>&nbsp; &nbsp; &nbsp;//这里是为了向固定的ChildWinID显示S<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; Result := True;<br>&nbsp; end;<br><br>&nbsp; function EnumWindowsProc(Hwnd:THandle;lParam:LParam):boolean;stdcall;<br>&nbsp; var<br>&nbsp; &nbsp; WindowCaption:array[0..254] of Char;<br>&nbsp; &nbsp; WinCapStr:string;<br>&nbsp; begin<br>&nbsp; &nbsp; GetWindowText(Hwnd,WindowCaption,255);<br>&nbsp; &nbsp; if StrPas(WindowCaption)= String(Pointer(lParam)) then<br>&nbsp; &nbsp; &nbsp; WinCapStr := StrPas(WindowCaption);<br>&nbsp; &nbsp; if WinCapStr = String(Pointer(lParam)) then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; cnt := 0;<br>&nbsp; &nbsp; &nbsp; EnumChildWindows(Hwnd,@EnumChildProc,0);<br>&nbsp; &nbsp; &nbsp; Result := False;<br>&nbsp; &nbsp; &nbsp; Exit;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; Result := True;<br>&nbsp; end;<br><br>begin<br>&nbsp; Enumwindows(@EnumWindowsProc, Integer(Pointer(WinCaption)));<br>end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>begin<br>&nbsp; ShowChildWinID('Form1','TEdit');<br>end;<br>
 
我这有好几段也是有去无回的,随便找一段来配个对儿,请帮忙看一看。<br>&nbsp; Win1 := FindWindow('Shell_TrayWnd', '');<br>&nbsp; if Win1 &gt; 0 then // &nbsp;---&gt;TrayNotifyWnd-----&gt;TrayClockWClass<br>&nbsp; begin<br>&nbsp; &nbsp; MessageBox(Handle, PChar(IntToStr(Win1)), 'Win1 find ...', mb_Ok);<br>&nbsp; &nbsp; Win2 := FindWindowEX(Win1, 0, 'TrayNotifyWnd', '');<br>&nbsp; &nbsp; if Win2 &gt; 0 then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; MessageBox(Handle, PChar(IntToStr(Win2)), 'Win2 find ...', mb_Ok);<br>&nbsp; &nbsp; &nbsp; Win3 := FindWindowEX(Win2, 0, 'TrayClockWClass', nil);<br>&nbsp; &nbsp; &nbsp; if Win3 &gt; 0 then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; MessageBox(Handle, PChar(IntToStr(Win3)), 'Win3 find ...', mb_Ok);<br>&nbsp; &nbsp; &nbsp; &nbsp; GetWindowThreadProcessId(Win3, @AProcID);<br>&nbsp; &nbsp; &nbsp; &nbsp; if AProcID &gt; 0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MessageBox(Handle, PChar(IntToStr(AProcID)), 'AProcID', mb_Ok);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; OldWndProc := Pointer(GetWindowLong(Win3, GWL_WNDPROC));// 取不到<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MessageBox(Handle,PChar(IntToStr(Longint(OldWndProc))), 'OldWndProc', mb_Ok);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ptr := MakeObjectInstance(NewWndProc);<br>&nbsp; &nbsp; &nbsp;........
 
小雨哥:<br>&nbsp; 我这里也取不出来,好象错误是“Access Denied.”
 
//Ex:WinExecW('c:/windows/notepad.exe','',sw_show); <br>//看看對你有沒有幫助...<br><br>Function WinExecExW(cmd,workdir:pchar;visiable:integer):DWORD; <br>var<br>&nbsp;StartupInfo:TStartupInfo;<br>&nbsp;ProcessInfo:TProcessInformation; <br>begin<br>&nbsp;FillChar(StartupInfo,SizeOf(StartupInfo),#0);<br>&nbsp;StartupInfo.cb:=SizeOf(StartupInfo);<br>&nbsp;StartupInfo.dwFlags:=STARTF_USESHOWWINDOW;<br>&nbsp;StartupInfo.wShowWindow:=visiable;<br>&nbsp;if not CreateProcess(nil,cmd,nil,nil,false,Create_new_console or Normal_priority_class,nil,nil,StartupInfo,ProcessInfo) then<br>&nbsp; &nbsp;result:=0<br>&nbsp;else<br>&nbsp;begin<br>&nbsp; &nbsp;waitforsingleobject(processinfo.hProcess,INFINITE);<br>&nbsp; &nbsp;GetExitCodeProcess(ProcessInfo.hProcess,Result);<br>&nbsp;end; <br>end;
 
没什么好方法的,还是用 FindWindows做吧.
 
接受答案了.
 
后退
顶部