4

I have a Form that needs to be embedded in another Form. I'm placing on TCard:

EmbeddedForm->Parent = ACard;
EmbeddedForm->BorderStyle = bsNone;
EmbeddedForm->Align = alClient;
EmbeddedForm->Show();

All is working well, except that menus on the EmbeddedForm are offset to where they would be if the form were located in top left of the screen. To be clear, the menu shows up in the proper place on the EmbeddedForm, but when clicked, the sub-menu is in the wrong place.

I've tried modifying the DrawItem event, but so far I can't call the base class DrawItem() as it's protected:

void __fastcall TEmbeddedForm::File1DrawItem(TObject *Sender, TCanvas *ACanvas, TRect &ARect, bool Selected)
{
    ARect.Left = MainMenu1.Left;  // or some other calculation, not important yet
    ARect.Top = MainMenu1.Top;

    // ??? how to do normal drawItem from here?
}

I'm thinking, either I have to draw it myself (I don't want to) or somehow explain to TMainMenu where it's actually located (preferred solution).

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
iplayfast
  • 140
  • 10
  • 1
    Is there a reason why you are embedding Forms, instead of using `TFrame`? – Remy Lebeau Oct 21 '21 at 23:05
  • I just tried using TFrames, in the last two weeks. So many more problems as other forms use the embedded forms for calculations or to push data, etc. I eventually just went back to embedding forms and this is basically the last problem. – iplayfast Oct 22 '21 at 13:31
  • Forms are designed to be free-floating windows, they are not really meant to be embedded. You really should be using `TFrame`, it was designed for embedded purposes. Except that `TMainMenu` is not really meant to be used in embedded UIs. It works for non-embedded Forms, and MDI child Forms. If that is not an option for you, have you tried docking the Forms instead of embedding them as children? It would really help if you could show screenshots of what you are trying to accomplish, and are having trouble with. – Remy Lebeau Oct 29 '21 at 00:19
  • Do you know of any sample code that docks forms programmatically? [link](https://scontent.fybz1-1.fna.fbcdn.net/v/t1.6435-9/243818344_10165536762160263_9188720836258366959_n.jpg?_nc_cat=101&ccb=1-5&_nc_sid=dbeb18&_nc_ohc=LdDQS_lY5SMAX_HcSo2&_nc_oc=AQk-upTHSQBNcYoIvM8e4F2x0b01J0fT_mDlBg5I86rGhB8F8GaqX1r3lSlAxxgqe6o&_nc_ht=scontent.fybz1-1.fna&oh=d2316ed36b253d9aa508d4f3f62ca959&oe=61A057AE) Hopefully this image shows the program, with the ide behind it. The panel in the middle with File Options etc menuitems is actually a form. The menu pops up in the top left corner.nowhere near the program. – iplayfast Oct 30 '21 at 01:44
  • See [Implementing Drag and Dock in Controls](https://docwiki.embarcadero.com/RADStudio/en/Implementing_Drag_and_Dock_in_Controls) and [`ManualDock()`](https://docwiki.embarcadero.com/Libraries/en/Vcl.Controls.TControl.ManualDock). But I would suggest trying a `TFrame` with a non-`TMainMenu` type of menu component on it, such as `TActionMainMenuBar` or `TActionToolBar`. – Remy Lebeau Oct 30 '21 at 05:15
  • So I've changed the Tforms to TFrames, and I'm getting an error on creation. Project cqs.exe raised exception class EReadError with message 'Ancestor for '' not found'. Coming from System.Classes.pas function TReader.FindAncestorComponent(const Name: string; ComponentClass: TPersistentClass): TComponent; I'm not sure as the Name doesn't show in the debugger, but I think it's coming from a pop-up menu. – iplayfast Nov 02 '21 at 21:49
  • I hope by "change the TForms to TFrames", you didn't simply rename any TForm references to TFrame. Create new TFrame classes fresh (File > New > Other... > Delphi > Individual Files > VCL Frame), and then copy the contents of your TForms to the TFrames as needed. – Remy Lebeau Nov 02 '21 at 23:17
  • I was hoping that renaming the base class would show up the incompatibilites when I recompiled. So yes I was doing that. Currently trying your suggestion, there is a ton of code that needs to be copied and hooked up again, which is why I was trying to avoid this method. – iplayfast Nov 05 '21 at 17:54

0 Answers0