Entwickler-Ecke

Windows API - Handle wird nich erkannt


supersass1 - Di 18.09.07 11:59
Titel: Handle wird nich erkannt
Hallo,
ich hab ein Programm welches aus 2 Teilen besteht.
Zuerst gibt der Benutzer die .exe Datei an, dann wird der Handle dazu erkannt.

Des weiteren klick der Benutzer auf einen Button und das Window zur .exe Datei soll minimiert werden. Doch leider wird immer nur das Programm selber minimiert:


Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, TLHelp32;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Edit1: TEdit;
    Label1: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;
  WindowHandle: THandle;
    h:HWnd;

implementation

{$R *.DFM}



function GetWindowThreadProcessId(hWnd: HWND; var dwProcessId: DWORD): DWORD;
stdcall; external 'user32.dll' name 'GetWindowThreadProcessId';

function MyEnumWindowProc(AHandle: THandle; LParam: LongWord): boolean;
stdcall;
var
  ProcessID: THandle;
begin
  ProcessID := 0;
  GetWindowThreadProcessID(AHandle, ProcessID);
  Result := not (ProcessID = LParam);
  if not Result then
    WindowHandle := AHandle;
end;

function GetWindowHandleByExeName(const AExeName: string): THandle;
var
  SnapShot: THandle;
  p: TProcessEntry32;
  ProcessHandle: THandle;
begin
  Result := 0;
  WindowHandle := 0;
  ProcessHandle := 0;
  p.dwSize := SizeOf(p);
  SnapShot := CreateToolhelp32Snapshot(TH32CS_SnapProcess, 0);
  try
    if Process32First(SnapShot, p) then
      repeat
      if AnsiLowerCase(AExeName) = AnsiLowerCase(p.szExeFile) then
        ProcessHandle := p.th32ProcessID;
      until (ProcessHandle <> 0) or not Process32Next(SnapShot, p);
    EnumWindows(@MyEnumWindowProc, ProcessHandle);
    Result := WindowHandle;
  finally
    CloseHandle(SnapShot);
  end;
end;


procedure TForm1.Button2Click(Sender: TObject);
var
m_exe:string;
begin
m_exe := Edit1.Text;
GetWindowHandleByExeName(m_exe);

end;


procedure TForm1.Button1Click(Sender: TObject);

begin
  h:=WindowHandle;
  postmessage(h,WM_SYSCOMMAND,SC_MINIMIZE,0);

//  SC_CLOSE        =       Fenster schließen
//(beim Hauptfenster wird die Anwendung beendet)
//SC_MINIMIZE     =       Fenster minimieren
//SC_MAXIMIZE     =       Fenster maximieren
//SC_RESTORE      =       Fenster wiederherstellen

end;

end.


Im DAteianhang habe ich noch ein Bild wie es ausschaut.

Vielleicht kann mir ja jemand helfen, danke. =)


Marc. - Di 18.09.07 14:59

Zitat:

Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
procedure TForm1.Button2Click(Sender: TObject);
var
m_exe:string;
begin
m_exe := Edit1.Text;
GetWindowHandleByExeName(m_exe);

end;


Was bringt dir denn das? :roll: Da fehlt wohl was ... ? ;)


supersass1 - Di 18.09.07 15:15

ja danke für deine antwort ;)

ich habs schon selber gesehn:


Delphi-Quelltext
1:
PostMessage(GetWindowHandleByExeName(Edit1.Text), WM_SYSCOMMAND, SC_CLOSE, 0);                    


und gut ist ;)


Marc. - Di 18.09.07 15:24

Das ist schön. :)
Und ein herzliches Willkommen im Forum! :welcome:

grüße,
marc


supersass1 - Di 18.09.07 20:45

hehe dankescön =)

ich hab ein neues Problem:
ich möchte, den PArameter
SC_MINIMIZE

in


Delphi-Quelltext
1:
PostMessage(GetWindowHandleByExeName(Edit1.Text), WM_SYSCOMMAND,SC_MINIMIZE,0);                    


gerne per Combobox steuern, sprich ich wähle in der Box aus zwischen Minimize, Close etc. Dies wird im label1.Caption gespeichert, nun geben ich folgendes ein:


Delphi-Quelltext
1:
PostMessage(GetWindowHandleByExeName(Edit1.Text), WM_SYSCOMMAND, Label1.Caption, 0);                    


Doch leider kommt folgende Fehlermeldung:

Quelltext
1:
2:
[Hinweis] Unit1.pas(61): Auf 'GetWindowHandleByExeName' zugewiesener Wert wird niemals benutzt
[Fehler] Unit1.pas(126): Inkompatible Typen: 'Integer' und 'TCaption'



Viele liebe grüße :)


Wolle92 - Di 18.09.07 20:58

Sc_MINIMIZE, SC_CLOSE usw. sind Konstanten die für Integerwerte stehen...


supersass1 - Di 18.09.07 21:20

user profile iconWolle92 hat folgendes geschrieben:
Sc_MINIMIZE, SC_CLOSE usw. sind Konstanten die für Integerwerte stehen...


Das löst mein Problem leider nicht, wie würdest du es lösen?


supersass1 - Mi 19.09.07 13:41

hat sich erledigt:


Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
begin
Flag := 0;
case ComboBox1.ItemIndex of
  0: Flag := SC_CLOSE;
  1: Flag := SC_MINIMIZE;
  2: Flag := SC_MAXIMIZE;
  3: Flag := SC_RESTORE;
  end;