问题
i have 2 forms (parent & child)
child form's properties are ControlBox:False
, DoubleBuffer:True
& FormBorderStyle:SizeableToolWindow
(rest all are default values).
i want child form to resize from center (and not from top left)
In Child form Resize
Event, i have following code
this.Location = System.Drawing.Point(x / 2 - this.Width / 2, y / 2 - this.Height / 2);
//tried this.CenterToParent();`
where x = parent form's width and y = parent form's height.
now after showing child form from parent form, while resizing it flickers a lot, child form tends to go back to its original location!
how to create smooth resize from center?
is there a way to change Form's resize origin to center?
this question is already posted here, but didn't understand solution
回答1:
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
protected override void OnResize(EventArgs e)
{
this.Location = new System.Drawing.Point(this.Location.X / 2 - this.Width / 2, this.Location.Y / 2 - this.Height / 2);
//tried this.CenterToParent();`
base.OnResize(e);
}
const int WM_SYSCOMMAND = 0x112;
const int SC_MAXIMIZE = 0xF030;
const int SC_MAXIMIZE2 = 0xF032;
protected override void WndProc(ref Message m)
{
if ((m.Msg == WM_SYSCOMMAND && m.WParam == new IntPtr(SC_MAXIMIZE)) || m.WParam == new IntPtr(SC_MAXIMIZE2))
{
this.Size = this.MaximumSize;
m.Result = new IntPtr(0);
//base.WndProc(ref m);
//Eat the message so won't process further
}
else
{
m.Result = new IntPtr(0);
base.WndProc(ref m);
}
}
}
}
Here's the better link: https://msdn.microsoft.com/en-us/library/system.windows.forms.control.wndproc(v=vs.110).aspx
来源:https://stackoverflow.com/questions/43502292/resize-from-center-winforms