Search code examples
delphidelphi-7windows-media-playerwmp

How to align Windows Media Player control to fit parent window?


I Have a Windows Media Player ActiveX control. I want it to be aligned to its parent TPanel.

The problem is that no matter what I try the WMP control is always set to its initial size without the possibility to resize it.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, XpMan, ExtCtrls, WMPLib_TLB;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
  public
    Panel: TPanel;
    MP: TWindowsMediaPlayer;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  Width := 450;
  Height := 260;

  Panel := TPanel.Create(Self);
  Panel.Parent := Self;
  Panel.Align := alClient;


  MP := TWindowsMediaPlayer.Create(Self);
  // MP.stretchToFit := True;
  MP.Parent := Panel;
  MP.Align := alClient;
  MP.URL := 'https://www.w3schools.com/html/mov_bbb.mp4';
end;

When you open the form the WMP control looks fine:

enter image description here

But when you resize the form, the WMP control wont align to the parent Panel:

enter image description here

This is actually the effect I see when trying to enlarge:

enter image description here

What can I do to make the WMP control behave as expected?

I have tried many stupid things like:

procedure TForm1.FormResize(Sender: TObject);
begin
  if not Assigned(MP) then Exit;
  MP.Width := Panel.ClientWidth;
  MP.Height := Panel.ClientHeight;
  Panel.Realign;
end;

But nothing works!


Solution

  • This is a bug in Delphi 7 TOleControl.SetBounds in OleCtrls. it was fixed in newer versions.

    procedure TOleControl.SetBounds(ALeft, ATop, AWidth, AHeight: Integer);
    var
      LRect: TRect;
    begin
      if ((AWidth <> Width) and (Width > 0)) or ((AHeight <> Height) and (Height > 0)) then
      begin
        if (FMiscStatus and OLEMISC_INVISIBLEATRUNTIME <> 0) or
          ((FOleObject.SetExtent(DVASPECT_CONTENT, Point(
            MulDiv(AWidth, 2540, Screen.PixelsPerInch),
            MulDiv(AHeight, 2540, Screen.PixelsPerInch))) <> S_OK)) then
        begin
          AWidth := Width;
          AHeight := Height;
        end;
        { fix start }
        if FOleInplaceObject <> nil then 
        begin
          LRect := Rect(Left, Top, AWidth, AHeight);
          FOleInplaceObject.SetObjectRects(LRect, LRect);
        end;
        { fix end }
      end;
      inherited SetBounds(ALeft, ATop, AWidth, AHeight);
    end;
    

    After applying that to a local copy of OleCtrls everything works fine.