Ver Mensaje Individual
  #1  
Antiguo 05-01-2020
MaxiDucoli MaxiDucoli is offline
Miembro
 
Registrado: feb 2006
Posts: 134
Reputación: 19
MaxiDucoli Va por buen camino
Optimizar código

Hola, buenas tardes.
Aprovecho que hay un gurú que sabe bocha de WinApi y quería preguntar lo siguiente:

Tengo este código insertado en un Timer dentro de un formulario que se ejecuta como "Loading Screen" mientras se carga un emulador de fondo.
Una vez que abrió el emulador el código busca el Caption del mismo y lo trae al frente y luego se cierra.
Hay alguna manera de optimizar el código este o sí o sí se tiene que hacer de una manera como esta?

Se pueden buscar los exe en ejecución y terminarlos directamente? Eso sería de mucha ayuda para el programa principal así puedo cerrar los emuladores al presionar ESCAPE (que ya me solucionaron como hacerlo, pero lo hago algo "Sucio" y a veces falla).

Les dejo el código y muchas gracias de ante mano!!!!


Código Delphi [-]
unit close;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, jpeg, pngimage, Vcl.Forms,TLHelp32, Vcl.Dialogs, JvBackgrounds, Vcl.ExtCtrls;

type
  TFrmLaunchIMG = class(TForm)
    JvBackground1: TJvBackground;
    TmrClose: TTimer;
    procedure FormCreate(Sender: TObject);
    procedure TmrCloseTimer(Sender: TObject);

  private
    { Private declarations }

  public
    { Public declarations }
  end;

var
  FrmLaunchIMG: TFrmLaunchIMG;
  Contador : integer;
  Win2 : HWND;
  Mensaje : String;

implementation

{$R *.dfm}



function FindWindowExtd(partialTitle: string): HWND;
var
  hWndTemp: hWnd;
  iLenText: Integer;
  cTitletemp: array [0..254] of Char;
  sTitleTemp: string;
begin
  hWndTemp := FindWindow(nil, nil);
  while hWndTemp <> 0 do begin
    iLenText := GetWindowText(hWndTemp, cTitletemp, 255);
    sTitleTemp := cTitletemp;
    sTitleTemp := UpperCase(copy( sTitleTemp, 1, iLenText));
    partialTitle := UpperCase(partialTitle);
    if pos( partialTitle, sTitleTemp ) <> 0 then
      Break;
    hWndTemp := GetWindow(hWndTemp, GW_HWNDNEXT);
  end;
  result := hWndTemp;
end;

procedure TFrmLaunchIMG.FormCreate(Sender: TObject);
begin
FrmLaunchIMG.Height := Screen.Height;
FrmLaunchIMG.Width := Screen.Width;
JVBackGround1.Image.Picture.LoadFromFile(ParamStr(1));
Contador := 0;
end;

procedure TFrmLaunchIMG.TmrCloseTimer(Sender: TObject);
begin
SetWindowPos(FrmLaunchIMG.Handle, HWND_TOPMOST, 0, 0, 0, 0,
                 SWP_NoMove or SWP_NoSize);
 inc(Contador);

 if Contador > 5 then Contador := 0;


      if Contador = 5 then
      Begin

            Win2 :=  FindWindowExtd('RetroArch');
            if IsWindow(Win2) then
            begin
                Sleep(200);
                //SetForegroundWindow(Win2);
                SetWindowPos(Win2, HWND_TOPMOST, 0, 0, 0, 0,
                SWP_NoMove or SWP_NoSize);
                application.Terminate;
            end;

            Win2 :=  FindWindowExtd('PPSSPP ');
            if IsWindow(Win2) then
            begin
                Sleep(200);
                //SetForegroundWindow(Win2);
                SetWindowPos(Win2, HWND_TOPMOST, 0, 0, 0, 0,
                SWP_NoMove or SWP_NoSize);
                application.Terminate;
            end;

            Win2 :=  FindWindowExtd('Slot');
            if IsWindow(Win2) then
            begin
                Sleep(200);
                //SetForegroundWindow(Win2);
                SetWindowPos(Win2, HWND_TOPMOST, 0, 0, 0, 0,
                SWP_NoMove or SWP_NoSize);
                application.Terminate;
            end;

            Win2 :=  FindWindowExtd('Cemu');
            if IsWindow(Win2) then
            begin
                Sleep(200);
                //SetForegroundWindow(Win2);
                SetWindowPos(Win2, HWND_TOPMOST, 0, 0, 0, 0,
                SWP_NoMove or SWP_NoSize);
                application.Terminate;
            end;

            Win2 :=  FindWindowExtd('Redream');
            if IsWindow(Win2) then
            begin
                Sleep(200);
                //SetForegroundWindow(Win2);
                SetWindowPos(Win2, HWND_TOPMOST, 0, 0, 0, 0,
                SWP_NoMove or SWP_NoSize);
                application.Terminate;
            end;

            Win2 :=  FindWindowExtd('FS-UAE');
            if IsWindow(Win2) then
            begin
                Sleep(200);
                //SetForegroundWindow(Win2);
                SetWindowPos(Win2, HWND_TOPMOST, 0, 0, 0, 0,
                SWP_NoMove or SWP_NoSize);
                application.Terminate;
            end;

            Win2 :=  FindWindowExtd('Future');
            if IsWindow(Win2) then
            begin
                Sleep(200);
                //SetForegroundWindow(Win2);
                SetWindowPos(Win2, HWND_TOPMOST, 0, 0, 0, 0,
                SWP_NoMove or SWP_NoSize);
                application.Terminate;
            end;

            Win2 :=  FindWindowExtd('MAME');
            if IsWindow(Win2) then
            begin
                Sleep(200);
                //SetForegroundWindow(Win2);
                SetWindowPos(Win2, HWND_TOPMOST, 0, 0, 0, 0,
                SWP_NoMove or SWP_NoSize);
                application.Terminate;
            end;

            Win2 :=  FindWindowExtd('scummvm');
            if IsWindow(Win2) then
            begin
                Sleep(200);
                //SetForegroundWindow(Win2);
                SetWindowPos(Win2, HWND_TOPMOST, 0, 0, 0, 0,
                SWP_NoMove or SWP_NoSize);
                application.Terminate;
            end;

            Win2 :=  FindWindowExtd('Dolphin');
            if IsWindow(Win2) then
            begin
                Sleep(200);
                //SetForegroundWindow(Win2);
                SetWindowPos(Win2, HWND_TOPMOST, 0, 0, 0, 0,
                SWP_NoMove or SWP_NoSize);
                application.Terminate;
            end;

            Win2 :=  FindWindowExtd('WINGAMES');
            if IsWindow(Win2) then
            begin
                Sleep(200);
                //SetForegroundWindow(Win2);
                SetWindowPos(Win2, HWND_TOPMOST, 0, 0, 0, 0,
                SWP_NoMove or SWP_NoSize);
                application.Terminate;
            end;

            Win2 :=  FindWindowExtd('DAPHNE');
            if IsWindow(Win2) then
            begin
                Sleep(200);
                //SetForegroundWindow(Win2);
                SetWindowPos(Win2, HWND_TOPMOST, 0, 0, 0, 0,
                SWP_NoMove or SWP_NoSize);
                application.Terminate;
            end;
      End;
end;



end.
Responder Con Cita