473,500 Members | 1,686 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

moving a form without the title bar

HaLo2FrEeEk
404 Contributor
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:

Expand|Select|Wrap|Line Numbers
  1. 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.
Jul 6 '10 #1
6 5656
GaryTexmo
1,501 Recognized Expert Top Contributor
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...

Expand|Select|Wrap|Line Numbers
  1.     public partial class Form1 : Form
  2.     {
  3.         private bool m_firstClick = false;
  4.         private Point m_firstClickLoc;
  5.  
  6.         public Form1()
  7.         {
  8.             InitializeComponent();
  9.         }
  10.  
  11.         private void Form1_MouseMove(object sender, MouseEventArgs e)
  12.         {
  13.             if (e.Button == MouseButtons.Left)
  14.             {
  15.                 if (m_firstClick == false)
  16.                 {
  17.                     m_firstClick = true;
  18.                     m_firstClickLoc = new Point(e.X, e.Y);
  19.                 }
  20.  
  21.                 this.Location = new Point(
  22.                     this.Location.X + e.X - m_firstClickLoc.X,
  23.                     this.Location.Y + e.Y - m_firstClickLoc.Y
  24.                     );
  25.             }
  26.             else
  27.             {
  28.                 m_firstClick = false;
  29.             }
  30.         }
  31.     }
Give that a try and let me know if it works for you.
Jul 6 '10 #2
HaLo2FrEeEk
404 Contributor
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:


Expand|Select|Wrap|Line Numbers
  1. this.Location = new Point(formLock.X + e.X - BLLock.X, formLock.Y);
  2. 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.Location.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.
Jul 6 '10 #3
GaryTexmo
1,501 Recognized Expert Top Contributor
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.
Jul 6 '10 #4
GaryTexmo
1,501 Recognized Expert Top Contributor
Oh, well then, glad you've got it figured out :D
Jul 6 '10 #5
HaLo2FrEeEk
404 Contributor
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
Jul 6 '10 #6
GaryTexmo
1,501 Recognized Expert Top Contributor
That's a little different than what I thought you had, but I see what you're doing now. Neat :)
Jul 6 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

6
17047
by: Matthew Wells | last post by:
I am trying to load a form (load Me) without the form becoming visible. If I try me.hide or me.visible = false, they load the form and the form blinks before becoming invisible. Is there a way to...
16
3155
by: Picho | last post by:
Hi all, Is there any .NET way (I am not rulling out API usage) to add button(s) to a form's title bar? I found some non-.NET solutions that did actually work in VB6 but not in the ..NET...
14
10099
by: Abhi | last post by:
FYI: This message is for the benefit of MS Access Community. I found that this prblem has been encounterd by many but there is hardly any place where a complete solution is posted. So I thought...
3
2525
by: JavaConvert | last post by:
I would like to create form that simply hold textboxes that I log info to during the execution of a single-thread application. In Java, I found JFrame ideal for the task, but I can't recreate the...
1
1666
by: jos | last post by:
Hi , I have a vb.net form without a Title Bar. How do i program such that i can still drag the window anywhere on my desktop?? Thanks Jots
1
1084
by: Kevin L | last post by:
I want to change the color of a form's title bar depending on whether a particular action is being performed. Is there a way I can do this in .NET?
4
2035
by: Sumit Gupta | last post by:
Hi Can any one tell me how to move a form without titlebar as we can use WIn API in VB 6.0 is there any other method or xyz for the same Sumit
10
5232
by: Sumit Gupta | last post by:
Hi.. Can anyone please tell me how to move a form (using mouse) if its FormBordestyle is none. i.e form without title bar Sumit
2
9994
by: Woo Mun Foong | last post by:
Hi, I would like to change the color and the font of my Form's Title bar. Anyone help ? Thank You, mfwoo
10
9487
nirmalsingh
by: nirmalsingh | last post by:
hi experts, l just want to open a window without title bar,maximize and minimize button, and close button. is there is any way to do like that? if yes, how can i do that?
0
7134
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7180
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7229
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6905
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7395
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5485
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4609
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3103
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1429
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.