简体   繁体   中英

How do you prevent a windows from being moved?

How would i go about stopping a form from being moved. I have the form border style set as FixedSingle and would like to keep it this way because it looks good in vista :)

Take a look at this link . You might be interested in option #3. It will require you to wrap some native code, but should work. There's also a comment at the bottom of the link that shows an easier way to do it. Taken from the comment (can't take credit for it, but I'll save you some searching):

protected override void WndProc(ref Message message)
{
    const int WM_SYSCOMMAND = 0x0112;
    const int SC_MOVE = 0xF010;

    switch(message.Msg)
    {
        case WM_SYSCOMMAND:
           int command = message.WParam.ToInt32() & 0xfff0;
           if (command == SC_MOVE)
              return;
           break;
    }

    base.WndProc(ref message);
}

您可以将 Form 的FormBorderStyle属性设置为 None

this.FormBorderStyle=System.Windows.Forms.FormBorderStyle.None

I found this to stop the form from moving (its in c#)

protected override void WndProc(ref Message m)
        {
            const int WM_SYSCOMMAND = 0x0112;
            const int SC_MOVE = 0xF010;

            switch (m.Msg)
            {
                case WM_SYSCOMMAND:
                    int command = m.WParam.ToInt32() & 0xfff0;
                    if (command == SC_MOVE)
                        return;
                    break;
            }
            base.WndProc(ref m);
        }

Found here

Try to override WndProc:

protected override void WndProc(ref Message m)
{
   const int WM_NCLBUTTONDOWN = 161;
   const int WM_SYSCOMMAND = 274;
   const int HTCAPTION = 2;
   const int SC_MOVE = 61456;

   if ((m.Msg == WM_SYSCOMMAND) && (m.WParam.ToInt32() == SC_MOVE))
   {
       return;
   }

   if ((m.Msg == WM_NCLBUTTONDOWN) && (m.WParam.ToInt32() == HTCAPTION))
   {
       return;
   }

   base.WndProc(ref m);
}
  1. Go to form events-> Location changed

write the following code

Location = new Point(this.Width,this.Height);

只需将FormBorderStyle属性更改为None

It's not a good practice to make your form immovable. I'd think agfain about it if I were you.
Anyway, you can do this by overridding the WinProc to disable the [Move] menuitem from the system menu.

[DllImport("user32.dll")]
private static extern Int32 EnableMenuItem ( System.IntPtr hMenu , Int32uIDEnableItem, Int32 uEnable);  
private const Int32 HTCAPTION = 0×00000002;  
private const Int32 MF_BYCOMMAND =0×00000000;  
private const Int32 MF_ENABLED =0×00000000;  
private const Int32 MF_GRAYED =0×00000001;  
private const Int32 MF_DISABLED =0×00000002; 
private const Int32 SC_MOVE = 0xF010; 
private const Int32 WM_NCLBUTTONDOWN = 0xA1;
private const Int32 WM_SYSCOMMAND = 0×112;
private const Int32 WM_INITMENUPOPUP = 0×117;

protected override void WndProc(ref System.Windows.Forms.Message m )
{
if (m.Msg == WM_INITMENUPOPUP)
{
    //handles popup of system menu
    if ((m.LParam.ToInt32() / 65536) != 0) // 'divide by 65536 to get hiword
    {
        Int32 AbleFlags = MF_ENABLED;
        if (!Moveable)
        {
            AbleFlags = MF_DISABLED | MF_GRAYED; // disable the move
        }
        EnableMenuItem(m.WParam, SC_MOVE, MF_BYCOMMAND | AbleFlags);
    }
}
if (!Moveable)
{
    if (m.Msg == WM_NCLBUTTONDOWN) //cancels the drag this is IMP
    {
        if (m.WParam.ToInt32() == HTCAPTION) return;
    }
    if (m.Msg == WM_SYSCOMMAND) // Cancels any clicks on move menu
    {
        if ((m.WParam.ToInt32() & 0xFFF0) == SC_MOVE) return;
    }
}
base.WndProc(ref m);
}  

Also, you can handle OnMove event of your form. But I think this will cause some flickering:

private void Form1_Move(object sender, EventArgs e)
{
    this.Location = defaultLocation;
}

It's not all pretty (there is some flashing going on when you try to move the form), but you can use the LocationChanged property to keep the form where you want it:

private Point _desiredLocation;
// assign the _desiredLocation variable with the form location at some
// point in the code where you know that the form is in the "correct" position


private void Form_LocationChanged(object sender, EventArgs e)
{
    if (this.Location != _desiredLocation)
    {
        this.Location = _desiredLocation;
    }
}

Out of curiousity; why would you want to do this?

In Windows, the WS_CAPTION style is the non-client area that allows your window to be moved with a mouse. So the easiest way to do what you want is to remove this style from your window.

However, if you need to have a caption and still achieve what you want, then the next style would be to capture the WM_NCHITTEST message and check for HTCAPTION. If the code is HTCAPTION, return NTNOWHERE instead. This will prevent the default window procedure from executing the default move window thing.

change the Form property StartPostion to Manual. Then, handle the LocationChanged event:

private void frmMain_LocationChanged(object sender, EventArgs e)
{
Location = new Point(0, 0);
}

只需将 formlocation_changed 事件上的位置重置为原来的位置,即将 Form.Location 设置为移动之前的变量,当用户尝试移动它时,它将返回到您设置的变量位置。

Private Sub MyFormLock() Me.Location = New Point(0, 0) End Sub

Private Sub SearchSDR_LocationChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LocationChanged
    Call MyFormLock()
End Sub

你可以试试:

this.Locked = true;

您可以订阅Form.Move事件并从中重新定位。

I would question your need to make the form unmovable. This doesn't sound nice. You could of course save the location of the window when the window closes and reopen the window into that position. That gives the user some control over where the window should be located.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM