Easy to use Delphi to achieve background playback

Source: Internet
Author: User

Tmediaplay has a display property, which is the Twincontrol type, and Twincontrol is the first VCL class to have a window handle (Handle). Tmediaplay is playing with the window area that the handle handle.

If we point the handle to the desktop, which Tmediaplay will play on the desktop. Unlike Tcanvas, Twincontrol's handle properties are read-only and the GetHandle method is a static method, which means that we have only two ways of pointing to the desktop

Modify Twincontrol so that handle can be written.

Creates a Twincontrol subclass, hides the Twincontrol.gethandle method, and returns the table handle in the new method.

The second method is better.

TSurpriseDisplay=class(TWinControl)
protected
function GetHandle:HWnd;virtual;
public
property Handle: HWnd read GetHandle;
end;

Where gethandle is defined as virtual, it is easy to inherit. Since the Twincontrol.gethandle method is only hidden here, when this subclass is assigned to Tmediaplay.display, it is coerced into the parent class and its GetHandle method is invoked, which does not achieve our purpose. So I made a little change to tmediaplay, enhance its function, the modified Tmediaplay completely compatible with the original.

Original code:

{setting a TWinControl to display video devices' output}
procedure TMediaPlayer.SetDisplay( Value: TWinControl );
var
AWindowParm: TMCI_Anim_Window_Parms;
begin
if (Value <> nil) and MCIOpened and FHasVideo then
begin
FFlags := mci_Wait or mci_Anim_Window_hWnd;
AWindowParm.Wnd := Longint(Value.Handle);
................
end;

After modification

{setting a TWinControl to display video devices' output}
procedure TMediaPlayer.SetDisplay( Value: TWinControl );
var
AWindowParm: TMCI_Anim_Window_Parms;
wnd:HWND;
begin
if (Value <> nil) and MCIOpened and FHasVideo then
begin
FFlags := mci_Wait or mci_Anim_Window_hWnd;
//************************************************************
if Value is TSurpriseDisplay then
wnd:=TSurpriseDisplay(Value).Handle
else
wnd:=Value.Handle;
//************************************************************
AWindowParm.Wnd := Longint(wnd);
............
end;

Although this modification is not graceful, it can guarantee maximum compatibility. Another way to modify the method is to change the tmediaplay.display to Tsurprisedisplay type, so as to ensure safety.

How to use:

Use desktop playback

Just return the desktop handle in GetHandle and assign it to display.

function TSurpriseDisplay.GetHandle:HWnd;
begin
Result:=GetDesktopWindow;
end;
mp.Display:=TSurpriseDisplay.Create(self);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.