很简单的问题,关于API。(100分)

  • 主题发起人 主题发起人 HookBoy
  • 开始时间 开始时间
H

HookBoy

Unregistered / Unconfirmed
GUEST, unregistred user!
我在程序中用<br>WinExec<br><br>执行了一个由用户选择的应用程序,我要把它的窗口提到桌面前来,<br>用SetForegroundWindow<br><br>但是我不知道用户选择了什么程序,因而不知道窗口的标题。<br>也就无法靠findwindow(nil,你程序的名称)来找这个程序的Handl。<br><br>所以如何执行了一个由用户选择的应用程序后,把它的窗口提到桌面前来。或是取得该句柄?
 
我采用了别人写的一个函数,得到THandle,可是发现并不如我所愿,实事上并没有找到这个<br>句柄。<br><br>function MyWinExec(const CmdLine: string; CmdShow: DWord): THandle;<br>var<br>&nbsp; SI :TStartupInfo;<br>&nbsp; PI :TProcessInformation;<br>begin<br>&nbsp; FillChar(si,SizeOf(si),0);<br>&nbsp; si.cb:=SizeOf(si);<br>&nbsp; si.dwFlags:=STARTF_USESHOWWINDOW;<br>&nbsp; si.wShowWindow:=CmdShow;<br>&nbsp; if CreateProcess(nil,PChar(CmdLine),nil,nil,false,0,nil,nil,si,pi) then<br>&nbsp; &nbsp; Result:=pi.hProcess<br>&nbsp; else<br>&nbsp; &nbsp; Result:=0;<br>end;<br><br>我的调用<br>var<br>&nbsp; s:string;<br>begin<br>&nbsp; ppp := MyWinExec('C:/WINNT/system32/notepad.exe', SW_Show );<br>&nbsp; GetWindowText( ppp, PChar(s), Length (s));<br>&nbsp; ShowMessage( s )<br>end;<br>结果没能得到notepad的窗口标题。<br>怎么回事?<br><br>
 
为什么这些天了没人睬?
 
换一个参数,用SW_MAXIMIZE看看<br>要不就用ShellExecute这个函数,返回值如下<br>If the function succeeds, the return value is <br>the instance handle of the application that was run, <br>or the handle of a dynamic data exchange (DDE) <br>server application.<br>
 
yanghai0437<br>对ShellExecute,我试了一下,不行的。还是找不到这个Handle;返回的值不知道是什么了。<br><br>SW_MAXIMIZE,是一点没用的。还是老样子。
 
pi.hProcess 只是一个进程句柄,和窗口句柄是两码事,当然不能混为一谈了。<br><br>通过进程句柄获得主窗口句柄我暂时没有好的办法,只能枚举所有窗口,然后<br>判断每个窗口对应的进程是否为指定进程号。<br>你前面的 MyWinExec 应略作修改:<br><br>function MyWinExec(const CmdLine: string; CmdShow: DWord): THandle;<br>var<br>&nbsp; SI :TStartupInfo;<br>&nbsp; PI :TProcessInformation;<br>begin<br>&nbsp; FillChar(si,SizeOf(si),0);<br>&nbsp; si.cb:=SizeOf(si);<br>&nbsp; si.dwFlags:=STARTF_USESHOWWINDOW;<br>&nbsp; si.wShowWindow:=CmdShow;<br>&nbsp; if CreateProcess(nil,PChar(CmdLine),nil,nil,false,0,nil,nil,si,pi) then<br>&nbsp; &nbsp; Result:=pi.dwProcessId &nbsp;// 这里改成进程号!!!!!<br>&nbsp; else<br>&nbsp; &nbsp; Result:=0;<br>end;<br><br>// 通过进程号查找主窗口句柄<br>function GetApplicationWnd(const ProcessID: DWORD): HWND;<br>type<br>&nbsp; PTopLevelWnd = ^TTopLevelWnd;<br>&nbsp; TTopLevelWnd = record<br>&nbsp; &nbsp; ProcessID: DWORD;<br>&nbsp; &nbsp; Wnd: HWND;<br>&nbsp; end;<br>var<br>&nbsp; TopLevelWnd: TTopLevelWnd;<br><br>&nbsp; function EnumWinProc(Wnd: HWND; Param: PTopLevelWnd): BOOL; stdcall;<br>&nbsp; var<br>&nbsp; &nbsp; PID: DWORD;<br>&nbsp; &nbsp; C: array [0..Length(ClassNameOfTApplication) + 1] of Char;<br>&nbsp; begin<br>&nbsp; &nbsp; GetWindowThreadProcessId(Wnd, @PID);<br>&nbsp; &nbsp; if (PID = Param^.ProcessID) and (GetClassName(Wnd, C, SizeOf(C)) &gt; 0) and<br>&nbsp; &nbsp; &nbsp; (C = ClassNameOfTApplication) then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; Result := False;<br>&nbsp; &nbsp; &nbsp; Param^.Wnd := Wnd;<br>&nbsp; &nbsp; end<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; Result := True;<br>&nbsp; end;<br><br>begin<br>&nbsp; TopLevelWnd.ProcessID := ProcessID;<br>&nbsp; TopLevelWnd.Wnd := 0;<br>&nbsp; EnumWindows(@EnumWinProc, LPARAM(@TopLevelWnd));<br>&nbsp; Result := TopLevelWnd.Wnd;<br>end;<br><br><br>然后就像这样使用:<br><br>var<br>&nbsp; s: string;<br>&nbsp; pid: DWord;<br>&nbsp; h: THandle;<br>begin<br>&nbsp; pid := MyWinExec('C:/WINNT/system32/notepad.exe', SW_Show);<br>&nbsp; h := GetApplicationWnd(pid);<br>&nbsp; SetLength(s, 255);<br>&nbsp; GetWindowText(h, PChar(s), Length(s));<br>&nbsp; ShowMessage(s);<br>end;<br><br>
 
我想要在Sendkeys中,先选中文本,后发送字符串,但总是不行,<br>大家看看。<br><br>procedure Sendkeys(focushld: HWND; sSend: string);<br>var<br>&nbsp; i:integer;<br>&nbsp; ch: byte;<br>begin<br>&nbsp; i := 1;<br><br>&nbsp; SendMessage(focushld, WM_IME_SELECT, WPARAM(true), 255); &nbsp; <br>&nbsp;// 要选中文本,但这处没有选中。<br><br>&nbsp; while i &lt;= Length(sSend) do<br>&nbsp; begin<br>&nbsp; &nbsp; ch := byte(sSend);<br>&nbsp; &nbsp; if Windows.IsDBCSLeadByte(ch) then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; Inc(i);<br>&nbsp; &nbsp; &nbsp; SendMessage(focushld, WM_IME_CHAR, MakeWord(byte(sSend), ch), 0);<br>&nbsp; &nbsp; end<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; SendMessage(focushld, WM_IME_CHAR, word(ch), 0);<br>&nbsp; Inc(i);<br>&nbsp; end;<br>end;
 
這裡也有討論的, 你去看看<br>http://www.delphibbs.com/delphibbs/DispQ.asp?LID=1395209
 
winexec('c:/rr.exe',1)
 
接受答案了.
 
后退
顶部