Impressum DatenschutzerklärungDiese Seite per HTTPS ansehen.
  • Artikel ist eingeordnet in:
  • Delphi


Interaktion mit fremden Fenstern geschrieben am 20.08.2006

Diese unit sammelt einige Funktionen zum Manipulieren von fremden Fenster in Delphi.

Delphi/Pascalunit my_winhandels; interface uses Messages,Windows,Dialogs,SysUtils,TlHelp32; //starthandle Handle der Form //Übergabe: Fenstertitel des zu bearbeitenden Programmes, Handel das als Ausgang dient (Handel vom Formular) //wenn starthandle=0 wird vom aktiven Fenster beginnend gesucht function my_wh_getWinhandle(titel:string;starthandle:hWnd):hWnd; //suche nach genau dem Fenstertitel oder der erste in dem der String vorkommt var my_wh_strickt:boolean=true; //Übergabe: Fenstertitel des zu bearbeitenden Programmes und Starthandel ab dem es zu suchen gild function my_wh_moveWin(x,y:integer;wintitel:string;starthandle:hWnd): boolean; function my_wh_moveWinTo(x,y:integer;wintitel:string;starthandle:hWnd): boolean; function my_wh_resizeWin(b,h:integer;wintitel:string;starthandle:hWnd): boolean; function my_wh_resizeWinTo(b,h:integer;wintitel:string;starthandle:hWnd): boolean; function my_wh_sendKeytoapp(key:integer;wintitel:string;starthandle:hWnd): boolean; function my_wh_sendStringtoapp(s:string;wintitel:string;starthandle:hWnd): boolean; //Gleiche Funktion, nur mit übergabe des Handels function my_wh_moveWinH(x,y:integer;handle:hWnd): boolean; function my_wh_moveWinToH(x,y:integer;handle:hWnd): boolean; function my_wh_resizeWinH(b,h:integer;handle:hWnd): boolean; function my_wh_resizeWinToH(b,h:integer;handle:hWnd): boolean; function my_wh_sendKeytoappH(key:integer;handle:hWnd): boolean; function my_wh_sendStringtoappH(s:string;handle:hWnd): boolean; //Schließe Programm, Übergabe Fenstertitel procedure my_wh_closeProgs(titel:string;starthandle:hWnd); procedure my_wh_startProgs(pfadundexe:string); function my_wh_IsExeRunning(const AExeName: string): boolean; //FEnster eines Programmes in ein Objekt (Fenster/Panel) einfangen, freigeben function my_wh_catchWindow(Winhandle:hWnd;zielHandle:thandle;maximize:boolean): boolean; procedure my_wh_uncatchWindow(Winhandle:hWnd); //Fenster verstecken/anzeigen, Übergabe Handle procedure my_wh_hideProg(win:THandle); procedure my_wh_showProg(win:THandle); function my_wh_getInfos(win:THandle):TProcessEntry32; function my_wh_setpriority(win:THandle;pri:DWORD):boolean; function my_wh_getpriority(win:THandle):DWORD; implementation //------------------------------------------------------------------------------ { Idle : setpriorityclass(GetCurrentProcess(), IDLE_PRIORITY_CLASS); |ccomd10| Normal : setpriorityclass(GetCurrentProcess(), NORMAL_PRIORITY_CLASS);|ccomd11| Hoch : setpriorityclass(GetCurrentProcess(), HIGH_PRIORITY_CLASS); |ccomd12| Realtime : setpriorityclass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS);|ccomd13| getpriorityclass(HANDLE hProcess ) BELOW_NORMAL_PRIORITY_CLASS = $4000; ABOVE_NORMAL_PRIORITY_CLASS= $8000; Echtzeit REAL_TIME_PRIORITY_CLASS Hoch HIGH_PRIORITY_CLASS Höher als normal ABOVE_NORMAL_PRIORITY_CLASS Normal NORMAL_PRIORITY_CLASS Niedriger als normal BELOW_NORMAL_PRIORITY_CLASS Leerlauf IDLE_PRIORITY_CLASS GetWindowThreadProcessId } //------------------------------------------------------------------------------ function my_wh_setpriority(win:THandle;pri:DWORD):boolean; var ProcID :DWord; begin // GetWindowThreadProcessID(win, @ProcID); // result:=setpriorityclass(ProcID, pri); end; function my_wh_getpriority(win:THandle):DWORD; var ProcID :DWord; SSHandle :THandle; Continue :boolean; ProcEntry:TProcessEntry32; re:DWORD; wcthreadID:DWORD; begin // ProcEntry:=my_wh_getInfos(win); // re:=ProcEntry.pcPriClassBase; end; function my_wh_getInfos(win:THandle):TProcessEntry32; var ProcID :DWord; SSHandle :THandle; Continue :boolean; ProcEntry:TProcessEntry32; begin GetWindowThreadProcessID(win, @ProcID); SSHandle := CreateToolhelp32Snapshot(TH32CS_SnapProcess, 0); ProcEntry.dwSize := Sizeof(ProcEntry); Continue := Process32First(SSHandle, ProcEntry); while Continue do begin // gesuchter Prozess (ProcID) in der Liste aller Prozesse gefunden? if ProcEntry.th32ProcessID = ProcID then begin Result :=ProcEntry;//.szExeFile) // pcPriClassBase 4=niedrig, // 6=niedriger als normal, // 8=normal, // 10=höher als normal, // 13=hoch, // 24=echtzeit end; Continue := Process32Next(SSHandle, ProcEntry); end; CloseHandle(SSHandle); end; //------------------------------------------------------------------------------ //windows.GetClientRect(mhandle,temprec); //l,t,b,h procedure my_wh_hideProg(win:THandle); begin if win>0 then ShowWindow(win,SW_MINIMIZE); //sonst bleibt das GetClientRect erhalten if win>0 then ShowWindow(win, SW_HIDE); //und cursor in Taskliste entfernen end; procedure my_wh_showProg(win:THandle); begin if win>0 then ShowWindow(win, SW_NORMAL); end; procedure my_wh_closeProgs(titel:string;starthandle:hWnd); begin //suche Programm nach: PostMessage(my_wh_getWinhandle(titel,starthandle), WM_CLOSE, 0, 0); end; procedure my_wh_startProgs(pfadundexe:string); begin winexec(PChar(pfadundexe), SW_SHOWNORMAL); end; function my_wh_IsExeRunning(const AExeName: string): boolean; var h: THandle; p: TProcessEntry32; begin // Result := False; p.dwSize := Sizeof(p); h := CreateToolHelp32Snapshot(TH32CS_SnapProcess, 0); try Process32First(h, p); repeat Result := AnsiUpperCase(AExeName) = AnsiUpperCase(p.szExeFile); until Result or (not Process32Next(h, p)); finally CloseHandle(h); end; end; //------------------------------------------------------------------------------ function my_wh_catchWindow(Winhandle:hWnd;zielHandle:thandle;maximize:boolean): boolean; begin Windows.SetParent(Winhandle, zielHandle);//http://www.delphipraxis.net/topic57134_fremde+anwendung+in+meiner+anwendung+laufen+lassen.html result:=my_wh_moveWinToH(0,0,Winhandle); if maximize then ShowWindow(Winhandle, SW_MAXIMIZE); end; { 'Shell_TrayWnd' Taskleiste 'ProgMan' Desktop 'DISPLAY' Windows 'Program Manager' Desktop Ikons ShowWindow(FindWindow(nil,'Program Manager'),SW_HIDE); |ccomd35| ShowWindow(FindWindow(nil,'Program Manager'),SW_SHOW); |ccomd36| } procedure my_wh_uncatchWindow(Winhandle:hWnd); begin Windows.SetParent(Winhandle, FindWindow('DISPLAY', nil)); end; function GetWindowRect(mhandle:thandle):trect; begin windows.GetClientRect(mhandle,result); end; //suche Fenster nach Titelnamen function my_wh_getWinhandle(titel:string;starthandle:hWnd):hWnd; var tempString: array[0..255] of Char; isfound:boolean; helphandle:hWnd; s2:string; t:integer; begin tempString:=''; isfound:=false; if starthandle=0 then starthandle:=windows.GetActiveWindow; if starthandle=0 then starthandle:=FindWindow(nil,PChar(titel)); if starthandle=0 then starthandle:=windows.GetDesktopWindow; helphandle:=starthandle; while helphandle>0 do begin windows.GetWindowText(helphandle, tempString, 255); s2:=''; for t:=0 to length(tempString)-1 do s2:=s2+tempString[t]; if length(s2)>0 then if my_wh_strickt then begin if(titel=s2)then isfound:=true; end else begin if(pos(titel,s2)>0)then isfound:=true; end; if isfound then break; helphandle:= GetNextWindow(helphandle,GW_HWNDNEXT); //erst in eine Richtung end; if isfound=false then //andere Richtung, wenn noch nicht gefunden begin helphandle:=starthandle; while helphandle>0 do begin windows.GetWindowText(helphandle, tempString, 255); s2:=''; for t:=0 to length(tempString)-1 do s2:=s2+tempString[t]; if length(s2)>0 then if my_wh_strickt then begin if(titel=s2)then isfound:=true; end else begin if(pos(titel,s2)>0)then isfound:=true; end; if isfound then break; helphandle:= GetNextWindow(helphandle,GW_HWNDPREV); //dann in die andere end; end; if isfound then result:=helphandle else result:=0; end; //----------------------------------------------------------------------------- //Funktionen suchen entsprechendes Fenster und machen dann Aktion function my_wh_moveWin(x,y:integer;wintitel:string;starthandle:hWnd): boolean; var mhandle:hWnd; begin mhandle:= my_wh_getWinhandle(wintitel,starthandle); result:=my_wh_moveWinH(x,y,mhandle); end; function my_wh_moveWinTo(x,y:integer;wintitel:string;starthandle:hWnd): boolean; var mhandle:hWnd; begin mhandle:= my_wh_getWinhandle(wintitel,starthandle); result:=my_wh_moveWinToH(x,y,mhandle); end; function my_wh_resizeWin(b,h:integer;wintitel:string;starthandle:hWnd): boolean; var mhandle:hWnd; begin mhandle:=my_wh_getWinhandle(wintitel,starthandle); result:=my_wh_resizeWinH(b,h,mhandle); end; function my_wh_resizeWinTo(b,h:integer;wintitel:string;starthandle:hWnd): boolean; var mhandle:hWnd; begin mhandle:=my_wh_getWinhandle(wintitel,starthandle); result:=my_wh_resizeWinToH(b,h,mhandle); end; function my_wh_sendKeytoapp(key:integer;wintitel:string;starthandle:hWnd): boolean; var mhandle:hWnd; begin mhandle:=my_wh_getWinhandle(wintitel,starthandle); result:=my_wh_sendKeytoappH(key,mhandle); end; function my_wh_sendStringtoapp(s:string;wintitel:string;starthandle:hWnd): boolean; var mhandle:hWnd; begin mhandle:=my_wh_getWinhandle(wintitel,starthandle); result:=my_wh_sendStringtoappH(s,mhandle); end; //--------------------------------------------------------------------------// //Führen Aktion an übergebenes Handle aus function my_wh_moveWinH(x,y:integer;handle:hWnd): boolean; var r:trect; begin result:=false; if handle>0 then begin if windows.GetWindowRect(handle,r) then result:=windows.MoveWindow(handle,r.Left+x,r.Top+y,r.Right-r.Left,r.Bottom-r.Top,true); end; end; function my_wh_moveWinToH(x,y:integer;handle:hWnd): boolean; var r:trect; b,h:integer; begin result:=false; if handle>0 then begin if windows.GetWindowRect(handle,r) then // in r aktuelle werte begin b:=r.Right-r.Left; h:=r.Bottom-r.Top; result:=windows.MoveWindow(handle,x,y,b,h,true); end; end; end; function my_wh_resizeWinH(b,h:integer;handle:hWnd): boolean; var r:trect; begin result:=false; if handle>0 then begin if windows.GetWindowRect(handle,r) then // result:=windows.MoveWindow(handle,r.Left,r.Top,r.Right-r.Left+b,r.Bottom-r.Top+h,true); end; end; function my_wh_resizeWinToH(b,h:integer;handle:hWnd): boolean; var r:trect; begin result:=false; if handle>0 then begin if windows.GetWindowRect(handle,r) then // result:=windows.MoveWindow(handle,r.Left,r.Top,b,h,true); end; end; // function my_wh_sendKeytoappH(key:integer;handle:hWnd): boolean; begin result:=false; if handle>0 then begin if PostMessage(handle, WM_KEYDOWN, key, 0) then //wenn handle=HWND_BROADCAST dann an aktivers Fenster result:=PostMessage(handle, WM_KEYUP , key, 0); //0=Key nicht wiederholen end; end; function my_wh_sendStringtoappH(s:string;handle:hWnd): boolean; var t:integer; begin result:=false; if handle>0 then begin for t := 0 to Length(s) do begin result:=SendMessage(handle, WM_CHAR, Ord( s[t] ), 0 )>0; if result=false then break; end; end; end; end.

Kommentare:


#1 Revan schrieb am 21.10.2010 um 15:50
jo danke is sehr nützlich!



Inhalt zur freien Verwendung gibs es beim Thema Downloads.
nach oben springen
mehr auf: Instagram, github, thingiverse