tags:

views:

139

answers:

4

I am trying to restrict where a form can be moved to on the desktop. Basically I don't want them to be able to move the form off the desktop. I found a bunch of SetBounds functions but they seem to do something that seems very odd to me for the names of the functions and aren't serving my purpose.

A: 

I think,If you set the form border style to none then you could not be able to move that form.

http://msdn.microsoft.com/en-us/library/system.windows.forms.form.formborderstyle%28VS.71%29.aspx

Thillakan
yes, but you can't move it *at all*, which is not what the OP asked for...
Thomas Levesque
+1  A: 

Just handle the Move event, or override OnMove, to make sure the window is in the desktop :

protected override OnMove(EventArgs e)
{
    if (Screen.PrimaryScreen.WorkingArea.Contains(this.Location))
    {
        this.Location = Screen.PrimaryScreen.WorkingArea.Location;
    }
}
Thomas Levesque
A: 

I just made and override of OnLocationChanged. It's crude but works and I was only allowed one day to find a fix so I'm done. Length and width of the form is 544 and 312. Whats the difference between OnMove and OnLocationChanged?

protected override void OnLocationChanged(EventArgs e)
    {
        if (this.Location.X > Screen.PrimaryScreen.WorkingArea.X + Screen.PrimaryScreen.WorkingArea.Width - 544)
        {
            this.SetBounds(0, 0, 544, 312);
        }
        else if(this.Location.X < Screen.PrimaryScreen.WorkingArea.X)
        {
            this.SetBounds(0, 0, 544, 312);
        }
        if (this.Location.Y > Screen.PrimaryScreen.WorkingArea.Y + Screen.PrimaryScreen.WorkingArea.Height - 312)
        {
            this.SetBounds(0, 0, 544, 312);
        }
        else if (this.Location.Y < Screen.PrimaryScreen.WorkingArea.Y)
        {
            this.SetBounds(0, 0, 544, 312);
        }
    }
Groller
+2  A: 

I realize you are not interested in an answer anymore, I'll post a solution anyway. You want to handle the WM_MOVING message and override the target position. Beware that it has side-effects on Win7 and is inadvisable if the user has more than one monitor. Mouse position handling isn't great either. The code:

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication1 {
  public partial class Form1 : Form {
    public Form1() {
      InitializeComponent();
    }
    protected override void WndProc(ref Message m) {
      if (m.Msg == 0x216) { // Trap WM_MOVING
        RECT rc = (RECT)Marshal.PtrToStructure(m.LParam, typeof(RECT));
        Screen scr = Screen.FromRectangle(Rectangle.FromLTRB(rc.left, rc.top, rc.right, rc.bottom));
        if (rc.left < scr.WorkingArea.Left) {rc.left = scr.WorkingArea.Left; rc.right = rc.left + this.Width; }
        if (rc.top < scr.WorkingArea.Top) { rc.top = scr.WorkingArea.Top; rc.bottom = rc.top + this.Height; }
        if (rc.right > scr.WorkingArea.Right) { rc.right = scr.WorkingArea.Right; rc.left = rc.right - this.Width; }
        if (rc.bottom > scr.WorkingArea.Bottom) { rc.bottom = scr.WorkingArea.Bottom; rc.top = rc.bottom - this.Height; }
        Marshal.StructureToPtr(rc, m.LParam, false);
      }
      base.WndProc(ref m);
    }
    private struct RECT {
      public int left; 
      public int top; 
      public int right; 
      public int bottom; 
    }
  }
}
Hans Passant