Delphi Modal Form Position - delphi

All the modal forms are displayed at Left Top of the screen while the setting are as Follows
BorderIcons = [biSystemMenu]
BorderStyle = bsSingle
Position = poOwnerFormCenter
Earlier it used to be display as per setting but recently i made some changes which causes the problem
Let me explain further so you can suggest appropriate solution.
My application has almost more than 50 forms and i open them as CustomerForm.Show/ShowModal.
All forms are inherited from one root form which has following code to display icon on task bar
procedure TBaseForm.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
Params.ExStyle := Params.ExStyle or WS_Ex_AppWindow;
Params.WndParent := GetDesktopwindow;
end;
There was one problem that whenever a file open or file save dialog was opened from any form(whether it is modal form or not) , Main form was coming at Top, to fix this i made a Dummy Main Form and
put Application.ShowMainForm := false; in project file and this worked fine but this started all modal form appearing at Left Top Corner of the screen.
Can you please suggests on this?

As you are using the same ancestor for all your windows, you can add your own public function ShowModal with a parameter Parent: TYourForm.
In this method, you get the position of the Parent, calculate the center, and you move your modal window to its center. After, you call the real ShowModal in your own...

Add this to the create of your main form:
SetWindowLong(Handle, GWL_EXSTYLE, GetWindowLong(Handle, GWL_EXSTYLE) or
WS_EX_TOPMOST);

Related

Vcl Style Utils - Get rid of default form icon

I've already posted this as an issue on RRUZ's Vcl Style Utils library on GitHub. However, I thought I could get some help here too.
I'm using VCL Styles to create a Windows 10 user interface, specifically using the "Windows 10 Dark" style. I'm also using the VCL Style Utils to be able to add buttons to the non-client area in the title bar. I'm attempting to completely disregard the form icon and its default functionality in favor of a back button, just like most new Windows 10 apps do.
I'm trying to place a button in the far upper-left corner of the form, using the TNCControls component in Vcl.Styles.NC. However, when I place a button over the form's icon, the button cannot be clicked in the area of the icon. Although I'm able to overlap the icon, clicking in that particular area of the title bar always opens the form's system menu, instead of clicking the button I've placed there.
I do not wish for this menu to pop up when clicking there:
How I'm currently creating this button:
procedure TfrmTestMain.SetupTitleBar;
var
B: TNCButton;
begin
FNCControls:= TNCControls.Create(Self);
B:= FNCControls.ButtonsList.Add;
B.Style := TNCButton.TNCButtonStyle.nsTranparent;
B.BoundsRect := Rect(0, 0, 45, 32);
B.UseFontAwesome:= True;
B.Caption := '';
B.ImageAlignment:= TImageAlignment.iaCenter;
B.ImageStyle:= TNCButton.TNCImageStyle.isNormal;
B.ImageIndex:= fa_chevron_left;
end;
What I've tried so far:
Replaced the form's Icon with a completely empty .ico file.
Changing form style to bsSizeToolWin, but the title bar becomes too small, and I lose the minimize / maximize buttons.
Changing form style to bsDialog, but I get the same effect as #2 above, as well as not being able to resize the form.
Made sure button style is nsPushButton, and although it covers up the form icon, clicking the area still clicks the icon, which in turn shows the default system menu.
Following everything in this thread, but the conclusion is that Windows forces you to have this icon.
Removed biSystemMenu from the form's BorderIcons property, but this also removes the default buttons in the top-right of the form, forcing me to place my own system buttons there.
How do do I completely eliminate the form icon and its default functionality in favor of my Windows 10 style back button?
The TNCControls component includes the ShowSystemMenu property. If you set the value to false, then the system menu will be not shown.
Try this
uses
Vcl.Styles.Utils.Graphics;
procedure TfrmTestMain.FormCreate(Sender: TObject);
begin
SetupTitleBar;
end;
procedure TfrmTestMain.NCClick(Sender: TObject);
begin
ShowMessage('Hello');
end;
procedure TfrmTestMain.SetupTitleBar;
var
B: TNCButton;
begin
FNCControls:= TNCControls.Create(Self);
FNCControls.ShowSystemMenu := False; //Disable the system menu.
B := FNCControls.ButtonsList.Add;
B.Style := TNCButton.TNCButtonStyle.nsTranparent;
B.BoundsRect := Rect(0, 0, 45, 32);
B.UseFontAwesome:= True;
B.Caption := '';
B.ImageAlignment:= TImageAlignment.iaCenter;
B.ImageStyle:= TNCButton.TNCImageStyle.isNormal;
B.ImageIndex:= fa_chevron_left;
B.OnClick := NCClick;
end;

How do I tile MDI children and maintain BorderStyle = bsNone?

I am hoping someone can help
I have created an object that has a form. I instantiate this object during runtime. The user can then instantiate a second (or multiple) object(s) via a menu step. So two child forms now exist in a parent form. Each objects form's BorderStyle is set to none and Style is set to MDIChild during form creation at runtime:
BorderStyle := bsNone;
FormStyle := fsMDIChild;
All fine. Form has no border.
Now I want to tile the two forms created. Menu > click Tile
procedure TMainForm.Tile1Click(Sender: TObject);
begin
Tile;
end;
Now the border re-appears. I need a way to get rid of the border.
I have tried stepping through the collection of objects and explicitly setting the BorderStyle to none:
procedure TMainForm.Tile1Click(Sender: TObject);
var
i: byte;
begin
Tile;
for i := 0 to GraphCollection.Count-1 do
(GraphCollection.Items[i] as TGraphForm).BorderStyle := bsNone;
end;
This does not work.
Does anyone have a workaround that retains the bsNone style after either Tile or Cascade.
You can do this in another way. I do this with Tabset (TTabSet class) without using fsMDIChild(but BorderStyle = bsNone).
You can add a tab for each form you open on TabSet. By clicking any tab, you can activate or show appropriate form. And delete the appropriate tab for any form by closing it. I prepare an example and you can get it from here.
(I miss-understand the question, my answer is about simulating MDITab for MdiChild forms when the FormStyle is fsNormal and not fsMdiChild)

How can I create a Delphi child window that has a main menu?

I am working with a Delphi form that contains a TMainMenu. In a particular situation I want to show this form parented and client-aligned inside another form. This works fine but the main menu of the parented form does not appear. I see a comment in this SO question that states "A child window cannot have a menu". Is there anything that I can do to override this behaviour and make a TMainMenu appear?
An aside: I've only just noticed this because where I have used this principle before, I've been using the Developer Express menu component and this is quite happy to show in a child form.
Later edit:
Using the code from TLama below, this works (but the child menu is not themed, I,e very plain):
This works:
procedure TForm65.FormShow(Sender: TObject);
begin
Winapi.Windows.SetParent(ChildForm.Handle, Handle); // <<<<<<<<
ChildForm.BorderStyle := bsNone;
ChildForm.Align := alClient;
ChildForm.Show;
end;
This code DOES NOT work. Why?
procedure TForm65.FormShow(Sender: TObject);
begin
ChildForm.Parent := Self; // <<<<<<<<<
ChildForm.BorderStyle := bsNone;
ChildForm.Align := alClient;
ChildForm.Show;
end;
MSDN makes this perfectly clear:
A child window has a client area but no other features, unless they are explicitly requested. An application can request a title bar, a window menu, minimize and maximize buttons, a border, and scroll bars for a child window, but a child window cannot have a menu.
This refers to the menu as drawn by Windows itself. If your component custom draws a menu bar, then of course it can have a menu, even if it is a child window.
Your call to SetParent does not make your window into a child window. This is explained in the documentation:
For compatibility reasons, SetParent does not modify the WS_CHILD or WS_POPUP window styles of the window whose parent is being changed. Therefore, if hWndNewParent is NULL, you should also clear the WS_CHILD bit and set the WS_ POPUP style after calling SetParent. Conversely, if hWndNewParent is not NULL and the window was previously a child of the desktop, you should clear the WS_POPUP style and set the WS_CHILD style before calling SetParent.

Resize won't execute untill I manually call ClientHeigh or until I manually resize it

I want to create a custom control derived from TPanel that contains an image and a bunch of other controls on it.
After writing the code I have some weird behavior in my program. I realized that some vars that were supposed to be initialized in TDisplay.Resize (override) was never initialized because the Resize was never executed.
To 'solve it' I put a button on a form and called the LoadSample function which calls ClientHeight which calls Resize FOR THE FIRST TIME!
constructor TDisplay.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Ready := FALSE;
Parent := Owner as TWinControl;
Width := 200;
Height := 86;
Color := clSilver;
Caption := '';
DoubleBuffered:= TRUE;
InternalDisplay:= TImage32.Create(Self);
with Display DO
begin
Parent := Self;
Bitmap.Width := 1;
Bitmap.Height := 1;
RepaintMode := rmOptimizer;
Align := alClient;
SetupBitmap(TRUE, clBlack32);
Visible := TRUE;
OnMouseDown := DMouseDown;
end;
...
end;
Update:
Also the InternalDisplay won't be aligned to its parent size until I manually resize the form (the control) at runtime. Only then it will act as it was supposed to act (to stay aligned to alClient).
Update 2:
Resize is declared like that: procedure Resize; override;
Update 3:
I removed the ClientHeight line from my construnctor and move it here:
procedure TDisplay.LoadSample(VAR Obj: TMySample; CONST bReleaseOnExit: boolean)
begin
ClientHeight; <--------- this will call Resize for the first time and my code will be finally initialized. But until this point my display will look odd because the code was never initialized. So the user will see weird stuff until it pushes the 'LoadSample' button.
more code here....
end;
Update 4:
I used HandleNeeded as David suggested and it solved the initialization problem. However, the Image still won't align to the entire client area unless I manually resize the form/control.
Update 5
Continued here, as David suggested: TImage won't align to parent
Your control is derived from TWinControl, and TWinControl calls Resize in response to the WM_SIZE message. So, Resize will not be called unless the control's window handle has been created.
The Resize method is not called when you assign Height, or indeed Width, because the window handle has not yet been allocated.
When you evaluate the ClientHeight property, that results in the window handle being created, and then Resize is called. That's because GetClientHeight calls GetClientRect which looks like this:
function TWinControl.GetClientRect: TRect;
begin
Winapi.Windows.GetClientRect(Handle, Result);
end;
And it's the evalutation of the Handle property that forces the window handle into existence.
Your form isn't showing yet, so it isn't yet able to receive Windows messages (such as the resize message that triggers the OnResize event).

Problem while using Two Forms

i don't know how to ask this question. problem is given below.
I'm using One Main form and many sub forms but not MDI Forms.
Main form Contains 5 Buttons and a Panel. each button will call a form
inside that Panel(as Parent). in that sub forms, one form(Sub3) contain TMainMenu component.
every form is working correctly while calling by clicking the buttons but, while calling the form(Sub3) the TMainMenu is not in visible. i don't know how to bring it visible.
Please help me any one.
Thanks in Advance.
Thanks & Regards,
Yuvaraj
You can only have one MainMenu on each form. While you can have multiple forms in an app each with its own MainMenu, if you show one form within another form, only the mainmenu of the "outer" form will be visible.
When you "reparent" a form to another (show formB as a "component" on formA), then you have to merge the menu's yourself as #skamradt already mentioned.
To do so, simply have your buttons use a "SwitchToForm" function like:
type
TMain_Form
...
private
FCurrentForm: TForm;
procedure SwitchToForm(showForm: TForm);
...
end;
procedure TMain_Form.SwitchToForm(showForm: TForm);
begin
if (FCurrentForm <> nil) and (FCurrentForm.Name = showForm.Name) then begin
// Naught to do
end else begin
// If a form is currently showing, hide it and if it has a menu, unmerge that
if FCurrentForm <> nil then
begin
FCurrentForm.Hide;
if Assigned(FCurrentForm.Menu) then
begin
MainMenu.UnMerge(FCurrentForm.Menu);
end;
end;
// Set the current form to the one passed in and re-parent that to the main form
// If the form has a menu, merge that with the main menu of the main form and then
// show it.
FCurrentForm := showForm;
with FCurrentForm do begin
Parent := self;
Align := alClient;
BorderIcons := [];
BorderStyle := bsNone;
end;
if Assigned(FCurrentForm.Menu) then begin
MainMenu.Merge(FCurrentForm.Menu);
end;
FCurrentForm.Show;
end;
end;
In this example: the form is parented to the main form itself, but you could of course also parent the forms to a panel or some other container on the main form.
You can use the TMainMenu.Merge and TMainMenu.Unmerge to merge/unmerge the sub-form menu with the main form menu. In your "OnActivate" for each of the child forms, I would send a custom message to the main form that would unmerge any menus from another form (which might already not be set) and merge the menu for the current form.

Resources