Recent

Author Topic: CreateMessageDialog: draw on Canvas  (Read 589 times)

Rik

  • Jr. Member
  • **
  • Posts: 78
CreateMessageDialog: draw on Canvas
« on: February 04, 2025, 11:08:37 am »
I want to create my own message dialog, with text and background colours at my choice and multiline text with different fonts for each line.
My thought was to use CreateMessageDialog, leave the message parameter empty and instead draw the message on the Canvas using TextOut:

Code: Pascal  [Select][+][-]
  1.   MsgFrm:= CreateMessageDialog('TEST','',mtConfirmation,[mbYes,mbNo]); // MsgFrm: tForm;
  2.   try
  3.     MsgFrm.Color:= clYellow;
  4.     MsgFrm.Font.Color:= clBlue;
  5.     MsgFrm.Canvas.TextOut(20,20,'Hello world, here I am');
  6.     MsgFrm.ShowModal;
  7.   finally
  8.     FreeAndNil(MsgFrm);
  9.   end;

MsgFrm.Canvas.TextOut(20,20,'Hello world, here I am') however seems not to work (the text doesn't appear).
Am I doing something wrong or is it just not possible to access the Canvas with CreateMessageDialog?

ASerge

  • Hero Member
  • *****
  • Posts: 2383
Re: CreateMessageDialog: draw on Canvas
« Reply #1 on: February 04, 2025, 05:19:20 pm »
...draw the message on the Canvas using TextOut:
Am I doing something wrong or is it just not possible to access the Canvas with CreateMessageDialog?
Rendering takes place according to the requirements of the OS at any time. No one stores your image between them.
In this case, you drew in the void before the window appeared, and then the window drew itself.
It must be rendered in the OnPaint event for a form created via CreateMessageDialog.

madref

  • Hero Member
  • *****
  • Posts: 1094
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: CreateMessageDialog: draw on Canvas
« Reply #2 on: February 04, 2025, 07:46:02 pm »
why not create a separate form for all your messages.


I created a form with a textbox, image and 2 buttons and made it flexible.
I did it like this:

Code: Pascal  [Select][+][-]
  1. unit referee_meldingen;
  2.  
  3.  
  4. {$mode objfpc}{$H+}
  5.  
  6.  
  7. interface
  8.  
  9.  
  10. uses
  11.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, Buttons,
  12.   ExtCtrls, ColorSpeedButton;
  13.  
  14.  
  15. type
  16.   TMsgType = (mGeen, mInfo, mUitroep1, mUitroep2, mStop, mVraag1, mVraag2,
  17.               mBuis, mGo, mThink, mSign, mPotlood1, mPotlood2, mPDF, mKrant);
  18.   TMsgButton = (bYesNo, bOKCancel, bOK);
  19.  
  20.  
  21. type
  22.  
  23.  
  24.   { TForm_Melding }
  25.  
  26.  
  27.   TForm_Melding = class(TForm)
  28.     Button1: TColorSpeedButton;
  29.     Button2: TColorSpeedButton;
  30.     Image_Pop: TImage;
  31.     Label_Message: TLabel;
  32.     procedure Button1Click(Sender: TObject);
  33.     procedure Button2Click(Sender: TObject);
  34.     procedure FormActivate(Sender: TObject);
  35.     procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
  36.     procedure FormCreate(Sender: TObject);
  37.     procedure FormKeyDown(Sender: TObject; var Key: Word);
  38.     procedure FormShow(Sender: TObject);
  39.   private
  40.     { private declarations }
  41.     MB: TMsgButton;
  42.     Custom: Boolean;     // True -=> MSgCustom.  False -=> Normal message
  43.     procedure Show_JuistePop(const MsgT: TMsgType);
  44.   public
  45.     { public declarations }
  46.     procedure Melding (const MsgT: TMsgType; const MsgB: TMsgButton;
  47.                        const MsgStr, MsgCaption: string);
  48.     procedure Melding_Alleen (const MsgT: TMsgType; const MsgStr, MsgCaption: string);
  49.     procedure Melding_Aangepast (const MsgT: TMsgType;
  50.                                 const MsgStr, MsgCaption: string;
  51.                                 const Btn1Msg, Btn2Msg: string;
  52.                                 const Btn1Gnr, Btn2Gnr: integer);
  53.   end;
  54.  
  55.  
  56. var
  57.   Form_Melding: TForm_Melding;
  58.  
  59.  
  60. implementation
  61.  
  62.  
  63. {$R *.lfm}
  64.  
  65.  
  66. uses LCLType,
  67.   referee_lint,
  68.   referee_types;
  69.  
  70.  
  71. { TForm_Melding }
  72.  
  73.  
  74. procedure TForm_Melding.Button1Click(Sender: TObject);
  75. begin
  76.   MessageButtonPressed := 0;
  77.   Close;
  78. end;    // Button1Click
  79.  
  80.  
  81. procedure TForm_Melding.Button2Click(Sender: TObject);
  82. begin
  83.   MessageButtonPressed := 1;
  84.   Close;
  85. end;    // Button2Click
  86.  
  87.  
  88. procedure TForm_Melding.FormActivate(Sender: TObject);
  89. begin
  90.                Button1.StateHover.Color := KleurPop + $00A0A000;
  91.                Button1.StateHover.BorderColor:= KleurPop;
  92.                Button1.StateActive.Color := Ribbon_Rood ;
  93.                Button1.StateActive.BorderColor:= clBlack;
  94.                Button2.StateHover.Color := KleurPop + $00A0A000;
  95.                Button2.StateHover.BorderColor:= KleurPop;
  96.                Button2.StateActive.Color := Ribbon_Rood ;
  97.                Button2.StateActive.BorderColor:= clBlack;
  98. end;     // FormActivate
  99.  
  100.  
  101. procedure TForm_Melding.FormClose(Sender: TObject; var CloseAction: TCloseAction);
  102. begin
  103.   Form_Melding.FormStyle := fsNormal;
  104.   CloseAction := caHide;
  105. end;     // FormClose
  106.  
  107.  
  108. procedure TForm_Melding.FormCreate(Sender: TObject);
  109. begin
  110.   MessageButtonPressed := -1;
  111.   Button1.Visible := False;
  112.   Button2.Visible := False;
  113.   Color := Achter;
  114. end;    // FormCreate
  115.  
  116.  
  117. procedure TForm_Melding.FormKeyDown(Sender: TObject; var Key: Word);
  118. begin
  119.   if Custom = False then begin
  120.     case MB of
  121.       bOK       : begin
  122.                     if Key = VK_O then begin
  123.                       Button1.Click;
  124.                       Key := 0;
  125.                     end;
  126.                   end;  // OK
  127.       bYesNo    : begin
  128.                     if Key = VK_Y then begin
  129.                       Button1.Click;
  130.                       Key := 0;
  131.                     end;
  132.                     if Key = VK_N then begin
  133.                       Button2.Click;
  134.                       Key := 0;
  135.                     end;
  136.                   end; // YesNo
  137.       bOKCancel : begin
  138.                     if Key = VK_O then begin
  139.                       Button1.Click;
  140.                       Key := 0;
  141.                     end;
  142.                     if Key = VK_C then begin
  143.                       Button2.Click;
  144.                       Key := 0;
  145.                     end;
  146.                   end; // OKCancel
  147.     end;     // MsgB
  148.   end;  // if
  149. end;     // FormKeyDown
  150.  
  151.  
  152. procedure TForm_Melding.FormShow(Sender: TObject);
  153. begin
  154.   Form_Melding.FormStyle := fsSystemStayOnTop;
  155. end;     // FormShow
  156.  
  157.  
  158. procedure TForm_Melding.Melding(const MsgT: TMsgType; const MsgB: TMsgButton;
  159.                                 const MsgStr, MsgCaption: string);
  160. begin
  161.   Form_Melding.Height := 195;
  162.   MessageButtonPressed := -1;
  163.   Custom := False;
  164.   MB := MsgB;
  165.   Form_Melding.Caption := MsgCaption;
  166.   Label_Message.Caption := MsgStr;
  167.   case MsgT of
  168.     mGeen     : begin
  169.                   //Ook lblMessage verschuiven
  170.                   Label_Message.Left := 10;
  171.                   Label_Message.Top := 10;
  172.                   Label_Message.Width := 400;
  173.                   Show_JuistePop (mGeen);
  174.                 end;     // mNone
  175.     mStop     : Show_JuistePop (mStop);
  176.     mInfo     : Show_JuistePop (mInfo);
  177.     mVraag1   : Show_JuistePop (mVraag1);
  178.     mVraag2   : Show_JuistePop (mVraag2);
  179.     mUitroep1 : Show_JuistePop (mUitroep1);
  180.     mUitroep2 : Show_JuistePop (mUitroep2);
  181.     mBuis     : Show_JuistePop (mBuis);
  182.     mGo       : Show_JuistePop (mGo);
  183.     mThink    : Show_JuistePop (mThink);
  184.     mSign     : Show_JuistePop (mSign);
  185.     mPotlood1 : Show_JuistePop (mPotlood1);
  186.     mPotlood2 : Show_JuistePop (mPotlood2);
  187.     mPDF      : Show_JuistePop (mPDF);
  188.     mKrant    : Show_JuistePop (mKrant);
  189.   end;     // MsgT
  190.   case MsgB of
  191.     bOK       : begin
  192.                   Button1.Left := 175;
  193.                   Button1.Top := 145;
  194.                   Button1.Caption := '  OK';
  195.                   Button1.Visible := True;
  196.                   Form_Lint.ImageList_Buttons_16.GetBitmap(0, Button1.Glyph);
  197.                   Button2.Visible := False;
  198.                 end;  // OK
  199.     bYesNo    : begin
  200.                   Button1.Left := 95;
  201.                   Button1.Top := 145;
  202.                   Button1.Caption := 'Yes';
  203.                   Button2.Caption := 'No';
  204.                   Form_Lint.ImageList_Buttons_16.GetBitmap(0, Button1.Glyph);
  205.                   Form_Lint.ImageList_Buttons_16.GetBitmap(5, Button2.Glyph);
  206.                   Button1.Visible := True;
  207.                   Button2.Visible := True;
  208.                 end; // YesNo
  209.     bOKCancel : begin
  210.                   Button1.Left := 95;
  211.                   Button1.Top := 145;
  212.                   Button1.Caption := 'Ok';
  213.                   Button2.Caption := 'Cancel';
  214.                   Form_Lint.ImageList_Buttons_16.GetBitmap(1, Button1.Glyph);
  215.                   Form_Lint.ImageList_Buttons_16.GetBitmap(4, Button2.Glyph);
  216.                   Button1.Visible := True;
  217.                   Button2.Visible := True;
  218.                 end; // OKCancel
  219.   end;     // MsgB
  220.   ShowModal;
  221. end;    // Melding
  222.  
  223.  
  224. procedure TForm_Melding.Melding_Alleen(const MsgT: TMsgType; const MsgStr, MsgCaption: string);
  225. begin
  226.   Button1.Visible := False;
  227.   Button2.Visible := False;
  228.   Form_Melding.Caption := MsgCaption;
  229.   Label_Message.Caption := MsgStr;
  230.   case MsgT of
  231.     mGeen     : begin
  232.                   //Ook lblMessage verschuiven
  233.                   Label_Message.Left := 10;
  234.                   Label_Message.Top := 10;
  235.                   Label_Message.Width := 400;
  236.                   Show_JuistePop (mGeen);
  237.                 end;     // mNone
  238.     mStop     : Show_JuistePop (mStop);
  239.     mInfo     : Show_JuistePop (mInfo);
  240.     mVraag1   : Show_JuistePop (mVraag1);
  241.     mVraag2   : Show_JuistePop (mVraag2);
  242.     mUitroep1 : Show_JuistePop (mUitroep1);
  243.     mUitroep2 : Show_JuistePop (mUitroep2);
  244.     mBuis     : Show_JuistePop (mBuis);
  245.     mGo       : Show_JuistePop (mGo);
  246.     mThink    : Show_JuistePop (mThink);
  247.     mSign     : Show_JuistePop (mSign);
  248.     mPotlood1 : Show_JuistePop (mPotlood1);
  249.     mPotlood2 : Show_JuistePop (mPotlood2);
  250.     mPDF      : Show_JuistePop (mPDF);
  251.     mKrant    : Show_JuistePop (mKrant);
  252.   end;     // MsgT
  253.   Form_Melding.Height := 145;
  254.   Form_Melding.FormStyle:=fsStayOnTop;
  255.   Show;
  256. end;     // Melding_Alleen
  257.  
  258.  
  259. procedure TForm_Melding.Melding_Aangepast(const MsgT: TMsgType;
  260.                                           const MsgStr, MsgCaption: string;
  261.                                           const Btn1Msg, Btn2Msg: string;
  262.                                           const Btn1Gnr, Btn2Gnr: integer);
  263. begin
  264.   Form_Melding.Height := 195;
  265.   MessageButtonPressed := -1;
  266.   Custom := True;
  267.   Form_Melding.Caption := MsgCaption;
  268.   Label_Message.Caption := MsgStr;
  269.   case MsgT of
  270.     mGeen     : begin
  271.                   //Ook lblMessage verschuiven
  272.                   Label_Message.Left := 10;
  273.                   Label_Message.Top := 10;
  274.                   Label_Message.Width := 400;
  275.                   Show_JuistePop (mGeen);
  276.                 end;     // mNone
  277.     mStop     : Show_JuistePop (mStop);
  278.     mInfo     : Show_JuistePop (mInfo);
  279.     mVraag1   : Show_JuistePop (mVraag1);
  280.     mVraag2   : Show_JuistePop (mVraag2);
  281.     mUitroep1 : Show_JuistePop (mUitroep1);
  282.     mUitroep2 : Show_JuistePop (mUitroep2);
  283.     mBuis     : Show_JuistePop (mBuis);
  284.     mGo       : Show_JuistePop (mGo);
  285.     mThink    : Show_JuistePop (mThink);
  286.     mSign     : Show_JuistePop (mSign);
  287.     mPotlood1 : Show_JuistePop (mPotlood1);
  288.     mPotlood2 : Show_JuistePop (mPotlood2);
  289.     mPDF      : Show_JuistePop (mPDF);
  290.     mKrant    : Show_JuistePop (mKrant);
  291.   end;     // MsgT
  292.   Button1.Left := 95;
  293.   Button1.Top := 145;
  294.   Button1.Caption := ' ' + Btn1Msg;
  295.   Button2.Caption := ' ' + Btn2Msg;
  296.   if Btn1Gnr = -1 then
  297.       Button1.Glyph.Clear
  298.   else
  299.     Form_Lint.ImageList_Buttons_16.GetBitmap(Btn1Gnr, Button1.Glyph);
  300.   if Btn2Gnr = -1 then
  301.       Button1.Glyph.Clear
  302.   else
  303.     Form_Lint.ImageList_Buttons_16.GetBitmap(Btn2Gnr, Button2.Glyph);
  304.   Button1.Visible := True;
  305.   Button2.Visible := True;
  306.   ShowModal;
  307. end;    // Melding_Aangepast
  308.  
  309.  
  310. procedure TForm_Melding.Show_JuistePop(const MsgT: TMsgType);
  311. begin
  312.                case MsgT of
  313.                  mGeen     : Image_Pop.Picture.Bitmap.Assign(nil);
  314.                  mInfo     : Form_Lint.ImageList_Rood.GetBitmap(01,Image_Pop.Picture.Bitmap);
  315.                  mUitroep1 : Form_Lint.ImageList_Rood.GetBitmap(00,Image_Pop.Picture.Bitmap);
  316.                  mUitroep2 : Form_Lint.ImageList_Rood.GetBitmap(14,Image_Pop.Picture.Bitmap);
  317.                  mStop     : Form_Lint.ImageList_Rood.GetBitmap(05,Image_Pop.Picture.Bitmap);
  318.                  mVraag1   : Form_Lint.ImageList_Rood.GetBitmap(03,Image_Pop.Picture.Bitmap);
  319.                  mVraag2   : Form_Lint.ImageList_Rood.GetBitmap(13,Image_Pop.Picture.Bitmap);
  320.                  mBuis     : Form_Lint.ImageList_Rood.GetBitmap(02,Image_Pop.Picture.Bitmap);
  321.                  mGo       : Form_Lint.ImageList_Rood.GetBitmap(04,Image_Pop.Picture.Bitmap);
  322.                  mThink    : Form_Lint.ImageList_Rood.GetBitmap(06,Image_Pop.Picture.Bitmap);
  323.                  mSign     : Form_Lint.ImageList_Rood.GetBitmap(12,Image_Pop.Picture.Bitmap);
  324.                  mPotlood1 : Form_Lint.ImageList_Rood.GetBitmap(08,Image_Pop.Picture.Bitmap);
  325.                  mPotlood2 : Form_Lint.ImageList_Rood.GetBitmap(17,Image_Pop.Picture.Bitmap);
  326.                  mPDF      : Form_Lint.ImageList_Rood.GetBitmap(18,Image_Pop.Picture.Bitmap);
  327.                  mKrant    : Form_Lint.ImageList_Rood.GetBitmap(11,Image_Pop.Picture.Bitmap);
  328.                end;  // MsgT
  329. end;     // Show_JuistePop
  330.  
  331.  
  332. end.
  333.  
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Main Platform:
--------------
Mac OS X Sonoma 14.7
Lazarus 3.99 (Lazarus 3.99 (rev main_3_99-2668-g6b352d830e) FPC 3.3.1 x86_64-darwin-cocoa)

Windows 10 Pro
Lazarus 3.99 (rev cbfd80ce39)

Rik

  • Jr. Member
  • **
  • Posts: 78
Re: CreateMessageDialog: draw on Canvas
« Reply #3 on: February 05, 2025, 09:11:32 am »
Quote
Posted by: ASerge
It must be rendered in the OnPaint event for a form created via CreateMessageDialog.
Indeed, I should have thought of that.
After adding an OnPaint event it works fine, thanks.

Quote
Posted by: madref
why not create a separate form for all your messages
No doubt that this would also do the job.
But using CreateMessageDialog it takes far less lines of code, about 25 lines instead of over 300 lines.

d2010

  • Full Member
  • ***
  • Posts: 103
Re: CreateMessageDialog: draw on Canvas
« Reply #4 on: February 05, 2025, 10:35:58 am »
You can add many items/ inside PageControl.
You search "PageControl"

Code: [Select]
    object PageControl: TPageControl
      Left = 12
      Top = 12
      Width = 409
      Height = 227
      HelpContext = 1650
      ActivePage = PakListSheet
      Align = alClient
      MultiLine = True
      TabOrder = 0
      object AutoexecPage: TTabSheet
        HelpContext = 1770
        Caption = 'Autoexec.bat'
        object TLabel
          Left = 648
          Top = 0
          Width = 44
          Height = 13
          Caption = 'Location:'
        end
        object Memo2: TMemo
          Left = 0
          Top = 0
          Width = 401
          Height = 199
          Align = alClient
          Font.Charset = DEFAULT_CHARSET
          Font.Color = clWindowText
          Font.Height = -15
          Font.Name = 'MS Sans Serif'
          Font.Style = []
          ParentFont = False
          ScrollBars = ssBoth
          TabOrder = 0
        end
      end
      object DirCondPage: TTabSheet



Code: Pascal  [Select][+][-]
  1. procedure TMainForm.Exit_zerop1Click(Sender: TObject);
  2. Var amsg_exit:TCloseAction;
  3.     Gcw_Cancelled:boolean;
  4. begin EroareMsgOkai := true;
  5.       Gcw_Cancelled:=false;
  6.       Label4que.caption:='qQuit?';
  7.       Case (Exit_zerop.checked) of
  8. fals:  Begin
  9.             dfn_dcl_yesno4.tab:=PageControl.ActivePage;
  10.             PageControl.ActivePage:=PakListSheet;Refresh;
  11.             dfn_dcl_yesno4.cmd:=epYesQuit;
  12.             Exit_zerop.checked:=not Exit_zerop.checked;
  13.        End;
  14. true:  Begin if (PageControl.ActivePage<>dfn_dcl_yesno4.tab) then
  15.                    PageControl.ActivePage:=dfn_dcl_yesno4.tab;
  16.              Refresh;
  17.              Exit_zerop.checked:=not Exit_zerop.checked;
  18.       End;
  19.      End;
  20.      Refresh;
  21. end;
  22.  
« Last Edit: February 05, 2025, 10:38:02 am by d2010 »

 

TinyPortal © 2005-2018