I have two separate forms in my application. I created one additional empty form and placed a TMainMenu component on it with two menu items. This new form will serve as the main form from which everything else will be loaded. Now I want it to work like this: when you click either of the two menu items, it should load the respective form's contents the menu item associated with to the main form. How can I achieve this? Or what is the typical approach to this kind of problem? Please, provide a simple illustrative example.
Create a TPanel on your MainForm and set its Align := alClient, BevelOuter := bvNone. Also give the MainForm attributes Form1: TForm1 and Form2: TForm2. Then display Form1 or Form2 in this way:
Showing Form1:
if not Assigned(MainForm.Form1) then
MainForm.Form1 := TForm1.Create(MainForm);
MainForm.Form1.Parent := MainForm.Panel1;
MainForm.Form1.Align := alClient;
MainForm.Form1.BorderStyle := bsNone;
MainForm.Form1.Visible := True;
if Assigned(MainForm.Form2) then
MainForm.Form2.Visible := False;
Of course you don't need to write MainForm; it's just to make the example clear.
If you have a bunch of Forms already created and want to add the ability of being show "docked" you could change the inheritance to a new template instead of TFrom.
By adding and overloaded constructor you will be able to use them as usual or "docked".
To change the inheritance you only have to replace
type
TYourForm = class(TForm)
with
type
TYourForm = class(TTemplate)
and replace the object in your DMF's with inherited
{
public
Constructor Create(AOwner:TComponent;AParent:TWinControl=nil);Overload;
.....
}
constructor TTemplate.Create(AOwner: TComponent; AParent: TWinControl);
begin
inherited Create(AOwner);
if Assigned(AParent) then
begin
BorderStyle := bsNone;
Parent := AParent;
Align := alClient;
end;
end;
if you want to embed other form in the your main form put a tpanel in to your main form set alclient property. And when clicked set fromxxx.parent is your panel name.
here is a sample code
begin
if Dm.TblUser.Active=False then
Dm.TblUser.Active := True;
if FrmPUserG=nil then
FrmPUserG := TFrmPUserG.Create(Self);
FrmpUserG.Parent := PnLContainer;
FrmpUserG.Align := alClient;
FrmpUserG.BorderStyle := bsNone;
FrmpUserG.Visible := True;
FrmpUserG.BringToFront;
end;
Related
Hopefully I can articulate this properly. I have a TScrollBox on a form. I am adding instances of another form to a dynamically created panel that I am adding to the TScrollBox , here is the code I am using to add it.
procedure TSettings.AddWFOnclick(Sender: TObject);
var
dlg : TWFDetail;
panel: TPanel;
i : Integer;
begin
panel := TPanel.Create(self);
dlg := TWFDetail.Create(self);
panel.Parent := WFList;
panel.clientheight := dlg.height;
panel.align := alTop;
panel.Top := 330;
panel.Left := 0;
dlg.Parent := panel;
dlg.align := alClient;
dlg.visible := True;
dlg.Show;
end;
The above works beautifully to add my form and panel. As seen here:
The issue occurs when I try to close the dlg and remove the panel. I don't have any sample code for that. I have tried a dozen different things and can't seem to figure it out. I am closing the form with a close call on the click of the red X, then the panel remains. Seen here:
I need to be able to remove the blank panel and shift everything up. I just can't seem to wrap my head around it since the panel is being created dynamically.
The parenting structure is TScrollBox > TPanel > MyForm
Any help would be appreciated.
You can use an TNorifyEvent on TWFDetail form. Define it:
property OnCloseForm:TNotifyEvent read FOnCloseForm write FOnCloseForm;
When you close the form, fire the event if assigned:
Self.Close;
if Assigned(OnCloseForm) then
OnCloseForm(Self);
When you create the form, assign the OnCloseForm event:
...
dlg.OnCloseForm := CloseForm;
...
And define a simple CloseForm procedure to free the panel that you use to contain the form:
var
pnl:TPanel;
begin
if (Sender is TWFDetail) then begin
if TWFDetail(Sender).Parent is TPanel then begin
pnl := TPanel(TWFDetail(Sender).Parent);
pnl.Free;
end;
end;
end;
There are some other ways to to this, but this work fine.
Is there any way to exclude VCL Styles from styling a system dialogs' border.
Sepecifically a dialog that is shown by calling MessageDlg or ShowMessage.
I read some articles on "The Road To Delphi" (which is an excellent site btw) but couldn't find the answer.
Here is what i want to achieve:
Now (Carbon Style with styled borders):
Goal (Carbon Style with standard windows borders):
I still want to have styled controls but no styled border.
Removing seBorder from the parent forms StyleElements doesn't do the trick.
Thanks!
MessageDlg() and ShowMessage() are Delphi VCL functions. They dynamically create a Delphi TForm and display that, so you do not have a chance to customise it. However, you can use CreateMessageDialog() instead to create the same TForm, then modify its style elements as needed, and then show it. For instance:
function DoMessageDlgPosHelp(MessageDialog: TForm; X, Y: Integer): Integer;
begin
with MessageDialog do
try
if X >= 0 then Left := X;
if Y >= 0 then Top := Y;
if (Y < 0) and (X < 0) then Position := poScreenCenter;
Result := ShowModal;
finally
Free;
end;
end;
procedure ShowStyledMessage(const Msg: string; const StyleElements: TStyleElements);
var
Form: TForm;
begin
Form := CreateMessageDialog(Msg, mtCustom, [mbOK]);
Form.StyleElements := StyleElements;
DoMessageDlgPosHelp(Form, -1, -1);
end;
Call it like this:
ShowStyledMessage('Some text', [seFont, seClient]);
And the dialog looks like this:
To have styled form without borders you have to remove seBorder from form's StyleElements property.
StyleElements := [seFont, seClient];
But you have to set that property for each and every form. If I understood you correctly you want to show message dialog with Windows border. In that case setting StyleElements property for form that invokes ShowMessage will have no effect on dialog box, because that is completely new form.
What you have to do is to somehow set StyleElements property for dialog form that Delphi creates out of your reach. To do that you have to create your own form StyleHook and replace TFormStyleHook registered for all forms.
Just add following unit in your project and all forms will have Windows border, without the need to set it explicitly for every form.
unit WinBorder;
interface
uses
Winapi.Windows,
Winapi.Messages,
Vcl.Themes,
Vcl.Controls,
Vcl.Forms;
type
TWinBorderFormStyleHook = class(TFormStyleHook)
protected
procedure WndProc(var Message: TMessage); override;
public
constructor Create(AControl: TWinControl); override;
end;
implementation
constructor TWinBorderFormStyleHook.Create(AControl: TWinControl);
begin
inherited;
OverridePaintNC := false;
end;
procedure TWinBorderFormStyleHook.WndProc(var Message: TMessage);
begin
inherited;
if Message.Msg = CM_VISIBLECHANGED then
begin
if (Control is TCustomForm) and (seBorder in TCustomForm(Control).StyleElements) then
TCustomForm(Control).StyleElements := [seFont, seClient];
end;
end;
initialization
TCustomStyleEngine.UnRegisterStyleHook(TCustomForm, TFormStyleHook);
TCustomStyleEngine.UnRegisterStyleHook(TForm, TFormStyleHook);
TCustomStyleEngine.RegisterStyleHook(TCustomForm, TWinBorderFormStyleHook);
TCustomStyleEngine.RegisterStyleHook(TForm, TWinBorderFormStyleHook);
finalization
TCustomStyleEngine.UnRegisterStyleHook(TCustomForm, TWinBorderFormStyleHook);
TCustomStyleEngine.UnRegisterStyleHook(TForm, TWinBorderFormStyleHook);
TCustomStyleEngine.RegisterStyleHook(TCustomForm, TFormStyleHook);
TCustomStyleEngine.RegisterStyleHook(TForm, TFormStyleHook);
end.
I am dynamically creating a Form that overrides the CreateParams so that I can have it displayed on the TaskBar. From the dynamically created Form, I am calling a TColorDialog but once it is displayed my Form will go under the MainForm with the ColorDialog on top of that.
After the ColorDialog is closed the dynamic Form will return back over the MainForm.
I see that on the ColorDialog Execute method there is a Handle that can be passed but I am not sure if I am on the right track with that?
If I click under the Dialog on the MainForm it will flash but how can I have the dynamically created Form "own" this Dialog with the MainForm at the back?
I create the Form like this:
procedure TMain.Button1Click(Sender: TObject);
var
SEMArcF: TWriteSEMArcFrm;
begin
SEMArcF := TWRiteSEMArcFrm.Create(nil);
SEMArcF.Show;
end;
and it is freed on the OnClose Event:
procedure TWriteSEMArcFrm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := caFree;
end;
I am overriding the CreateParams like this:
procedure TWriteSEMArcFrm.CreateParams(var Params: TCreateParams);
begin
inherited;
if (FormStyle = fsNormal) then begin
Params.ExStyle := Params.ExStyle or WS_EX_APPWINDOW;
Params.WndParent := GetDesktopWindow;
end;
end;
and to show the ColorDialog I either create it or just have a TColorDialog Component on the Form, either way will result in the same. I want it to be owned by the dynamic Form.
EDIT
I now add:
Application.ModalPopupMode := pmAuto;
The full code:
procedure TWriteSEMArcFrm.btnBackColourClick(Sender: TObject);
var
ColorDlg: TColorDialog;
begin
Application.ModalPopupMode := pmAuto;
ColorDlg := TColorDialog.Create(nil);
try
if ColorDlg.Execute then
re.Color := ColorDlg.Color;
finally
ColorDlg.Free;
end;
end;
This works fine but could there be any unusual behaviour by setting this?
Thank you
Chris
TColorDialog derives from TCommonDialog, which has two overloaded versions of Execute() available - the legacy parameterless version that has existed for years, and a newer overload that takes a parent HWND as an input parameter. You are likely calling the former. That overload uses the Handle property of the currently active TForm (only if the TApplication.ModalPopupMode property is not set to pmNone), falling back to the Handle of the MainForm if needed. If you want more control, you should call the other overload directly instead, then you can pass the dynamic form's Handle property as the parameter value.
When we create a component as a custom control and the control is dropped on a panel the control always appears on the form rather than the containing control. How do you set the parent of the custom control in Create so that when the button is dropped on panel the buttons parent is the panel?
TGlassButton = class(TCustomControl)
...
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
...
constructor TGlassButton.Create(AOwner: TComponent);
begin
inherited; ???????????
inherited Create(AOwner); ????????????
Parent := TWinControl( AComponent ); ??????????????
...
end;
The problem is designtime creation not runtime. This works perfectly:
procedure TForm10.FormCreate(Sender: TObject);
begin
GlassButton0 := TGlassButton.Create( Panel1 );
GlassButton0.Parent := Panel1;
GlassButton0.Left := 20;
GlassButton0.Top := 6;
GlassButton0.Width := 150;
GlassButton0.Height := 25;
GlassButton0.Caption := 'Created At RunTime';
end;
DO NOT set the Parent property in the constructor! As others have said, the IDE and DFM streaming systems will assign the Parent automatically AFTER the constructor has exited. If you need to perform operations in your constructor that are dependant on a Parent being assigned, then you need to re-design your component. Override the virtual SetParent() and/or Loaded() methods and do your operations from there instead. And make use of if (csDesigning in ComponentState) then ... checks in places where you can avoid operations that are not actually needed at design-time.
Parents should be set by whomever is creating the control. For controls created at design time, this would be done by the streaming system when the form is created. For controls created at run-time, it should be done when the control is created:
var
Control: TWinControl;
begin
Control := TGlassButton.Create(<Form or Application>);
Control.Parent := <Some other control on the form>;
end;
Please note that in general the form is the owner of all controls on it, regardless of parent-ing. The Parent of a control is / should be the control responsible for painting it: in other words the control in which it is visually located. Ie a Panel, TabSheet, GroupBox or some other container.
This will sound against the nature of MDI.. I need to show a MDI form (FormStyle=fsMdiChild) as modal sometimes. And I also need to access the part between Application.CreateForm and OnShow event of another MDI form, i.e.
Application.CreateForm(Form2,TForm2); // but don't set form2's visible property true.
Form2.caption:='not working example';
Form2.SomeMagicToSetVisibleTrue;
Any ideas?
For your first problem: Add another constructor, for example CreateAsMDI, like this:
constructor TModalAndMDIForm.CreateAsMDI(AOwner: TComponent);
begin
f_blChild := true;
GlobalNameSpace.BeginWrite;
try
inherited CreateNew(AOwner);
if(not(csDesigning in ComponentState)) then begin
Include(FFormState, fsCreating);
try
FormStyle := fsMDIChild;
if(not(InitInheritedComponent(self, TForm))) then
raise Exception.CreateFmt('Can't create %s as MDI child', [ClassName]);
finally
Exclude(FFormState, fsCreating);
end;
end;
finally
GlobalNameSpace.EndWrite;
end;
end;
In the normal constructor just set the variable f_blChild to false and call the inherited create.
You need two more things, rather self explaining:
procedure TModalAndMDIForm.Loaded;
begin
inherited;
if(f_blChild) then
Position := poDefault
else begin
Position := poOwnerFormCenter;
BorderStyle := bsDialog;
end;
end;
//-----------------------------------------------------------------------------
procedure TModalAndMDIForm.DoClose(var Action: TCloseAction);
begin
if(f_blChild) then
Action := caFree;
inherited DoClose(Action);
end;
Now you can call the form modal, if created with the standard constructor, and as MDI child, if created with CreateAsMDI.
If you include this in your form's declaration
property IsChild: boolean read f_blChild;
you can even do things depending on whether the form is an MDI child or not, just interrogating the isChild property.
As for your second problem: do not use Application.CreateForm, but create your form yourself:
Here the two creations for modal and MDI:
//Modal
frmDialog := TMyForm.Create(self);
// Your Code
frmDialog.ShowModal;
frmDialog.Release;
//MDI-Child
frmDialog := TMyForm.CreateChild(self);
// Your code
frmDialog.Show;
I have translated this answer form an article on the site DelphiPraxis.
The simplest method is to create a trivial subclass of the form, and set
FormStyle = fsMDIChild
AND
Form.Visible = False
in the property inspector. This is tried and tested!
At least for Delphi 2007 and 2009 creating the MDI child form invisible is easy. For the early Delphi versions (where it was impossible to set Visible to False in the property inspector) you just have to provide a handler for the OnCreate event and access a protected field of the class:
procedure TMDIChild.FormCreate(Sender: TObject);
begin
FFormState := FFormState - [fsVisible];
end;
This will disable the automatic showing of the MDI child. After you are done with your other initialisations you can simply Show it or set Visible to True.
I will not try to answer your question about modal MDI child forms, as this violates the conventions of the Windows platform.
No answers above actually does the job required. The best answer is wrong as well, because of following:
Opening the first form;
Maximize it;
Now say this form calls another form (mdi);
When it is constructed the same way you will get buggy layout... (it will mdi child, but not maximized, however the first will be still maximized). So a buggy state.
If you really need to decide in run-time whether it's a fsMDIChild or fsNormal you need to apply following approach.
You have form which is stored as fsNormal in design (or vice-versa);
Override the InitializeNewForm method;
Set the value of FFormStyle to fsMDIChild (as shown below).
...
TYourForm = class(TForm)
...
protected
procedure InitializeNewForm; override;
...
procedure TYourForm.InitializeNewForm;
var
FS: ^TFormStyle;
begin
inherited;
if DoYourCheckForMDI then
begin
FS := #FormStyle;
FS^ := fsMDIChild;
end;
end;