4
votes

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!

1
Do you really need to embed Media Player? Wouldn't be better to craft your own based on DirectShow?TLama
@TLama, Thanks. The WMP works quite well for my needs. For now I'm stuck only with this issue. I'm looking for a quick solution to preview media only from the internet (basically only MP4 format). IMO, switching to DirectShow (DSPack?) at this point will open a whole new wasp nest to deal with. I might be wrong...zig
@TLama, I just tried DSPack. as I thought it has its own issues. like for example being able to play a local file but not play the same file from the internet. also when resizing a TVideoWindow the video stops playing with no reason, and so on.zig
I am unable to reproduce the problem in Delphi 10.3 on Windows 10. But you seem to use Delphi 7 on Windows 7.Andreas Rejbrand
@AndreasRejbrand, Thanks. Your comment helped me big time. I compared TOleControl.SetBounds (OleCtrls) from Delphi 2009 with mine. and there was a difference in code. the if FOleInplaceObject <> nil then... part was missing. after applying that to a local copy of OleCtrls everything works fine!zig

1 Answers

5
votes

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.