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.
How can I make my form unmovable?
Page 1 of 17 Replies - 7537 Views - Last Post: 13 November 2008 - 12:35 PM
Replies To: How can I make my form unmovable?
#2
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?
#3
Re: How can I make my form unmovable?
Posted 12 November 2008 - 07:38 PM
Go to your Form's Locationchanged() Event and type:
Hope this helps
this.Location = this.Location;
Hope this helps
#4
Re: How can I make my form unmovable?
Posted 12 November 2008 - 08:57 PM
#5
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
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
#6
Re: How can I make my form unmovable?
Posted 13 November 2008 - 05:30 AM
gbertoli3, 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
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);
#7
Re: How can I make my form unmovable?
Posted 13 November 2008 - 07:43 AM
I'm Glad you Figured it out.
#8
Re: How can I make my form unmovable?
Posted 13 November 2008 - 12:35 PM
Curiosity strikes, wouldnt doing this be easier:
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.
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.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote






|