How to fix the position of Mdichild forms?

Sir, I have 3 forms of mdichild. I want to fix the position of all forms. I mean, I want the user not to move the form, and the form should be displayed in the position I fixed. How to achieve this .. Answer please ... Thank you ..

+3
source share
3 answers

try it

private void childForm_LocationChanged (object sender, EventArgs e)

    {

        this.Location = new Point(x, y); //give fixed postion as you want
    }
0
source

You set the position of the form with

this.WindowState = FormWindowState.Normal;
this.StartPosition = FormStartPosition.CenterScreen;

If you have your own coordinate set, use this

this.Bounds = new Rectangle(new Point(50,50) , this.Size);

To prevent movement, you can override the OnMove method of the form

protected override void OnMove(EventArgs e)
        {
            this.Bounds = this.RestoreBounds;
        }
+1
source

WM_NCHITTEST .

The message WM_NCHITTESTtells Windows about which part of the non-client area of ​​the window the user clicked. This tells Windows that the user, for example. wants to resize the window or click the "Close" button.

You can force the default results so that Windows cannot say that 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 bit with the values ​​you want in the instructions switch. You can, for example, either disable the minimize / maximize buttons in your form, but you can also add them to the operator switch.

+1
source

Source: https://habr.com/ru/post/1788415/


All Articles