Code to move window not working.
-
Ok, I've gotten it to move horizontally, vertically and diagonally fine so i've added boundaries so that it can't move past the screen limits. However when i added that it just shakes back and forth. From what i understand it first increases the x co-ordinates, moving the window to the right. Then the if statement checks if it has gone past the left of the screen and if it has it will reverse the direction the window moves. The second if statement checks that x co-ordinates including the width of the window doesn't past the width of the screen. Is this all correct?
private void timer4_Tick (object sender, EventArgs e)
{
this.Left += WindowVx;
if (this.Left < 0) {
WindowVx = -WindowVx;
}
if (this.Left + this.Width > ClientSize.Width) {
WindowVx = -WindowVx;
}
this.Refresh ();
}So what you want it to do is move all the way right, then when it reaches the edge, turn round and move the other way? To do that isn't difficult - when you reach the screen edge, change the sign on the value you are adding! Then, when you reach the other edge, change it back again.
Left += deltaX;
if (Left < 0 || (Left + Width) > ClientSize.Width)
{
deltaX = -deltaX;
}You don't need to specify
this
all the time - you only need it when you have a local variable of the same name as your class level one and you want to access the class level variable. You also don't need to refresh anything - when you update Left and Top they will refresh for you.Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
So what you want it to do is move all the way right, then when it reaches the edge, turn round and move the other way? To do that isn't difficult - when you reach the screen edge, change the sign on the value you are adding! Then, when you reach the other edge, change it back again.
Left += deltaX;
if (Left < 0 || (Left + Width) > ClientSize.Width)
{
deltaX = -deltaX;
}You don't need to specify
this
all the time - you only need it when you have a local variable of the same name as your class level one and you want to access the class level variable. You also don't need to refresh anything - when you update Left and Top they will refresh for you.Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
Hm... for some reason the window just moves back and forth kind of like it's vibrating. I'm not really sure what's causing it as the code without the if statement works fine. This is the code right now:
this.Left += WindowVx; if (Left < 0 || (Left + this.Width) > ClientSize.Width){ WindowVx = -WindowVx; }
-
Hm... for some reason the window just moves back and forth kind of like it's vibrating. I'm not really sure what's causing it as the code without the if statement works fine. This is the code right now:
this.Left += WindowVx; if (Left < 0 || (Left + this.Width) > ClientSize.Width){ WindowVx = -WindowVx; }
Check your "ClientSize" object and see what values it has - at a guess the Width is zero. You know how to use the debugger, yes?
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
Check your "ClientSize" object and see what values it has - at a guess the Width is zero. You know how to use the debugger, yes?
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
I checked and the width is 282 i think. And by debugger you mean like the play button on xamarin studio? That's what i've been using to test my program. Also just in case i'll post the rest of my form information.
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(282, 253); this.ControlBox = false; this.Controls.Add(this.webBrowser1); this.KeyPreview = true; this.MaximizeBox = false; this.MinimizeBox = false; this.Name = "Form1"; this.ShowIcon = false; this.ShowInTaskbar = false; this.Text = "test"; this.TopMost = true; this.ResumeLayout(false);
-
I checked and the width is 282 i think. And by debugger you mean like the play button on xamarin studio? That's what i've been using to test my program. Also just in case i'll post the rest of my form information.
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(282, 253); this.ControlBox = false; this.Controls.Add(this.webBrowser1); this.KeyPreview = true; this.MaximizeBox = false; this.MinimizeBox = false; this.Name = "Form1"; this.ShowIcon = false; this.ShowInTaskbar = false; this.Text = "test"; this.TopMost = true; this.ResumeLayout(false);
OK: so you can't affect the Left and Top of your current window - because the ClientSize of the current window is going to be at best the same as the width of the window! So, if you are trying to move something within the current window, use its properties: Left, Width, and Top, and the Window Client Area. Your original code shows you being inside the form, so
this
refers to the whole form, not anything within it. If you are trying to move the form, then you need to get the information on sizing from whatever it is displayed on - probably the desktop (or android equivalent, if that's what you are using).Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
OK: so you can't affect the Left and Top of your current window - because the ClientSize of the current window is going to be at best the same as the width of the window! So, if you are trying to move something within the current window, use its properties: Left, Width, and Top, and the Window Client Area. Your original code shows you being inside the form, so
this
refers to the whole form, not anything within it. If you are trying to move the form, then you need to get the information on sizing from whatever it is displayed on - probably the desktop (or android equivalent, if that's what you are using).Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
When you say move the form do you mean the things inside the window? I'm trying to move the entire window and by information on sizing do you mean my screen resolution? If i use Left, Width, and Top, and the Window Client Area to move the thing inside the window, do i need something else to move the window itself? Edit: Oh i just looked it up and form is the window itself.
-
When you say move the form do you mean the things inside the window? I'm trying to move the entire window and by information on sizing do you mean my screen resolution? If i use Left, Width, and Top, and the Window Client Area to move the thing inside the window, do i need something else to move the window itself? Edit: Oh i just looked it up and form is the window itself.
Backup a second... When you are a form, you have four properties you are interested in: Width and Height - which tell you how big the whole form is; then Top and Left - which tell you where you are relative to the container the form is displayed within. For Windows, that's the Desktop - so Top and Left of zero is the top left hand corner of your monitor. The Client Area of a form is the bit of the form that isn't the title bar and the form border and it measured relative to the form top left corner - so the Client Area will normally be smaller than the Width and Height, and never, ever bigger. So trying to move the form doesn't involve the form's client area for "reaching an edge" - it involves whatever the form is contained within: normally the Desktop. See if you have a Screen object you can access the Width and Height from and try those.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
Backup a second... When you are a form, you have four properties you are interested in: Width and Height - which tell you how big the whole form is; then Top and Left - which tell you where you are relative to the container the form is displayed within. For Windows, that's the Desktop - so Top and Left of zero is the top left hand corner of your monitor. The Client Area of a form is the bit of the form that isn't the title bar and the form border and it measured relative to the form top left corner - so the Client Area will normally be smaller than the Width and Height, and never, ever bigger. So trying to move the form doesn't involve the form's client area for "reaching an edge" - it involves whatever the form is contained within: normally the Desktop. See if you have a Screen object you can access the Width and Height from and try those.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
Oh i think the problem is that sometimes the window is created partially outside the screen resolution and for some reason the if statements make the window start vibrating because it's constantly changing directions. So to fix the problem i need to make sure that the window isn't created outside of the screen. I changed a few things and it seemed to fix a majority of the problems:
WindowX = (random.Next(0, width - Width));
WindowY = (random.Next(0, height - Height));this.Left += WindowVx; if (Left < 0 || (Left + this.Width) > width){ WindowVx = -WindowVx; } this.Top += WindowVy; if (Top < 0 || (Top + this.Height) > height) { WindowVy = -WindowVy; }
However sometimes the window is still created a bit past the bounds for some reason, but I'll work on it tomorrow, for now i need to sleep. Thank you for the help so far though! Oh, and for screen object, do you mean this?:
private int width = Screen.PrimaryScreen.WorkingArea.Width; private int height = Screen.PrimaryScreen.WorkingArea.Height;
I changed it to this:
private int width = Screen.PrimaryScreen.Bounds.Width; private int height = Screen.PrimaryScreen.Bounds.Height;
That way it'll go over my task bar.
-
Oh i think the problem is that sometimes the window is created partially outside the screen resolution and for some reason the if statements make the window start vibrating because it's constantly changing directions. So to fix the problem i need to make sure that the window isn't created outside of the screen. I changed a few things and it seemed to fix a majority of the problems:
WindowX = (random.Next(0, width - Width));
WindowY = (random.Next(0, height - Height));this.Left += WindowVx; if (Left < 0 || (Left + this.Width) > width){ WindowVx = -WindowVx; } this.Top += WindowVy; if (Top < 0 || (Top + this.Height) > height) { WindowVy = -WindowVy; }
However sometimes the window is still created a bit past the bounds for some reason, but I'll work on it tomorrow, for now i need to sleep. Thank you for the help so far though! Oh, and for screen object, do you mean this?:
private int width = Screen.PrimaryScreen.WorkingArea.Width; private int height = Screen.PrimaryScreen.WorkingArea.Height;
I changed it to this:
private int width = Screen.PrimaryScreen.Bounds.Width; private int height = Screen.PrimaryScreen.Bounds.Height;
That way it'll go over my task bar.
:thumbsup: Sleep well!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
:thumbsup: Sleep well!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...