We help IT Professionals succeed at work.
Get Started

How get handle of window (not child windows) owned by the specified window

jlvill
jlvill asked
on
371 Views
Last Modified: 2010-04-05

Hi experts,
I'm trying to get  handle of window (not child windows) owned by the specified window. More precisely I'm trying to get handle of #32770 (WAB Property of OE). I'm succeed with the code below but probably it's can be done more efficiently. Somebody can help me ?

Thanks for all,

Jean-Louis


===========================
unit Main;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
var
  Form1: TForm1;
  szWABBrowseView   : PChar = 'WABBrowseView'#0;
  sz32770           : PChar = '#32770'#0;
  szSysTabControl32 : PChar = 'SysTabControl32'#0;
implementation

{$R *.dfm}
function EnumProc(wnd: HWND; Lines: TStrings): BOOL; stdcall;
var
  buf, Caption: array[0..255] of char;
  idStr, idHandle,idName:string;
begin
  Result := True;
  GetClassName(wnd, buf, SizeOf(buf) - 1);
  SendMessage(wnd, WM_GETTEXT, 256, Integer(@Caption));
  idName :='';
  idName :=trim(Format('%s,',[buf]));
  idName :=concat(idName,StringOfChar(' ', 20-length(idName)));
  idStr  :='';
  idStr  :=trim(Format('ID[%d],',[GetDlgCtrlID(wnd)]));
  idStr  :=concat(idStr,StringOfChar(' ', 15-length(idStr)));
  idHandle :='';
  idHandle :=trim(Format('Handle{%d}',[wnd]));
  idHandle :=concat(idHandle,StringOfChar(' ', 20-length(concat(idHandle,idStr))));
  Form1.Memo1.Lines.Add(concat(idName,' ',idStr,' ',idHandle));
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  VhWABBrowseView :THandle;
  Vh32770         :THandle;
  VhSysTabControl32 : THandle;
  item : TTCItem;
  ViRowCount: integer;
  VhWnd : Hwnd;
  Hex : string;
  VaCharClassName, Caption: array[0..255] of char;
  idHandle,VsClassName:string;
  Vi,ViRow     : Integer;
  VbFini : boolean;
  VsNewTab:String;
begin
  Hex:='0';
  VhSysTabControl32 := 0;
  VhWABBrowseView := FindWindow(szWABBrowseView,nil);
  VhWnd:=0;
  if  VhWABBrowseView <> 0
    then VhWnd:=GetWindow(VhWABBrowseView,GW_HWNDNEXT);
  if VhWnd <> 0 then begin
    Vi:= 0;
    VbFini := false;
//
// get handle of #32770 (WAB Property of OE).
//
    while (Vi < 100) and not VbFini do begin
      VhWnd:=GetWindow(VhWnd,GW_HWNDPREV);
      GetClassName(VhWnd, VaCharClassName, SizeOf(VaCharClassName) - 1);
      SendMessage(VhWnd, WM_GETTEXT, 256, Integer(@Caption));
      VsClassName :='';
      VsClassName :=trim(Format('%s',[VaCharClassName]));
      (*idStr  :='';
      idStr  :=trim(Format('ID[%d],',[GetDlgCtrlID(VhWnd)]));*)
      idHandle :='';
      idHandle :=trim(Format('Handle{%s}',['$'+inttohex(VhWnd,8)]));
      if pos(sz32770,VsClassName)<> 0 then begin
        Form1.Memo1.Lines.Add(concat(VsClassName{,' ',idStr},' ',idHandle));
          Hex := '$'+inttohex(VhWnd,8);
          Vh32770 := strtoint(Hex);
          VhSysTabControl32:= FindWindowEx(Vh32770,0,szSysTabControl32,nil);
          if VhSysTabControl32 <> 0
            then VbFini := True
            else Vh32770:= 0;
      end;
      Inc(Vi);
    end;
  end;
  Hex := '$'+inttohex(VhSysTabControl32,8);
  ZeroMemory(@item, sizeof(item));
  item.cchTextMax := length(VsNewTab);
  VsNewTab     := 'Const';
  ZeroMemory(@item, sizeof(item));
  item.pszText := 'test';//PChar(VsNewTab);
  item.mask    := {TCIF_IMAGE or}  TCIF_TEXT;
  ViRowCount:=sendmessage(VhSysTabControl32,TCM_GETITEMCOUNT,0,0);
  ViRow:=sendmessage(VhSysTabControl32,TCM_INSERTITEM ,ViRowCount,integer(@item));
  sendmessage(VhSysTabControl32,TCM_SetCurSel, -1,0);
  Memo1.Lines.Add('Insertion à ...'+Inttostr(ViRow));
end;
end.
Comment
Watch Question
Software Developer
Commented:
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE