Recent

Author Topic: TMenuItem created runtime with caption "-"  (Read 1374 times)

MISV

  • Hero Member
  • *****
  • Posts: 783
TMenuItem created runtime with caption "-"
« on: July 15, 2019, 02:11:50 pm »
I have a piece of code where I copy menu items from one list to another. How that works is that the new destination menu item is created runtime and then assigned properties including caption.

I have noticed that for menu items with "-" they do not appear to be drawn in normal menu separator mode but instead simply "-"


skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: TMenuItem created runtime with caption "-"
« Reply #1 on: July 15, 2019, 03:17:27 pm »
sample project?

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 1313
Re: TMenuItem created runtime with caption "-"
« Reply #2 on: July 15, 2019, 05:52:59 pm »
There is a separate function in the Windows API to create a separator, but the function to add a menu item translates a '-' into that. That translation isn't happening, probably because you're assigning pointers instead of creating menu items.

MISV

  • Hero Member
  • *****
  • Posts: 783
Re: TMenuItem created runtime with caption "-"
« Reply #3 on: July 15, 2019, 10:33:33 pm »
This seems to solve it

Code: Pascal  [Select][+][-]
  1.  
  2.   ADestItem.Caption := ASourceItem.Caption;
  3.   if ADestItem.Caption = '-' then
  4.     ADestItem.RecreateHandle
  5.   ;
  6.  

But I will provide more code later

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: TMenuItem created runtime with caption "-"
« Reply #4 on: July 16, 2019, 12:13:11 am »
I use the following code in a project of mine. It builds a menu from a list of strings (containing the captions, read from an INI file) either under some other menu item or as a "root" menu item in the aplications main menu; i.e. it does something like:
ParentItem                -or-    TopItem
      \- TopItem | Item1 |              | Item1 |
                 | Item2 |              | Item2 |
                 | etc.. |              | etc.. |

In the original the items' Tag serves to differentitate it from the others and to specify in which group it is; it's later divided in the DoMarkup() handler into two indexes to access an array[x][y] of string. I've deleted all that since it bears no relation to your post.

The main point, wrt this thread, is that if the string to use as caption is "-" you should use AddSeparator() (or NewLine()).
Code: Pascal  [Select][+][-]
  1. procedure TMainForm.AddMenu(const ParentItem: TMenuItem;
  2.   const TopName: String; const NewItems: TStrings);
  3. var
  4.   TopItem, AnItem: TMenuItem;
  5.   AName: String;
  6. begin
  7.   {Root the hierarchy}
  8.   TopItem := TMenuItem.Create(Self);
  9.   TopItem.Caption := TopName;
  10.   if Assigned(ParentItem) then
  11.     ParentItem.Add(TopItem)
  12.   else
  13.     MainMenu.Items.Add(TopItem);
  14.   { Build the menu}
  15.   for AName in NewItems do begin
  16.     if AName = '-' then
  17.       TopItem.AddSeparator
  18.     else begin
  19.       AnItem := TMenuItem.Create(Self);
  20.       AnItem.Caption := AName;
  21.       AnItem.OnClick := @DoMarkup;
  22.       TopItem.Add(AnItem);
  23.     end;
  24.   end;
  25. end;
Note that where I create new items and set their properties, etc. you could just as easily assign from your "source" menu, only where I test "if AName = '-' then..." you would have "if Source.Caption = '-'", etc.
« Last Edit: July 16, 2019, 12:15:44 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: TMenuItem created runtime with caption "-"
« Reply #5 on: July 16, 2019, 02:28:10 am »
But I will provide more code later
I think, it's not needed.
try r61595

MISV

  • Hero Member
  • *****
  • Posts: 783
Re: TMenuItem created runtime with caption "-"
« Reply #6 on: July 17, 2019, 11:56:37 am »
Thank you - I will try when I get my release done (won't risk updating/breaking my app just before release) and report back!

 

TinyPortal © 2005-2018