Recent

Author Topic: Matching video to form  (Read 1020 times)

Pe3s

  • Hero Member
  • *****
  • Posts: 638
Matching video to form
« on: December 07, 2025, 07:41:42 pm »
I wrote a procedure that adjusts the form dimensions to the video dimensions, but I got stuck in one place, namely how to limit the form dimensions so that it does not go beyond the Windows taskbar?

Code: Pascal  [Select][+][-]
  1. procedure TForm1.ResizeFormToVideo;
  2. var
  3.   VW, VH: Integer;
  4.   WA: TRect;
  5.   MaxW, MaxH: Integer;
  6.   BW, BH: Integer;
  7.   AR: Double;
  8.   FW, FH: Integer;
  9.   AvH: Integer;
  10.   SL: Integer;
  11. begin
  12.  
  13.   VW := MPVPlayer1.GetVideoWidth;
  14.   VH := MPVPlayer1.GetVideoHeight;
  15.   if (VW <= 0) or (VH <= 0) then Exit;
  16.  
  17.   AR := VW / VH;
  18.  
  19.  
  20.   SystemParametersInfo(SPI_GETWORKAREA, 0, @WA, 0);
  21.   MaxW := WA.Right - WA.Left;
  22.   MaxH := WA.Bottom - WA.Top;
  23.  
  24.  
  25.   BW := Width - ClientWidth;
  26.   BH := Height - ClientHeight;
  27.  
  28.  
  29.   FH := MaxH;
  30.   AvH := FH - BH;
  31.  
  32.  
  33.   FW := Trunc(AvH * AR) + BW;
  34.  
  35.  
  36.   if FW > MaxW then
  37.   begin
  38.     FW := MaxW;
  39.     AvH := Trunc((FW - BW) / AR);
  40.     FH := Trunc(AvH) + BH;
  41.   end;
  42.  
  43.  
  44.   SL := WA.Left + (MaxW - FW) div 2;
  45.  
  46.  
  47.   SetBounds(SL, WA.Top, FW, FH);
  48. end;                            
  49.  

Thausand

  • Sr. Member
  • ****
  • Posts: 457
Re: Matching video to form
« Reply #1 on: December 07, 2025, 09:48:04 pm »

Pe3s

  • Hero Member
  • *****
  • Posts: 638
Re: Matching video to form
« Reply #2 on: December 09, 2025, 12:24:33 pm »
Is there any other way to calculate the workspace limited to the taskbar?

paweld

  • Hero Member
  • *****
  • Posts: 1561
Re: Matching video to form
« Reply #3 on: December 09, 2025, 12:50:39 pm »
Code: Pascal  [Select][+][-]
  1. Screen.WorkAreaHeight
eg.
Code: Pascal  [Select][+][-]
  1. uses
  2.   LCLIntf, LCLType;
  3.  
  4. procedure TForm1.Button1Click(Sender: TObject);
  5. var
  6.   tp, s: String;
  7. begin
  8.   Memo1.Lines.Clear;
  9.   Memo1.Lines.Add(Format('Titlebar height: %d', [GetSystemMetrics(SM_CXFRAME) + GetSystemMetrics(SM_CYSMCAPTION)]));
  10.   Memo1.Lines.Add(Format('Fullscreen size: %d x %d', [Screen.Width, Screen.Height]));
  11.   Memo1.Lines.Add(Format('Workarea size: %d x %d', [Screen.WorkAreaWidth, Screen.WorkAreaHeight]));
  12.   if (Screen.Width = Screen.WorkAreaWidth) and (Screen.Height = Screen.WorkAreaHeight) then
  13.     Memo1.Lines.Add('Taskbar not visible or autohide enabled')
  14.   else
  15.   begin
  16.     tp := 'bottom';
  17.     if Screen.WorkAreaLeft > 0 then
  18.       tp := 'left'
  19.     else if Screen.WorkAreaTop > 0 then
  20.       tp := 'top'
  21.     else if Screen.WorkAreaWidth < Screen.Width then
  22.       tp := 'rigth';
  23.     if (tp = 'bottom') or (tp = 'top') then
  24.       s := Format('Taskbar height: %d', [Screen.Height - Screen.WorkAreaHeight])
  25.     else
  26.       s := Format('Taskbar width: %d', [Screen.Width - Screen.WorkAreaWidth]);
  27.     Memo1.Lines.Add(s);
  28.     Memo1.Lines.Add(Format('Taskbar position: %s', [tp]));
  29.   end;
  30. end;  
Best regards / Pozdrawiam
paweld

Pe3s

  • Hero Member
  • *****
  • Posts: 638
Re: Matching video to form
« Reply #4 on: December 09, 2025, 09:40:53 pm »
I corrected the code, but the form still goes beyond the task bar.
How can I calculate this? Where am I making a mistake?

Code: Pascal  [Select][+][-]
  1. procedure TForm1.ResizeFormToVideo;
  2. var
  3.   VW, VH: Integer;   // wideo width/height
  4.   AR: Double;        // aspect ratio
  5.   MaxW, MaxH: Integer;
  6.   BW, BH: Integer;   // ramki okna
  7.   FW, FH: Integer;   // final width/height
  8.   AvW, AvH: Integer; // dostępna przestrzeń klienta
  9. begin
  10.   // Pobierz wymiary wideo
  11.   VW := MPVPlayer1.GetVideoWidth;
  12.   VH := MPVPlayer1.GetVideoHeight;
  13.   if (VW <= 0) or (VH <= 0) then Exit;
  14.  
  15.   AR := VW / VH;
  16.  
  17.   // Dostępna przestrzeń ekranu (bez taskbara)
  18.   MaxW := Screen.WorkAreaWidth;
  19.   MaxH := Screen.WorkAreaHeight;
  20.  
  21.   // Oblicz rozmiar ramek okna
  22.   BW := Width - ClientWidth;
  23.   BH := Height - ClientHeight;
  24.  
  25.   // Najpierw dopasuj do wysokości ekranu
  26.   AvH := MaxH - BH;               // dostępna wysokość client area
  27.   AvW := Trunc(AvH * AR);         // szerokość z proporcji
  28.  
  29.   // Gdyby szerokość przekraczała ekran — dopasuj do szerokości
  30.   if AvW + BW > MaxW then
  31.   begin
  32.     AvW := MaxW - BW;
  33.     AvH := Trunc(AvW / AR);
  34.   end;
  35.  
  36.   // Finalny rozmiar okna
  37.   FW := AvW + BW;
  38.   FH := AvH + BH;
  39.  
  40.   // Wyśrodkuj na ekranie roboczym
  41.   SetBounds(
  42.     Screen.WorkAreaLeft + (MaxW - FW) div 2,
  43.     Screen.WorkAreaTop,
  44.     FW,
  45.     FH
  46.   );
  47. end;
  48.  
« Last Edit: December 09, 2025, 09:43:38 pm by Pe3s »

paweld

  • Hero Member
  • *****
  • Posts: 1561
Re: Matching video to form
« Reply #5 on: December 10, 2025, 08:44:46 am »
You've overcomplicated the calculations. . All you need to do is calculate the window reduction scale and apply it to the video size. In the example, I also showed how to get the height of the title bar and frames.
Code: Pascal  [Select][+][-]
  1. procedure TForm1.ResizeFormToVideo;
  2. var
  3.   VW, VH: Integer;   // wideo width/height
  4.   Scale: Double;
  5.   MaxW, MaxH: Integer;
  6.   FW, FH: Integer;   // final width/height
  7. begin
  8.   // Pobierz wymiary wideo
  9.   VW := MPVPlayer1.GetVideoWidth;
  10.   VH := MPVPlayer1.GetVideoHeight;
  11.   if (VW <= 0) or (VH <= 0) then
  12.     Exit;
  13.  
  14.   // Dostępna przestrzeń ekranu (bez taskbara)
  15.   MaxW := Screen.WorkAreaWidth - 2 * GetSystemMetrics(SM_CXFRAME); //szerokość bez ewentualnego taskbara i ramek
  16.   MaxH := Screen.WorkAreaHeight - 2 * GetSystemMetrics(SM_CYFRAME) - GetSystemMetrics(SM_CYSMCAPTION); //wysokość bez ewentualnego taskbara, ramek i paska tytułowego okna
  17.  
  18.   // Finalny rozmiar okna
  19.   Scale := 1;
  20.   FW := VW;
  21.   FH := VH;
  22.   if (VW > MaxW) or (VH > MaxH) then
  23.   begin
  24.     Scale := MaxW / VW;
  25.     if (MaxH / VH) < Scale then
  26.       Scale := MaxH / VH;
  27.     FW := Trunc(VW * Scale);
  28.     FH := Trunc(VH * Scale);
  29.   end;
  30.  
  31.   // Wyśrodkuj na ekranie roboczym
  32.   SetBounds(
  33.     Screen.WorkAreaLeft + (MaxW - FW) div 2,
  34.     Screen.WorkAreaTop + (MaxH - FH) div 2,
  35.     FW,
  36.     FH
  37.   );
  38. end;    
Best regards / Pozdrawiam
paweld

Pe3s

  • Hero Member
  • *****
  • Posts: 638
Re: Matching video to form
« Reply #6 on: December 10, 2025, 01:54:30 pm »
@paweld, I have one more question: is it possible to add the option of scaling from the bottom and top to this procedure? Currently, it only scales from the left and right sides and corners. Is there another option for proportional scaling with the possibility of maximizing the form? Best regards

Code: Pascal  [Select][+][-]
  1. procedure TForm1.HandleSizing(var Msg: TLMessage);
  2. Const
  3.  Aspect = 16 / 9;
  4. begin
  5.   With WindowPos(Pointer(Msg.Lparam)^) do
  6.    Begin
  7.      cy := Round(cx / Aspect);
  8.    end;
  9. end;
  10.  


paweld

  • Hero Member
  • *****
  • Posts: 1561
Re: Matching video to form
« Reply #7 on: December 11, 2025, 06:40:24 am »
I'm sorry, but I don't quite understand your question—could you give me an example?
Best regards / Pozdrawiam
paweld

Pe3s

  • Hero Member
  • *****
  • Posts: 638
Re: Matching video to form
« Reply #8 on: December 11, 2025, 10:56:47 am »
Here is an example
Code: Pascal  [Select][+][-]
  1. procedure TForm1.HandleSizing(var Msg: TLMessage);
  2. const
  3.   Aspect = 16 / 9;
  4. var
  5.   R: PWindowPos;
  6. begin
  7.   R := PWindowPos(Msg.LParam);
  8.  
  9.   // Jeśli użytkownik zmienia szerokość — dopasuj wysokość
  10.   if R^.cx <> Width then
  11.     R^.cy := Round(R^.cx / Aspect)
  12.   // Jeśli użytkownik zmienia wysokość — dopasuj szerokość
  13.   else if R^.cy <> Height then
  14.     R^.cx := Round(R^.cy * Aspect);
  15. end;      
  16.  

Pe3s

  • Hero Member
  • *****
  • Posts: 638
Re: Matching video to form
« Reply #9 on: December 17, 2025, 01:11:07 pm »
How can I combine the procedures into one?
Width
Code: Pascal  [Select][+][-]
  1. const
  2.   Aspect = 16 / 9;
  3.  
  4. procedure TForm1.FormResize(Sender: TObject);
  5. begin
  6.   Constraints.MinHeight := Round(Width / Aspect);
  7.   Constraints.MaxHeight := Constraints.MinHeight;
  8. end;
  9.  
Height
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormResize(Sender: TObject);
  2. begin
  3.   Constraints.MinWidth  := Round(Height * Aspect);
  4.   Constraints.MaxWidth  := Constraints.MinWidth;
  5. end;
  6.  

 

TinyPortal © 2005-2018