Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I provide an inner private non visual window (handle) a non visual VCL component?

This is a follow-up question.

My prior questions:

  • How to paste a custom format clipboard data into a TMemo?
  • Joining the Clipboard Chain Best practices?

My problem:

TComponent doesn't own a window handle like TWinControl. I don't want to rely on an external one.

This is a snippet of my custom component

type
  TMyClipBoardListener = class(TComponent)
  private
    FInnerWindowHandle: HWnd;
    FNextHWnd:  HWnd;
    //...
  protected
    procedure Loaded; override;
    procedure WndProc(var Msg: TMessage); // <<< This is my wouldbe Window to handle messages
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    // ...
  published
    // ...
  end;

Implementation excerpt of my custom component

constructor TMyClipBoardListener .Create(AOwner: TComponent);
begin
  inherited;
  //
  FInnerWindowHandle := ...; // <<< What to do here ? Should I pass it to a function/procedure I missed?
end;

destructor TMyClipBoardListener .Destroy;
begin
  if not(csDesigning in ComponentState) then
  begin
    ChangeClipboardChain(FInnerWindowHandle, FNextHWnd);
  end;
  //
  // <<< Are there some cleaning code related to FInnerWindowHandle to implement here or elsewhereCreates a window that implements a specified window procedure. ?
  //
  inherited;
end;

procedure TMyClipBoardListener.Loaded;
begin
  inherited;
  //
  if not(csDesigning in ComponentState) then
  begin
    FNextHWnd:= SetClipboardViewer(FInnerWindowHandle);
  end;
end;

procedure TMyClipBoardListener.WndProc(var Msg: TMessage);
begin
  with Msg do
  begin
    // Message to handle : WM_CHANGECBCHAIN and WM_DRAWCLIPBOARD
    // ... 
    else
      Result := DefWindowProc(FInnerWindowHandle, Msg, WParam, LParam); // <<< Is this the right way to do default handling properly?
  end;
end;

My question:

How can I get for my custom component an inner window implementing the embedded window procedure?

like image 412
menjaraz Avatar asked Dec 16 '22 05:12

menjaraz


1 Answers

Call AllocateHwnd from the Classes unit (not Forms).

FInnerWindowHandle := AllocateHwnd(WndProc);

When you're finished, call DeallocateHwnd.

like image 166
Rob Kennedy Avatar answered Dec 28 '22 09:12

Rob Kennedy