Einzelnen Beitrag anzeigen

Benutzerbild von sakura
sakura

Registriert seit: 10. Jun 2002
Ort: München
11.412 Beiträge
 
Delphi 11 Alexandria
 
#4

AW: Anderes Programm verschieben und vergrößern

  Alt 4. Mai 2018, 13:21
Für Notepad gilt:
Delphi-Quellcode:
program ProjectNpMove;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils,
  Winapi.Windows,
  Winapi.PsAPI;

type
  TFindWindowRec = record
    ModuleToFind: string;
    FoundHWnd: HWND;
  end;

function EnumWindowsCallBack(Handle: hWnd; var FindWindowRec: TFindWindowRec): BOOL; stdcall;
const
  C_FileNameLength = 256;
var
  WinFileName: string;
  PID, hProcess: DWORD;
  Len: Byte;
begin
  Result := True;
  SetLength(WinFileName, C_FileNameLength);
  GetWindowThreadProcessId(Handle, PID);
  hProcess := OpenProcess(PROCESS_ALL_ACCESS, False, PID);
  Len := GetModuleFileNameEx(hProcess, 0, PChar(WinFileName), C_FileNameLength);
  if Len > 0 then
  begin
    SetLength(WinFileName, Len);
    if SameText(WinFileName, FindWindowRec.ModuleToFind) then
    begin
      Result := False;
      FindWindowRec.FoundHWnd := Handle;
    end;
  end;
end;

function MoveNotepad(const Left, Top, Width, Height: Integer): Boolean;
var
  FindWindowRec: TFindWindowRec;
begin
  FindWindowRec.ModuleToFind := 'c:\windows\system32\notepad.exe';
  FindWindowRec.FoundHWnd := 0;
  EnumWindows(@EnumWindowsCallback, integer(@FindWindowRec));
  Result := FindWindowRec.FoundHWnd <> 0;

  if Result then
    SetWindowPos(FindWindowRec.FoundHWnd, 0, Left, Top, Width, Height, 0);
end;

begin
  try
    Writeln(MoveNotepad(10, 10, 200, 300));
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.
Daniel W.
Ich bin nicht zurück, ich tue nur so
  Mit Zitat antworten Zitat