Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. move form

move form

Scheduled Pinned Locked Moved C#
graphicsquestion
8 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rob Tomson
    wrote on last edited by
    #1

    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.

    H 1 Reply Last reply
    0
    • R Rob Tomson

      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.

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      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

      R 1 Reply Last reply
      0
      • H Heath Stewart

        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

        R Offline
        R Offline
        Rob Tomson
        wrote on last edited by
        #3

        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.

        H 1 Reply Last reply
        0
        • R Rob Tomson

          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.

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          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 call base.OnMouseMove if you've reached your MdiParent's extents. This might work, so long as it cancels the underlying WM_MOUSEMOVE notification message handler. I highly doubt it since the MouseMove event is fired in response to the mouse moving. In this case, try overriding the child form's WndProc and before calling base.WndProc, handle the WM_MOUSEMOVE notification message (0x0200) and just return without doing anything if you've reached your MdiParent's extents.

          Microsoft MVP, Visual C# My Articles

          R 1 Reply Last reply
          0
          • H Heath Stewart

            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 call base.OnMouseMove if you've reached your MdiParent's extents. This might work, so long as it cancels the underlying WM_MOUSEMOVE notification message handler. I highly doubt it since the MouseMove event is fired in response to the mouse moving. In this case, try overriding the child form's WndProc and before calling base.WndProc, handle the WM_MOUSEMOVE notification message (0x0200) and just return without doing anything if you've reached your MdiParent's extents.

            Microsoft MVP, Visual C# My Articles

            R Offline
            R Offline
            Rob Tomson
            wrote on last edited by
            #5

            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.

            H 1 Reply Last reply
            0
            • R Rob Tomson

              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.

              H Offline
              H Offline
              Heath Stewart
              wrote on last edited by
              #6

              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 the WM_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 changed WM_MOUSEMOVE that I posted before to WM_NCMOUSEMOVE for the non-client area of the dialog...what was I think?! :)

              Microsoft MVP, Visual C# My Articles

              R 1 Reply Last reply
              0
              • H Heath Stewart

                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 the WM_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 changed WM_MOUSEMOVE that I posted before to WM_NCMOUSEMOVE for the non-client area of the dialog...what was I think?! :)

                Microsoft MVP, Visual C# My Articles

                R Offline
                R Offline
                Rob Tomson
                wrote on last edited by
                #7

                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 is 0x00a0 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.

                H 1 Reply Last reply
                0
                • R Rob Tomson

                  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 is 0x00a0 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.

                  H Offline
                  H Offline
                  Heath Stewart
                  wrote on last edited by
                  #8

                  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

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups