How to make WinForms so that one form is positioned inside another form and when the size of the main form changes, the size of the other form (child) changes?
1 answer
If it were not for the strange limitations of the framework, you could just write childForm.Parent = parentForm
, but for some reason this is prohibited. But there is no reception against the scrap, just use the SetParent
function. Well and further it is banal: we create the child form, we process the SizeChanged
event:
partial class Form1 : Form { Form _child; public Form1 () { InitializeComponent(); _child = new Form { StartPosition = FormStartPosition.Manual, Left = 20, Top = 20, Width = ClientSize.Width - 40, Height = ClientSize.Height - 40, }; _child.Show(this); SetParent(_child.Handle, Handle); } protected override void OnSizeChanged (EventArgs e) { base.OnSizeChanged(e); _child.Width = ClientSize.Width - 40; _child.Height = ClientSize.Height - 40; } [DllImport ("user32.dll", SetLastError = true)] static extern IntPtr SetParent (IntPtr hWndChild, IntPtr hWndNewParent); }
- What is the use of user32.dll in your solution? - Vladimir
- @Vladimir To call SetParent . - Athari
|