move form
-
if i have a mdi child form within a mdi form and i want to restrict the child so it can't move outside of the parent form, how would i do this? this is what i have but when i'm moving the child form i get some weird painting effects.
protected override void OnMove(System.EventArgs e) { if (this.Left <= this.Parent.Left) this.Location = new System.Drawing.Point(this.Parent.Left, this.Location.Y); if (this.Right >= this.Parent.Right - 3) this.Location = new System.Drawing.Point(this.Parent.Right - this.Size.Width - 4, this.Location.Y); if (this.Top <= this.Parent.Top) this.Location = new System.Drawing.Point(this.Location.X, this.Parent.Top); if (this.Bottom >= this.Parent.Bottom - 3) this.Location = new System.Drawing.Point(this.Location.X, this.Parent.Bottom - this.Size.Height - 4); base.OnMove(e); }
thanks, Rob Tomson -- There are 10 kinds of people. Those who understand binary and those who don't. -
if i have a mdi child form within a mdi form and i want to restrict the child so it can't move outside of the parent form, how would i do this? this is what i have but when i'm moving the child form i get some weird painting effects.
protected override void OnMove(System.EventArgs e) { if (this.Left <= this.Parent.Left) this.Location = new System.Drawing.Point(this.Parent.Left, this.Location.Y); if (this.Right >= this.Parent.Right - 3) this.Location = new System.Drawing.Point(this.Parent.Right - this.Size.Width - 4, this.Location.Y); if (this.Top <= this.Parent.Top) this.Location = new System.Drawing.Point(this.Location.X, this.Parent.Top); if (this.Bottom >= this.Parent.Bottom - 3) this.Location = new System.Drawing.Point(this.Location.X, this.Parent.Bottom - this.Size.Height - 4); base.OnMove(e); }
thanks, Rob Tomson -- There are 10 kinds of people. Those who understand binary and those who don't.I'm not sure of some auto-magical way to accomplish this, but what you're doing is roughly what needs to happen somewhere down the call stack anyway. So, what kind of "weird painting" effects are you getting? Is there a lot of flickering, or is something else going on (like your VCR time gets reset :))?
Microsoft MVP, Visual C# My Articles
-
I'm not sure of some auto-magical way to accomplish this, but what you're doing is roughly what needs to happen somewhere down the call stack anyway. So, what kind of "weird painting" effects are you getting? Is there a lot of flickering, or is something else going on (like your VCR time gets reset :))?
Microsoft MVP, Visual C# My Articles
my vcr time is just fine but what i am getting is bad flickering. it flashes back and forth from where the form is suppose to be to where i am trying to put it. so is there a way to disable the mouse down or the form painting when it reaches the boundaries of the parent form? thanks, rob tomson -- There are 10 kinds of people. Those who understand binary and those who don't.
-
my vcr time is just fine but what i am getting is bad flickering. it flashes back and forth from where the form is suppose to be to where i am trying to put it. so is there a way to disable the mouse down or the form painting when it reaches the boundaries of the parent form? thanks, rob tomson -- There are 10 kinds of people. Those who understand binary and those who don't.
I see. The form is still trying to go where you put it but when your code executes it tries to put it back to where you want it to be. You definitely don't want to stop painting, but you could try to override
OnMouseMove
and do NOT callbase.OnMouseMove
if you've reached yourMdiParent
's extents. This might work, so long as it cancels the underlyingWM_MOUSEMOVE
notification message handler. I highly doubt it since theMouseMove
event is fired in response to the mouse moving. In this case, try overriding the child form'sWndProc
and before callingbase.WndProc
, handle theWM_MOUSEMOVE
notification message (0x0200) and just return without doing anything if you've reached yourMdiParent
's extents.Microsoft MVP, Visual C# My Articles
-
I see. The form is still trying to go where you put it but when your code executes it tries to put it back to where you want it to be. You definitely don't want to stop painting, but you could try to override
OnMouseMove
and do NOT callbase.OnMouseMove
if you've reached yourMdiParent
's extents. This might work, so long as it cancels the underlyingWM_MOUSEMOVE
notification message handler. I highly doubt it since theMouseMove
event is fired in response to the mouse moving. In this case, try overriding the child form'sWndProc
and before callingbase.WndProc
, handle theWM_MOUSEMOVE
notification message (0x0200) and just return without doing anything if you've reached yourMdiParent
's extents.Microsoft MVP, Visual C# My Articles
wow, you lost me there. i don't know the language well enough to do what you're suggesting. i guess i'll just put it back to the default, where the form is allowed to go off screen. thanks for you help though, i really appreciate it. thanks, Rob Tomson -- There are 10 kinds of people. Those who understand binary and those who don't.
-
wow, you lost me there. i don't know the language well enough to do what you're suggesting. i guess i'll just put it back to the default, where the form is allowed to go off screen. thanks for you help though, i really appreciate it. thanks, Rob Tomson -- There are 10 kinds of people. Those who understand binary and those who don't.
Consider this a learning experience, then! :) Have you ever done MFC or even just straight win32 programming with the Windows Management APIs? If so, you know about all you need to know (undering marshaling and P/Invoke will be required a little, too, but nothing extraordinary. In your class for which you want to override the behavior, just do something like this:
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_NCMOUSEMOVE)
{
// Determine if window should move. If not just return.
if (Left <= MdiParent.Left || ...) return;
}
base.WndProc(ref m);
}
private const int WM_NCMOUSEMOVE = 0x00a0;Like I said, though, I'm really not sure this will work, although I have more hope for this than for override
OnMouseMove
since that happens in response to theWM_NCMOUSEMOVE
notification message and can't be canceled. This little line just determines if your window can move anymore and throws out the message if it can't. You might also notice I changedWM_MOUSEMOVE
that I posted before toWM_NCMOUSEMOVE
for the non-client area of the dialog...what was I think?! :)Microsoft MVP, Visual C# My Articles
-
Consider this a learning experience, then! :) Have you ever done MFC or even just straight win32 programming with the Windows Management APIs? If so, you know about all you need to know (undering marshaling and P/Invoke will be required a little, too, but nothing extraordinary. In your class for which you want to override the behavior, just do something like this:
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_NCMOUSEMOVE)
{
// Determine if window should move. If not just return.
if (Left <= MdiParent.Left || ...) return;
}
base.WndProc(ref m);
}
private const int WM_NCMOUSEMOVE = 0x00a0;Like I said, though, I'm really not sure this will work, although I have more hope for this than for override
OnMouseMove
since that happens in response to theWM_NCMOUSEMOVE
notification message and can't be canceled. This little line just determines if your window can move anymore and throws out the message if it can't. You might also notice I changedWM_MOUSEMOVE
that I posted before toWM_NCMOUSEMOVE
for the non-client area of the dialog...what was I think?! :)Microsoft MVP, Visual C# My Articles
i'm always ready to learn. unforunately i havn't worked with MFC or windows management APIs. i've tried everything i can think of to get this to work and i tried playing around with the code you gave me but it seems like that code executes at odd times. i had it update a textbox when it drops into the if statement before it returns and it doesn't update it in the same spot everytime, it's weird. i would like to know where you got the
WM_NCMOUSEMOVE = 0x00a0
though. what does this mean and why is0x00a0
an int? if anyone out there has any input for constricting the movement of a form to stay within it's parent that would be great. thanks, Rob Tomson -- There are 10 kinds of people. Those who understand binary and those who don't. -
i'm always ready to learn. unforunately i havn't worked with MFC or windows management APIs. i've tried everything i can think of to get this to work and i tried playing around with the code you gave me but it seems like that code executes at odd times. i had it update a textbox when it drops into the if statement before it returns and it doesn't update it in the same spot everytime, it's weird. i would like to know where you got the
WM_NCMOUSEMOVE = 0x00a0
though. what does this mean and why is0x00a0
an int? if anyone out there has any input for constricting the movement of a form to stay within it's parent that would be great. thanks, Rob Tomson -- There are 10 kinds of people. Those who understand binary and those who don't.I got the constant value from winuser.h, the header with most of the constants defined for the Windows Management and related APIs. You can easily find these if you download and install the Platform SDK, which does get installed by default with VS.NET (though it's not pretty old). Why is 0x00a0 an
int
? You've never seen this notation? It's a simple hexidecimal notation, or base16, which is 0123456789abcdef. So, 10 (a) * 16 = 160.int
(Int32
) is only a 32-bit number, as is 0xXXXX (four alphanumeric characters). I typically keep the hexidecimal notation that's defined with the C/C++ headers from the PSDK, though, because it makes lookups easier and lets me format my code better.Microsoft MVP, Visual C# My Articles