Einzelnen Beitrag anzeigen

svko

Registriert seit: 19. Mai 2008
5 Beiträge
 
#1

Findwindow Handle immer Null

  Alt 26. Jul 2008, 09:43
Ich fülle ein Popupmenü zu Laufzeit mit Fenstertiteln,wenn ich auf einen Eintrag gehe,soll
das Fenster versteckt werden. An der Stelle "wnd := FindWindowByTitle(TMenuItem(Sender).Caption);" erhalte ich immer Null.

Danke für die Antworten.

Delphi-Quellcode:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, antTaskbarIcon, ComCtrls, Menus, StdCtrls;

type
  Tmanager = class(TForm)
    tb: TantTaskbarIcon;
    popup: TPopupMenu;
    beenden1: TMenuItem;
    N11: TMenuItem;
    Label1: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure tbClick(Sender: TObject);
    procedure beenden1Click(Sender: TObject);
    procedure popupPopup(Sender: TObject);
  private
    { Private-Deklarationen }
   procedure MenuItem_OnClick(Sender: TObject);
  public
    { Public-Deklarationen }
  end;

var
  manager: Tmanager;

implementation

{$R *.dfm}

function GetText(Wnd: HWND): string;

var
  textlength: Integer;
  Text: PChar;
begin
  textlength := SendMessage(Wnd, WM_GETTEXTLENGTH, 0, 0);
  if textlength = 0 then Result := ''
  else
  begin
    GetMem(Text, textlength + 1);
    SendMessage(Wnd, WM_GETTEXT, textlength + 1, Integer(Text));
    Result := Text;
    FreeMem(Text);
  end;
end;



function EnumWindowsProc(Wnd: HWND; lParam: lParam): BOOL; stdcall;
var
i:integer;
amenuitem:tmenuitem;
begin
  Result := True;
  if (IsWindowVisible(Wnd) or IsIconic(wnd)) and
    ((GetWindowLong(Wnd, GWL_HWNDPARENT) = 0) or
    (GetWindowLong(Wnd, GWL_HWNDPARENT) = GetDesktopWindow)) and
    (GetWindowLong(Wnd, GWL_EXSTYLE) and WS_EX_TOOLWINDOW = 0) and (IntToStr(Wnd) <> '' )then
     begin

     AMenuItem := TMenuItem.Create(manager.popup);
AMenuItem.Tag := i;
AMenuItem.Caption := GetText(Wnd);
AMenuItem.OnClick := manager.MenuItem_OnClick;
manager.popup.Items.Insert(0,AMenuItem);
     end;
end;

procedure Tmanager.FormCreate(Sender: TObject);
begin
tb.visible:=true;
end;

procedure Tmanager.tbClick(Sender: TObject);
begin
manager.Show;
end;

procedure Tmanager.beenden1Click(Sender: TObject);
begin
close;
end;

 function FindWindowByTitle(WindowTitle: string): Hwnd;
var
  NextHandle: Hwnd;
  NextTitle: array[0..260] of char;
begin

  NextHandle := GetWindow(Application.Handle, GW_HWNDFIRST);
  while NextHandle > 0 do
  begin

    GetWindowText(NextHandle, NextTitle, 255);
    if Pos(WindowTitle, StrPas(NextTitle)) <> 0 then
    begin
      Result := NextHandle;
      Exit;
    end
    else
      // Get the next window
      NextHandle := GetWindow(NextHandle, GW_HWNDNEXT);
  end;
  Result := 0;
end;

procedure Tmanager.MenuItem_OnClick(Sender: TObject);
var
  wnd : HWND;
begin

   label1.Caption:=(TMenuItem(Sender).Caption);
  wnd := FindWindowByTitle(TMenuItem(Sender).Caption);

ShowWindow(wnd, SW_HIDE);
end;

procedure Tmanager.popupPopup(Sender: TObject);
var
  Param: Longint;
begin
  EnumWindows(@EnumWindowsProc, Param);
end;

end.
  Mit Zitat antworten Zitat