DialogHandle (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

The code represents the replacement of the window procedure for the Delphi encapsulation of the Windows search-and-replace common dialog box. Click Button2 to activate an open dialog and send WM_NCACTIVATE to FindReplaceWndProc.

Code

function FindReplaceWndProc(Wnd: HWND; Msg, WParam, LParam: Longint): Longint; stdcall;

  function CallDefWndProc: Longint;
  begin
    Result := CallWindowProc(Pointer(GetProp(Wnd,
      MakeIntAtom(WndProcPtrAtom))), Wnd, Msg, WParam, LParam);
  end;

begin
  case Msg of
    WM_DESTROY:
      if Application.DialogHandle = Wnd then Application.DialogHandle := 0;
    WM_NCACTIVATE:
      if WParam <> 0 then
      begin
        if Application.DialogHandle = 0 then Application.DialogHandle := Wnd;
      end else
      begin
        if Application.DialogHandle = Wnd then Application.DialogHandle := 0;
      end;
    WM_NCDESTROY:
      begin
        Result := CallDefWndProc;
        RemoveProp(Wnd, MakeIntAtom(WndProcPtrAtom));
        Exit;
      end;
   end;
   Result := CallDefWndProc;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ReplaceDialog1.Execute;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  OpenDialog1.Execute;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  AtomText: array[0..31] of Char;
begin
  WndProcPtrAtom := GlobalAddAtom(StrFmt(AtomText,
    'WndProcPtr%.8X%.8X', [HInstance, GetCurrentThreadID]));
  SetWindowLong(Application.Handle, GWL_WNDPROC, Longint(@FindReplaceWndProc));;
end;

Uses