7 Replies - 7537 Views - Last Post: 13 November 2008 - 12:35 PM Rate Topic: -----

#1 lordelf2004  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 10-October 08

How can I make my form unmovable?

Post icon  Posted 12 November 2008 - 11:31 AM

I set my form start at Point(0,0) in screen, and I don't want users can move it into another point when they run it . Which properties should I use?
Thanks in advance.
Is This A Good Question/Topic? 0
  • +

Replies To: How can I make my form unmovable?

#2 nofear217  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 12
  • View blog
  • Posts: 323
  • Joined: 08-November 07

Re: How can I make my form unmovable?

Posted 12 November 2008 - 01:56 PM

do you need the form to have a border or title bar?
Was This Post Helpful? 0
  • +
  • -

#3 gbertoli3  Icon User is offline

  • DIC at Heart + Code
  • member icon

Reputation: 40
  • View blog
  • Posts: 1,162
  • Joined: 23-June 08

Re: How can I make my form unmovable?

Posted 12 November 2008 - 07:38 PM

Go to your Form's Locationchanged() Event and type:
this.Location = this.Location;



Hope this helps
Was This Post Helpful? 0
  • +
  • -

#4 lordelf2004  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 10-October 08

Re: How can I make my form unmovable?

Posted 12 November 2008 - 08:57 PM

View Postgbertoli3, on 12 Nov, 2008 - 06:38 PM, said:

Go to your Form's Locationchanged() Event and type:
this.Location = this.Location;



Hope this helps


I tried it, but it didn't work. I also need border and title bar in my form.
Was This Post Helpful? 0
  • +
  • -

#5 gbertoli3  Icon User is offline

  • DIC at Heart + Code
  • member icon

Reputation: 40
  • View blog
  • Posts: 1,162
  • Joined: 23-June 08

Re: How can I make my form unmovable?

Posted 12 November 2008 - 10:54 PM

Well for the Title Bar I suggest you read gabehabe's tutorial.

You could also try putting that code in the MouseDown() Event, that way whenever the mouse is down(includes moving) it will not move.

Hope this helps
Was This Post Helpful? 0
  • +
  • -

#6 lordelf2004  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 10-October 08

Re: How can I make my form unmovable?

Posted 13 November 2008 - 05:30 AM

View Postgbertoli3, on 12 Nov, 2008 - 09:54 PM, said:

Well for the Title Bar I suggest you read gabehabe's tutorial.

You could also try putting that code in the MouseDown() Event, that way whenever the mouse is down(includes moving) it will not move.

Hope this helps

Thanks gbertoli3,

I found this class in Google, it solved my problem.

	/// <summary>
	/// Prevents a form being moved.
	/// </summary>
	public sealed class unmovableForm : NativeWindow
	{
		/// <summary>
		/// Represents all the positional characteristics of a window.
		/// </summary>
		private struct WindowPos
		{
			public int hwnd;
			public int hWndInsertAfter;
			public int x;
			public int y;
			public int cx;
			public int cy;
			public int flags;
		}

		private const int WM_WINDOWPOSCHANGING = 0x46;

		/// <summary>
		/// The form to be immobilised.
		/// </summary>
		private Form target;

		public unmovableForm(Form target)
		{
			this.target = target;
			this.target.HandleCreated += new EventHandler(target_HandleCreated);
			this.target.HandleDestroyed += new EventHandler(target_HandleDestroyed);
		}

		void target_HandleCreated(object sender, EventArgs e)
		{
			// Listent to the target forms message queue.
			AssignHandle(this.target.Handle);
		}

		void target_HandleDestroyed(object sender, EventArgs e)
		{
			ReleaseHandle();
		}

		protected override void WndProc(ref Message m)
		{
			if (m.Msg == WM_WINDOWPOSCHANGING)
			{
				WindowPos pos = (WindowPos)Marshal.PtrToStructure(m.LParam, typeof(WindowPos));

				// Reset the new location to the same as the current location.
				pos.x = this.target.Left;
				pos.y = this.target.Top;

				Marshal.StructureToPtr(pos, m.LParam, true);
			}

			base.WndProc(ref m);
		}
	}




add this code in constructor's form, it worked.

			unmovableForm unmovableform;			
			//Prevent the form being moved.
			unmovableform = new unmovableForm(this);


Was This Post Helpful? 0
  • +
  • -

#7 gbertoli3  Icon User is offline

  • DIC at Heart + Code
  • member icon

Reputation: 40
  • View blog
  • Posts: 1,162
  • Joined: 23-June 08

Re: How can I make my form unmovable?

Posted 13 November 2008 - 07:43 AM

I'm Glad you Figured it out.
Was This Post Helpful? 0
  • +
  • -

#8 indrora  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 100
  • Joined: 25-July 08

Re: How can I make my form unmovable?

Posted 13 November 2008 - 12:35 PM

Curiosity strikes, wouldnt doing this be easier:
point start = new point(0,0);
public myform()
{
  this.Location = start;
}
(Whatever function for handling Locationchanged)
{
  this.location = start;
}


The problem with gbertoli's was this.Location refers to the current location of the form. the compiler auto-optimizes things such as s = s and ignores them. It ignored the this.location = this.location call because it was setting itself to itself.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1