Ir para conteúdo
Fórum Script Brasil
  • 0

HInst e Handle


Guest --Leandro --

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

  • 0
O ShellExecute retorna o Handle do programa, eu queria pegar o Handle da janela, para poder usar o SendMessage..

Tente dessa maneira:

procedure TForm1.Button1Click(Sender: TObject);
var 
  H: HWND; 
  WindowName: Array[0..255] of Char; 
  ClassName: Array[0..255] of Char; 
begin 
  H := GetForegroundWindow; 
  GetWindowText(H, WindowName, SizeOf(WindowName));
  GetClassName(H, ClassName, SizeOf(ClassName)); 
  Label1.Caption := 'Nome-> '+WindowName+' / Classe-> '+ClassName;
end;

De uma olhada neste post tambem

http://scriptbrasil.com.br/forum/index.php...st&p=391451

abraço

Link para o comentário
Compartilhar em outros sites

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.

Visitante
Responder esta pergunta...

×   Você colou conteúdo com formatação.   Remover formatação

  Apenas 75 emoticons são permitidos.

×   Seu link foi incorporado automaticamente.   Exibir como um link em vez disso

×   Seu conteúdo anterior foi restaurado.   Limpar Editor

×   Você não pode colar imagens diretamente. Carregar ou inserir imagens do URL.



  • Estatísticas dos Fóruns

    • Tópicos
      152,2k
    • Posts
      651,9k
×
×
  • Criar Novo...