How to fix position of Mdichild forms?
Sir, I have 3 mdichild forms. i want to fix the position of all the forms. I mean, i want to prevent user from moving the开发者_如何学C form and form must be displayed at the position fixed by me. How to achieve this.. Please reply... Thanks..
You set up the position of the form by using
this.WindowState = FormWindowState.Normal;
this.StartPosition = FormStartPosition.CenterScreen;
If you have your own co-ordinate fixed the use this
this.Bounds = new Rectangle(new Point(50,50) , this.Size);
To prevent move you may override the OnMove methid of the form
protected override void OnMove(EventArgs e)
{
this.Bounds = this.RestoreBounds;
}
You can force the results of the default WM_NCHITTEST
handler.
The WM_NCHITTEST
message tells Windows on what part of the non-client area of the window the user clicked. This tells Windows that the user e.g. wants to resize the window or clicked the Close button.
You can force the default results so that Windows can't tell the user wants to drag the Window or resize it:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == WM_NCHITTEST)
{
switch ((HitTestValues)m.Result)
{
case HitTestValues.HTBORDER:
case HitTestValues.HTBOTTOM:
case HitTestValues.HTBOTTOMLEFT:
case HitTestValues.HTBOTTOMRIGHT:
case HitTestValues.HTCAPTION:
case HitTestValues.HTGROWBOX:
case HitTestValues.HTLEFT:
case HitTestValues.HTRIGHT:
case HitTestValues.HTTOP:
case HitTestValues.HTTOPLEFT:
case HitTestValues.HTTOPRIGHT:
m.Result = (IntPtr)HitTestValues.HTNOWHERE;
break;
}
}
}
private const int WM_NCHITTEST = 0x84;
enum HitTestValues
{
HTERROR = -2,
HTTRANSPARENT = -1,
HTNOWHERE = 0,
HTCLIENT = 1,
HTCAPTION = 2,
HTSYSMENU = 3,
HTGROWBOX = 4,
HTMENU = 5,
HTHSCROLL = 6,
HTVSCROLL = 7,
HTMINBUTTON = 8,
HTMAXBUTTON = 9,
HTLEFT = 10,
HTRIGHT = 11,
HTTOP = 12,
HTTOPLEFT = 13,
HTTOPRIGHT = 14,
HTBOTTOM = 15,
HTBOTTOMLEFT = 16,
HTBOTTOMRIGHT = 17,
HTBORDER = 18,
HTOBJECT = 19,
HTCLOSE = 20,
HTHELP = 21
}
}
Play around a little bit with the values you want to have in your switch
statement. You could e.g. either disable the minimize/maximize buttons on your form, but you could also add them to the switch
statement.
Try this
private void childForm_LocationChanged(object sender, EventArgs e)
{ this.Location = new Point(x, y); //give fixed postion as you want }
精彩评论