moving a form without the title bar

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • HaLo2FrEeEk
    Contributor
    • Feb 2007
    • 404

    #1

    moving a form without the title bar

    I'm making a screenshot program that lets me position a secondary form and press a button in the main form to take a screenshot where the second form is located. I have the second form set to have no border (so no title bar), and to be transparent so I can see what I'm taking a screenshot of. I've set a panel in the middle that uses the SizeAll cursor, to indicate that you click and drag there to move the area around.

    I've got the moving working, using this code:

    Code:
    this.Location = new Point((this.Location.X + e.Location.X), (this.Location.Y + e.Location.Y));
    The problem is that the movement happens from the top-left corner of the panel (which is centered in the form,) and not from where I actually click within the panel. Essentially I want to click at, say, 6x6 in the panel and have my mouse be locked to that position while I'm moving the form around. What happens is I'll click at 6x6 and as soon as I move my mouse a single pixel, the form will snap so that the cursor is at the top-left corner of the panel. How can I make is so that it won't do that snapping to the top-left corner?

    I can't for the life of me figure it out. Any help would be greatly appreciated.

    As a side note, I'm also going to be doing this for resizing the form, and it's gonna do the same thing, snapping the size box (set into the corners of the form) so that the mouse is at the top-left of the size handle.

    Edit: I found a way to make this work, but I'd still like other suggestions. Basically I created a second panel, exactly the same as the main movement panel, and in the same position, but hidden. When I mouse down on the main panel, it gets moved the the x,y coordinates of my mouse then sized down to 0x0, and the secondary panel is made visible. Since I'm moving based on the position of the main panel, and since no matter where I click it'll always be 0x0 (the top-left corner) since I resized, and the secondary panel behind it shows in the exact same place as the original, it looks as though you're grabbing the handle from wherever you clicked it at. Of course, this is a hack and I'd rather do it the right way, if there is such a way, so I'd still love some suggestions.
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    Yea, the problem is that the mouse coordinates are relative to the form, not the screen. It took some playing about, but I got it to work.

    The trick seems to be you need to find the location of the mouse in the form when you first click, then subtract that from your new location to shift the form back to the right location relative to the mouse. Here's my code...

    Code:
        public partial class Form1 : Form
        {
            private bool m_firstClick = false;
            private Point m_firstClickLoc;
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_MouseMove(object sender, MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Left)
                {
                    if (m_firstClick == false)
                    {
                        m_firstClick = true;
                        m_firstClickLoc = new Point(e.X, e.Y);
                    }
    
                    this.Location = new Point(
                        this.Location.X + e.X - m_firstClickLoc.X,
                        this.Location.Y + e.Y - m_firstClickLoc.Y
                        );
                }
                else
                {
                    m_firstClick = false;
                }
            }
        }
    Give that a try and let me know if it works for you.

    Comment

    • HaLo2FrEeEk
      Contributor
      • Feb 2007
      • 404

      #3
      Wow...your code works great, and it's WAY less complicated than mine. I also plugged it into one of my resizing handles (bottom-right) and it worked there as well.

      I ran into a problem when I tried to use the same method with the bottom-left resize handle. When I click on it to change the height, it doesn't move that corner but instead resizes from the bottom-right (that much I expected,) the problem is that when I click and move my mouse even a single pixel to the left or right (up and down works as expected) the growth of the form becomes exponential on the right side the further I move my mouse from the handle.

      It's difficult to explain exactly what's going on, here's a build of my project showing what's happening:

      http://infectionist.com/extras/cshar...creenshot2.zip

      Moving works great, resizing from the bottom-right handle works great, but try resizing from the bottom-left handle and you'll see exactly what I mean.

      I know I have to resize them offset the form's X location based on how far I've resized, but I just can't figure out how to do it. I tried saving the form's coordinates to a point on the mouse down event, then using this code:


      Code:
      this.Location = new Point(formLock.X + e.X - BLLock.X, formLock.Y);
      this.Size = new Size(this.Width + e.X - BLLock.X, this.Height + e.Y - BLLock.Y);
      It didn't work. I'm sure that's wrong, though. Now I'm trying to eliminate the sizing and just nail the movement from the bottom-left handle. It should only move the x position of the form, the y position should be unaffected. What I'm getting is as I move the handle, the form bounces back and forth, farther and farther the more I move the mouse away from the handle.

      This is frustrating.

      EDIT: Well son of a gun, I figured it out. I did have the math wrong. For the bottom-left handle, I was only moving the x position of the form, y stayed static, so I moved it using this code:

      this.Location = new Point(this.Loca tion.X + e.X - BLLock.X, this.Location.Y );

      Then I did the actual resizing:

      this.Size = new Size(this.Width - e.X + BLLock.X, this.Height + e.Y - BLLock.Y);

      Oddly, the top-left corner was the quickest, since I'd already done the math for the top-right and bottom-left, I just reused those bits : )

      Thanks for the help, I wouldn't have gotten it without you.

      Comment

      • GaryTexmo
        Recognized Expert Top Contributor
        • Jul 2009
        • 1501

        #4
        Can you post more of your code for this? To keep things clean, try to reproduce your functionality in a clean project from VS. I can't reproduce your behaviour... I'm not sure how you get resizing when you've got the border style set to none, unless you're putting it back in...?

        If that's the case, you can always set a flag when you detect you're resizing and not do the form move code.

        Comment

        • GaryTexmo
          Recognized Expert Top Contributor
          • Jul 2009
          • 1501

          #5
          Oh, well then, glad you've got it figured out :D

          Comment

          • HaLo2FrEeEk
            Contributor
            • Feb 2007
            • 404

            #6
            In case you're still interested, here's my source code:

            http://infectionist.com/extras/cshar...nshot2_src.zip

            To achieve resizing without a form border I just use panels positioned in the corners of the form, acting as handles. I have a mouse down, mouse up, and mouse move event for each of them. Mouse down turns the boolean on for that handle, telling it that I'm actively resizing from that handle, and also saves my lock point. Mouse up turns the boolean off. Mouse move handles the resizing / moving of the form to simulate resizing. It works quite well actually, I'm rather surprised.

            Here's a compiled version of the same application:

            http://infectionist.com/extras/cshar...nshot2_app.zip

            Comment

            • GaryTexmo
              Recognized Expert Top Contributor
              • Jul 2009
              • 1501

              #7
              That's a little different than what I thought you had, but I see what you're doing now. Neat :)

              Comment

              Working...