Jump to content
Marco Cantu

VCL Support for Per Monitor v2 and GetSystemMetrics Coming in 10.3

Recommended Posts

Quote

This is compatible in terms of parameters with the Windows API of the same name, so an existing call to the API in one of your components gets redirected to the method, which in turn calls GetSystemMetricsForWindow passing the control's parent handle as parameter.

If I understand properly, to get a form's handle that a component belongs to, you need to use its Owner property, not the Parent property.

 

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  if Button1.Parent.Handle = Panel1.Handle then
    ShowMessage('My parent is Panel1');
  if (Button1.Owner as TWinControl).Handle = Form1.Handle then
    ShowMessage('My owner is Form1');
end;

Good addition BTW!

Edited by Kryvich

Share this post


Link to post

The Owner is not necessarily the form. Sometimes the owner is a TFrame descendant or may even be nil.

 

Iterating the Parent until a TCustomForm is found is usually the way to get the required form instance. Perhaps iterating until Parent is nil is another valid approach. That would also cover the case where one form is parented into another one.

  • Like 5

Share this post


Link to post
On 11/20/2018 at 11:49 AM, Kryvich said:

If I understand properly, to get a form's handle that a component belongs to, you need to use its Owner property, not the Parent property.

Actually I was imprecise. It doesn't use the parent handle, but walks over the parent hierarchy up to the containing form

  • Like 1

Share this post


Link to post
On 11/20/2018 at 3:36 AM, Uwe Raabe said:

Iterating the Parent until a TCustomForm is found is usually the way to get the required form instance. Perhaps iterating until Parent is nil is another valid approach. That would also cover the case where one form is parented into another one.

The VCL has a GetParentForm() function in the Vcl.Forms unit for this very purpose.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×