Recent

Author Topic: RunTime CreateNew Form use of Windows Themes  (Read 2433 times)

tudi_x

  • Hero Member
  • *****
  • Posts: 532
RunTime CreateNew Form use of Windows Themes
« on: September 28, 2017, 11:52:59 am »
hi All,
i am creating a form at runtime as per the below code.
please advise how i can pass the use of Windows Themes to the form as currently it does not get the blue like the parent.

Code: Pascal  [Select][+][-]
  1. unit cancel_form;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Graphics, Controls, StdCtrls,
  9.   m_panel;
  10.  
  11. type
  12.   TCancel = class(TForm)
  13.   public
  14.     btYes, btNo: TModPanel;
  15.     lbSure: TLabel;
  16.     constructor Create(AOwner: TForm; ADistance: word; AText: string); reintroduce;
  17.     destructor Destroy; override;
  18.   end;
  19.  
  20. implementation
  21.  
  22. constructor TCancel.Create(AOwner: TForm; ADistance: word; AText: string);
  23. begin
  24.   inherited CreateNew(AOwner);
  25.  
  26.   self.Color := clWhite;
  27.   self.Position := poOwnerFormCenter;
  28.   self.BorderIcons := [biSystemMenu];
  29.   self.BorderStyle := bsSingle;
  30.   self.Caption := 'Please Confirm';
  31.   self.Parent := AOwner;
  32.  
  33.   {add label}
  34.   lbSure := TLabel.Create(self);
  35.   lbSure.Caption:= AText;
  36.   lbSure.AnchorHorizontalCenterTo(self);
  37.   lbSure.Top:= 25;
  38.   lbSure.Parent:= self;
  39.  
  40.   {add button Yes}
  41.   btYes := TModPanel.Create(self, 1);
  42.   btYes.Caption := 'Yes';
  43.   btYes.Font.Size := AOwner.Font.Size;
  44.   btYes.Left := round((self.Width - 2 * btYes.Width - ADistance) / 2);
  45.   btYes.Top := lbSure.Top + lbSure.Height + ADistance;
  46.  
  47.   {add button No}
  48.   btNo := TModPanel.Create(self, 1);
  49.   btNo.Caption := 'No';
  50.   btNo.Font.Size := AOwner.Font.Size;
  51.   btNo.Left := btYes.Left + btYes.Width + ADistance;
  52.   btNo.Top := btYes.Top;
  53.  
  54.   self.Height:= lbSure.Top + lbSure.Height + ADistance + btYes.Height + ADistance;
  55. end;
  56.  
  57. destructor TCancel.Destroy;
  58. begin
  59.   if Assigned(btYes) then FreeAndNil(btYes);
  60.   if Assigned(btNo) then FreeAndNil(btNo);
  61.   if Assigned(lbSure) then FreeAndNil(lbSure);
  62.   inherited Destroy;
  63. end;
  64.  
  65. end.

thank you

Lazarus 1.8 RC4 64b, Windows 7
Lazarus 2.0.2 64b on Debian LXDE 10

RAW

  • Hero Member
  • *****
  • Posts: 868
Re: RunTime CreateNew Form use of Windows Themes
« Reply #1 on: September 28, 2017, 03:12:27 pm »
Delete this line...
Code: Pascal  [Select][+][-]
  1. self.Parent := AOwner;
... then this MDI-crap goes byebye...  :)
Windows 7 Pro (x64 Sp1) & Windows XP Pro (x86 Sp3).

RAW

  • Hero Member
  • *****
  • Posts: 868
Re: RunTime CreateNew Form use of Windows Themes
« Reply #2 on: September 28, 2017, 03:16:25 pm »
Try this...

Code: Pascal  [Select][+][-]
  1. Procedure TForm1.FormClick(Sender: TObject);
  2.   Var
  3.    wnd: TForm;
  4.  Begin
  5.   wnd            := TForm.Create(Self);
  6.   wnd.Caption    := 'MyNewWindow';
  7.   wnd.PopupMode  := pmExplicit;
  8.   wnd.PopupParent:= Self;
  9.   wnd.Show;
  10.  End;
Windows 7 Pro (x64 Sp1) & Windows XP Pro (x86 Sp3).

RAW

  • Hero Member
  • *****
  • Posts: 868
Re: RunTime CreateNew Form use of Windows Themes
« Reply #3 on: September 28, 2017, 03:37:30 pm »
BTW: you don't need a class if you need a custom window.... a simple function would be enough...  :)

For example... (something like this...)
Code: Pascal  [Select][+][-]
  1. Function TForm1.DialogWnd
  2. (strWndTxt, strTxt: String; wndCalcPos: TForm; booOkBtn: Boolean): String;
  3.   Const
  4.    MyFont = 'Arial';
  5.   Var
  6.    Wnd: TForm; Memo: TMemo;
  7.    btnYes, btnNo, btnCancel: TButton;
  8.  
  9.    iBtnX, iBtnY, iDlgX, iDlgY, iDlgTop, iDlgLeft,
  10.    iWorkAreaX, iWorkAreaY, iWALeft, iWATop: Integer;
  11.  Begin
  12.   Try
  13.    Result:= '';
  14.     wnd:= TForm.Create(Nil);
  15.      Try
  16.       wnd.Color         := clBtnFace;
  17.       wnd.Caption       := strWndTxt;
  18.       wnd.BorderStyle   := bsSizeable;
  19.       wnd.DoubleBuffered:= True;
  20.       wnd.BorderIcons   := [];
  21.  
  22.       iDlgX:= wndCalcPos.Width;
  23.       iDlgY:= wndCalcPos.Height;
  24.  
  25.       If iDlgX < 300 Then iDlgX:= 300;
  26.       If iDlgY < 200 Then iDlgY:= 200;
  27.  
  28.       If iDlgX >= 800 Then iDlgX:= iDlgX Div 2;
  29.       If iDlgY >= 550 Then iDlgY:= iDlgY Div 2;
  30.  
  31.       iDlgLeft:= wndCalcPos.Left+((wndCalcPos.Width -iDlgX) Div 2);
  32.       iDlgTop := wndCalcPos.Top +((wndCalcPos.Height-iDlgY) Div 2);
  33.  
  34.       iWorkAreaX:= Screen.WorkAreaWidth;
  35.       iWorkAreaY:= Screen.WorkAreaHeight;
  36.       iWALeft   := Screen.WorkAreaLeft;
  37.       iWATop    := Screen.WorkAreaTop;
  38.  
  39.       If iDlgLeft < iWALeft Then iDlgLeft:= iWALeft+40;
  40.       If iDlgTop  < iWATop  Then iDlgTop := iWATop +40;
  41.  
  42.       If iDlgTop   > (iWorkAreaY-iDlgY)
  43.       Then iDlgTop:= (iWorkAreaY-iDlgY-40);
  44.  
  45.       If iDlgLeft   > (iWorkAreaX-iDlgX)
  46.       Then iDlgLeft:= (iWorkAreaX-iDlgX-40);
  47.  
  48.       wnd.SetBounds(iDlgLeft, iDlgTop, iDlgX, iDlgY);
  49.  
  50.       wnd.Constraints.MinHeight:= wnd.Height;
  51.       wnd.Constraints.MaxHeight:= wnd.Height;
  52.       wnd.Constraints.MinWidth := wnd.Width;
  53.       wnd.Constraints.MaxWidth := wnd.Width;
  54.  
  55.       btnYes               := TButton.Create(wnd);
  56.       btnYes.Caption       := 'YES';
  57.       btnYes.Font.Name     := MyFont;
  58.       btnYes.Font.Quality  := fqAntialiased;
  59.       btnYes.Font.Style    := [fsBold];
  60.       btnYes.DoubleBuffered:= True;
  61.       btnYes.ModalResult   := mrYes;
  62.       btnYes.Parent        := wnd;
  63.  
  64.       btnNo               := TButton.Create(wnd);
  65.       btnNo.Caption       := 'NO';
  66.       btnNo.Font.Name     := MyFont;
  67.       btnNo.Font.Quality  := fqAntialiased;
  68.       btnNo.Font.Style    := [fsBold];
  69.       btnNo.DoubleBuffered:= True;
  70.       btnNo.ModalResult   := mrNo;
  71.       btnNo.Parent        := wnd;
  72.  
  73.       btnCancel               := TButton.Create(wnd);
  74.       btnCancel.Caption       := 'CANCEL';
  75.       btnCancel.Font.Name     := MyFont;
  76.       btnCancel.Font.Quality  := fqAntialiased;
  77.       btnCancel.Font.Style    := [fsBold];
  78.       btnCancel.DoubleBuffered:= True;
  79.       btnCancel.ModalResult   := mrCancel;
  80.       btnCancel.Parent        := wnd;
  81.  
  82.       iBtnX:= (wnd.ClientWidth-20) Div 3;
  83.       iBtnY:= iBtnX Div 5;
  84.  
  85.       btnYes.SetBounds(5, wnd.ClientHeight-iBtnY, iBtnX, iBtnY);
  86.       btnNo.SetBounds((wnd.ClientWidth-iBtnX) Div 2, btnYes.Top, iBtnX, iBtnY);
  87.       btnCancel.SetBounds(wnd.ClientWidth-iBtnX-5, btnYes.Top, iBtnX, iBtnY);
  88.  
  89.       Canvas.Font.Size   := 1;
  90.       Canvas.Font.Name   := MyFont;
  91.       Canvas.Font.Style  := btnCancel.Font.Style;
  92.       Canvas.Font.Quality:= fqAntialiased;
  93.  
  94.       While Not (Canvas.TextWidth('CANCEL') > iBtnX-20)
  95.       Do Canvas.Font.Size:= Canvas.Font.Size+1;
  96.  
  97.       While (Canvas.TextHeight('CANCEL') > btnCancel.ClientHeight)
  98.       Do Canvas.Font.Size:= Canvas.Font.Size-1;
  99.  
  100.       btnYes.Font.Size   := Canvas.Font.Size;
  101.       btnNo.Font.Size    := btnYes.Font.Size;
  102.       btnCancel.Font.Size:= btnYes.Font.Size;
  103.  
  104.       Memo               := TMemo.Create(wnd);
  105.       Memo.Font.Name     := MyFont;
  106.       Memo.Font.Size     := wnd.ClientWidth Div 20;
  107.       Memo.Font.Style    := [fsBold];
  108.       Memo.Font.Quality  := fqAntialiased;
  109.       Memo.Font.Color    := clBtnText;
  110.       Memo.Color         := clBtnFace;
  111.       Memo.Alignment     := taCenter;
  112.       Memo.BorderStyle   := bsNone;
  113.       Memo.HideSelection := True;
  114.       Memo.Text          := strTxt;
  115.       Memo.ReadOnly      := True;
  116.       Memo.WordWrap      := True;
  117.       Memo.WantTabs      := False;
  118.       Memo.DoubleBuffered:= True;
  119.       Memo.WantReturns   := False;
  120.       Memo.ScrollBars    := ssAutoVertical;
  121.       Memo.SetBounds     (0, 0, wnd.ClientWidth, wnd.ClientHeight-iBtnY-10);
  122.       Memo.Parent        := wnd;
  123.  
  124.       If booOkBtn
  125.       Then
  126.        Begin
  127.         btnYes.Hide;
  128.         btnCancel.Hide;
  129.         btnNo.Caption:= 'OK';
  130.         wnd.ActiveControl:= btnNo;
  131.        End
  132.       Else wnd.ActiveControl:= btnYes;
  133.  
  134.       wnd.ShowModal;
  135.  
  136.       Case wnd.ModalResult
  137.       Of
  138.        mrYes   : Result:= 'YES';
  139.        mrNo    : Result:= 'NO';
  140.        mrCancel: Result:= 'CANCEL';
  141.       End;
  142.      Finally
  143.       wnd.Release;
  144.       wnd:= Nil;
  145.      End;
  146.   Except
  147.   End;
  148.  End;

// Choice Dialog
Code: Pascal  [Select][+][-]
  1. Procedure TForm1.FormClick(Sender: TObject);
  2.  Begin
  3.   Case DialogWnd('MyProgram', strText, Self, False)
  4.   Of
  5.    'YES'   : ShowMessage('YES');
  6.    'NO'    : ShowMessage('NO');
  7.    'CANCEL': ShowMessage('CANCEL');
  8.   End;
  9.  End;

// OK-Window
Code: Pascal  [Select][+][-]
  1. Procedure TForm1.FormClick(Sender: TObject);
  2.  Begin
  3.   DialogWnd('MyProgram', strText, Self, True);
  4.  End;
« Last Edit: October 04, 2017, 01:49:50 am by RAW »
Windows 7 Pro (x64 Sp1) & Windows XP Pro (x86 Sp3).

RAW

  • Hero Member
  • *****
  • Posts: 868
Re: RunTime CreateNew Form use of Windows Themes
« Reply #4 on: September 28, 2017, 04:40:47 pm »
Quote
destructor TCancel.Destroy;
begin
  if Assigned(btYes) then FreeAndNil(btYes);
  if Assigned(btNo) then FreeAndNil(btNo);
  if Assigned(lbSure) then FreeAndNil(lbSure);
  inherited Destroy;
end;

What exactly is this for ??? You've created everything with (SELF)...  :)
Windows 7 Pro (x64 Sp1) & Windows XP Pro (x86 Sp3).

tudi_x

  • Hero Member
  • *****
  • Posts: 532
Re: RunTime CreateNew Form use of Windows Themes
« Reply #5 on: September 28, 2017, 05:42:25 pm »
thank you RAW!
i thought it would have been a property or something to inherit the os theme usage.
now i understand that in case of a ShowModal due to the fact that main thread is with the Modal also the focus shifts to the Modal and change of focus is change of color.

i am looking into and it looks like for Linux and OSX i would need to write a different code.
the destroy is just in case  :)
« Last Edit: September 28, 2017, 06:22:34 pm by tudi_x »
Lazarus 2.0.2 64b on Debian LXDE 10

 

TinyPortal © 2005-2018