Docking Windows Forms in C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bentleyw
    New Member
    • Feb 2008
    • 32

    Docking Windows Forms in C#

    In a current project I am working on, I have the need to dock forms on each other. For example, you have form1 and when you move form2 close to the borders of it, it attaches itself to form1. Then it could move with form1.

    I would have thought that this would be a built in feature already in .NET, but after doing some brief searches I was not able to find it.

    If anyone knows how to do this automatically in C# .NET, or if you have a different solution on how to accomplish it, please let me (and everyone) know.

    Thanks,
    Bentley.
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    Looks like you found a solution - Attachments on thescripts. Not sure why you thought it would be built-in or is that what you found?

    Comment

    • bentleyw
      New Member
      • Feb 2008
      • 32

      #3
      kenobewan,

      To me, this seems like something many people would have wanted to do before. Also, many applications already incorporate this type of behavior.

      I really like the way that Winamp does it. It's very smooth. The code I wrote to try and duplicate this type of behavior is similar, but a little jumpy when trying to dock. I was hoping to find other people that have run into this same situation of wanting this behavior and who either found some other solution or created one themselves.

      The question of attachments in the forum is still unanswered. I would like to post the simple project I made, but there still is no way to do that here. I'm assuming that it's turned off to prevent people from uploading stuff to each other through this site, but isn't that what the moderators are here for?

      Anyways, back to the subject. Has anyone needed to have forms dock with each other before?

      Comment

      • bentleyw
        New Member
        • Feb 2008
        • 32

        #4
        I've just received instructions on how to attach files to my posts by msquared, so I'll be posting my attempt at solving this issue very soon.

        Comment

        • bentleyw
          New Member
          • Feb 2008
          • 32

          #5
          Solution

          So, here's what I could figure out...

          After searching the internet like a big boy in fat camp looking for cupcakes, I found many articles talking about overriding the WndProc protected method and catching messages from the operating system. This seemed like a great idea but after hours of playing with all of the messages, I was unable to find what I was looking for.

          I found that if the message is WM_NCHITTEST and the WParam is HTCAPTION, then you can catch it and stop the form from going into it's "moving" mode. What I was really looking for was a way to stop the repainting of the form while it was being moved so that then I could repaint it only when the form wasn't in the designated docking distance of the parent form.

          If I kept going down that road, the only way I could see to do it would be to stop the form from going into "moving" mode, then track the mouse movements on the screen with a WIN32 GetCursorPos call, see how that would translate into the hypothetical movement of the form, check the hypothetical new gap distance from the parent form, and end up moving the form manually to its new correct place on the screen.

          Needless to say, I don't want this functionality THAT bad.

          I am attaching to this post my original simple implementation to try and get this functionality that is far easier to understand than all of this was for me. The one drawback is that when you are moving the form within the gap distance of the parent form, it jumps around a little bit because it is redrawing itself. It is a Visual C# 2008 Express project and was just meant to test how this could work for me. I hope it helps someone else out there as well.

          Cheers,
          Bentley.
          Attached Files
          Last edited by bentleyw; Feb 18 '08, 12:25 AM. Reason: Attachment

          Comment

          • bentleyw
            New Member
            • Feb 2008
            • 32

            #6
            Docking Windows Forms in C#

            Well, it appears that I did want it THAT bad. I did some more searching on the net to see how to stop Windows from doing the form move so that I could do it myself programmaticall y. Here is the article that gave me the best clues:

            Build faster with enterprise-ready, AI-powered UI components including blazing fast grids, charts, schedulers, and editors. Get PDF, Word, and Excel document SDKs for Blazor, React, Angular, JavaScript, and .NET MAUI.


            Attached (as soon as I edit this post) is the new version I have been working on for docking one form onto another. It is just a simple example to illustrate the theory and principle of how it can work, so you'll still have to adapt the actual functionality to how you would want it to work in your application. I think it's a good start though.

            The main change from my previous version is that now instead of using the move event of the form, I am catching the Windows messages that are passed to actually do the moving of the form. This way I can keep it from moving when I don't want it to. After intercepting the intial message to start the move, I then move the form the way that I intend it to move.

            Another change is that now the window will dock with the sides of the screen as well as the sides of Form1.

            It appears to work pretty smoothly, but if you find it not working correctly or have another way of doing this same type of thing, I would really like to hear about it.

            Hope it helps someone,
            Bentley.
            Attached Files
            Last edited by bentleyw; Mar 2 '08, 09:55 PM. Reason: Attachment

            Comment

            • bentleyw
              New Member
              • Feb 2008
              • 32

              #7
              DockableForm

              Hello to anybody still interested in this...

              This morning I've published a working class library and source code onto SourceForge of my most recent version of the form docking functionality.

              Download DockableForm for free. A C# .NET implementation of the window docking functionality found in many popular applications. This is my attempt at creating that functionality using many of the pieces I have found on the net.


              One of the issues that still exists in this current implementation is related to the way that I've chosen to implement the functionality. That issue is that sometimes the operating system decides not to fire move events, probably in an effort to maximize efficiency. This can cause the forms to sometimes become undocked. I have coded around the problem a little bit, but I think the best way would be to approach this functionality from another angle. Possibly an external form movement manager. Perhaps I'll work on that later...

              If you'd like to play around with the source code, feel free to go to the SourceForge link above to download it. I will attach to this post a goofy little demo I made of this functionality that you can take a look at (or maybe your kids would be amused by it). :)

              Cheers,
              Bentley.
              Attached Files
              Last edited by bentleyw; Mar 23 '08, 11:34 PM. Reason: Attachment

              Comment

              • altafr
                New Member
                • Apr 2010
                • 1

                #8
                Source code for the test program

                Hi bentlyw,

                Thanks for posting the code on docking windows. I would like to get the source code for the test application you posted using your component.

                I would like to use this for one of the projects we are working on where there are a lot many windows floating on the desktop. From what i understand that the "connectfor m" method needs to be called from within one window to connect to the other ? I am still a bit unclear on how to use this dockign library ? please help ?

                Comment

                • cnqt
                  New Member
                  • Apr 2010
                  • 1

                  #9
                  hi bentleyw,

                  i have only one form in my application and i want it to dock it to one of the nearest borders of the screen (top, left, right or bottom when i drag near to them).

                  so i think i don't need to give a parameter to ConnectForm(); function?
                  how can i do it?

                  Comment

                  • Vijay Kamat

                    #10
                    Dear bentleyw,
                    Great work! This is exactly what I was looking for. :)

                    Is this a free software? Can I use it without any concerns?

                    Comment

                    • bentleyw
                      New Member
                      • Feb 2008
                      • 32

                      #11
                      Answers to Questions

                      Sorry for the very late reply to a couple of you. I was under the assumption that it would email me if someone posted on this topic, but it looks like I didn't have that option enabled...

                      altafr - Yes, the ConnectForm method basically just adds each form into the other's list of forms to dock with. So, if you have 5 forms that you want to dock with each other, then you just need to make sure each pair of forms is connected at least once. Multiple connects of the same pair are ignored. Feel free to contact me on my blog (link below) if you still would like the test form code or have any other questions.

                      cnqt - Unfortunately, I put the code for docking to the sides of the screen inside the looping of the connected forms, so the only time it looks for the sides of the screen is if there are more than one form and connected. But, I made another form type after the DockableForm called BouncingForm, and I believe that will do what you're looking for. Try using that one in place of the DockableForm and set the FormBouncingOn property to false, and see if that gets you what you want. http://sourceforge.net/projects/bouncingform/

                      Vijay Kamat - Yep, it's all free and feel free to use however you like. I, of course, accept no liability for its use, though. You can either compile as a library and include in your project, or just throw the form code files in with your own (usually my preference). If you have any other questions, feel free to get a hold of me on my blog.

                      Thanks,
                      Bentley Wolfe

                      Comment

                      Working...