function ProcIDFromWnd(hwnd: HWND): Integer;
var
idProc: Integer;
begin
GetWindowThreadProcessId(hwnd, @idProc);
Result := idProc;
end;
function GetWinHandle(hInstance: HINST): Integer;
var
tempHwnd: HWND;
begin
// Grab the first window handle that Windows finds
tempHwnd := FindWindow(nil, nil);
// Loop until you find a match or there are no more window handles:
While tempHwnd <> 0 do
begin
// Check if no parent for this window
if GetParent(tempHwnd) = 0 then begin
// Check for PID match
if hInstance = ProcIDFromWnd(tempHwnd) Then begin
// Return found handle
Result := tempHwnd;
// Exit search loop
break;
end;
end;
// Get the next window handle
tempHwnd := GetWindow(tempHwnd, GW_HWNDNEXT)
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
hInst, hWndApp: HWND; // Instance handle from Shell function.
buffer: array[0..260] of char;
numChars: Integer; // Count of bytes returned.
begin
hInst := ShellExecute(0, 'open', 'calc.exe', 0, 0, SW_SHOWNORMAL);
// Begin search for handle
hWndApp := GetWinHandle(hInst);
if hWndApp <> 0 then begin
// Get caption of window
GetWindowText(hWndApp, buffer, 255);
// Display window's caption
ShowMessage('You shelled to the Application: ' + StrPas(buffer));
end;
end;
Pergunta
Guest --Leandro --
O ShellExecute retorna o Handle do programa, eu queria pegar o Handle da janela, para poder usar o SendMessage..
Eu axei isso no site da microsoft e portei para Delphi, mas não funcionou...
http://support.microsoft.com/kb/242308
function ProcIDFromWnd(hwnd: HWND): Integer; var idProc: Integer; begin GetWindowThreadProcessId(hwnd, @idProc); Result := idProc; end; function GetWinHandle(hInstance: HINST): Integer; var tempHwnd: HWND; begin // Grab the first window handle that Windows finds tempHwnd := FindWindow(nil, nil); // Loop until you find a match or there are no more window handles: While tempHwnd <> 0 do begin // Check if no parent for this window if GetParent(tempHwnd) = 0 then begin // Check for PID match if hInstance = ProcIDFromWnd(tempHwnd) Then begin // Return found handle Result := tempHwnd; // Exit search loop break; end; end; // Get the next window handle tempHwnd := GetWindow(tempHwnd, GW_HWNDNEXT) end; end; procedure TForm1.Button1Click(Sender: TObject); var hInst, hWndApp: HWND; // Instance handle from Shell function. buffer: array[0..260] of char; numChars: Integer; // Count of bytes returned. begin hInst := ShellExecute(0, 'open', 'calc.exe', 0, 0, SW_SHOWNORMAL); // Begin search for handle hWndApp := GetWinHandle(hInst); if hWndApp <> 0 then begin // Get caption of window GetWindowText(hWndApp, buffer, 255); // Display window's caption ShowMessage('You shelled to the Application: ' + StrPas(buffer)); end; end;alguém poderia me ajudar?
Grato!
Link para o comentário
Compartilhar em outros sites
1 resposta a esta questão
Posts Recomendados
Participe da discussão
Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.