Center on object always
-
Tuesday, September 18, 2012 4:49 AMHow do I force the screen to always center on a picture box or object?
All Replies
-
Tuesday, September 18, 2012 5:10 AMDo you mean you always want your form centered in the screen? I am unsure of what you mean.
You've taught me everything I know but not everything you know.
-
Tuesday, September 18, 2012 5:28 AM
Be aware that this is sometimes done by some websites (card games and sex) who force their part in the middle of the screen over all other data.
That is not liked by most users and can therefore be seen as usable for malicious intent.
Success
Cor- Edited by Cor LigthertMVP Tuesday, September 18, 2012 5:29 AM
-
Tuesday, September 18, 2012 3:51 PM
I have a sample code were its done kinda using the mouse event
Private _GridSize As Size Private _GridOrigin As Point Private _CellSize As Size = New Size(100, 100) Private _ClickDelta As Point Public Sub New() ' This call is required by the designer. InitializeComponent() ' Add any initialization after the InitializeComponent() call. DoubleBuffered = True Me.ClientSize = New Size(600, 600) End Sub Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs) MyBase.OnMouseDown(e) _ClickDelta = e.Location End Sub Protected Overrides Sub OnMouseMove(ByVal e As System.Windows.Forms.MouseEventArgs) MyBase.OnMouseMove(e) If e.Button = Windows.Forms.MouseButtons.Left Then _GridOrigin += New Size(e.Location.X - _ClickDelta.X, e.Location.Y - _ClickDelta.Y) _ClickDelta = e.Location Invalidate() End If End Sub
But I need it to center on a picture box. I could take the normal route were you make the tiles move instead and create a illusion that the map is moving but I thought it would be easier to just move the object its self and make the screen follow the position of the object instead.
The above is just a code snippet of course but I hope it helps explain what I'm attempting to do.
-
Tuesday, September 18, 2012 5:03 PM
I made a video explaining it a little better and I came up with a solution kinda. So now the new question is how do you make the slide bar move left anr right using key strokes through code. The idea is get the slide bar to move when the player moves so it will move with the player.
Video:
Edit:
I found this but i still have no clue how to control the scrollbar or how to force it to move only with the image/object:
http://msdn.microsoft.com/en-us/library/system.windows.forms.scrolleventargs.aspx#Y215
- Edited by mholmes_3038 Tuesday, September 18, 2012 5:36 PM
-
Tuesday, September 18, 2012 6:53 PM
I found this sample but I dont know which sample ref to grab and it dont say. Here is link though maybe you guys can figure it out:
http://msdn.microsoft.com/en-us/library/system.windows.forms.scrollbar.scroll.aspx
Public Class Form1 Private Sub HandleScroll(ByVal sender As [Object], ByVal e As ScrollEventArgs) Handles VScrollBar.Scroll, HScrollBar.Scroll 'Create a graphics object and draw a portion of the image in the PictureBox. Dim g As Graphics = PictureBox1.CreateGraphics() Dim xWidth As Integer = PictureBox1.Width Dim yHeight As Integer = PictureBox1.Height Dim x As Integer Dim y As Integer If (e.ScrollOrientation = ScrollOrientation.HorizontalScroll) Then x = e.NewValue y = VScrollBar.Value Else 'e.ScrollOrientation == ScrollOrientation.VerticalScroll y = e.NewValue x = HScrollBar.Value End If 'First Rectangle: Where to draw the image. 'Second Rectangle: The portion of the image to draw. g.DrawImage(PictureBox1.Image, _ New Rectangle(0, 0, xWidth, yHeight), _ New Rectangle(x, y, xWidth, yHeight), _ GraphicsUnit.Pixel) PictureBox1.Update() End Sub End Class
Changed:
HScrollBar1 to HScrollBar
VScrollBar1 to VScrollBar
I think this would work just in reverse order i think. Right now I think the object moves based on scroll bar but I need that in reverse so I need help getting this sample to work. Says I need to add a refference from assebly but it does not say which refference to add. No clue were to grab it ethier.
-
Tuesday, September 18, 2012 7:10 PM
Ok I found the refference to import but still not working, suggestions?
Imports System.Windows.Forms 'Added ref System.Windows.Forums Public Class Form1 Private Sub HandleScroll(ByVal sender As [Object], ByVal e As ScrollEventArgs) Handles VScrollBar1.Scroll, HScrollBar1.Scroll 'Create a graphics object and draw a portion of the image in the PictureBox. Dim g As Graphics = PictureBox1.CreateGraphics() Dim xWidth As Integer = PictureBox1.Width Dim yHeight As Integer = PictureBox1.Height Dim x As Integer Dim y As Integer If (e.ScrollOrientation = ScrollOrientation.HorizontalScroll) Then x = e.NewValue y = VScrollBar1.Value Else 'e.ScrollOrientation == ScrollOrientation.VerticalScroll y = e.NewValue x = HScrollBar1.Value End If 'First Rectangle: Where to draw the image. 'Second Rectangle: The portion of the image to draw. g.DrawImage(PictureBox1.Image, _ New Rectangle(0, 0, xWidth, yHeight), _ New Rectangle(x, y, xWidth, yHeight), _ GraphicsUnit.Pixel) PictureBox1.Update() End Sub End Class
-
Tuesday, September 18, 2012 7:14 PM
Blue Line under :
VScrollBar1:Handle clause requires a WithEvents variable defined in containing type or one of its base types
HScrollBar1: Same error as above
-
Tuesday, September 18, 2012 8:28 PMYou need to add WithEvents to your scroll bar definitions.
- Edited by JohnWeinMicrosoft Community Contributor Tuesday, September 18, 2012 8:28 PM
-
Tuesday, September 18, 2012 8:54 PMThats the MSDN sample code though posted above, I should not have to add anything else to it plus I dont know where you would even add it for it to even work as suggested on the MSDN sample.
- Edited by mholmes_3038 Tuesday, September 18, 2012 8:54 PM
-
Tuesday, September 18, 2012 9:12 PMIt won't work with errors. You'll either have to fix it or get rid of it. Go to the scroll bar definitions and add WithEvents.
-
Wednesday, September 19, 2012 12:42 AMPlease post a working sample then please I honestly dont think adding WithEvents will fix it but if you think it will please post a working sample of what you suggest please.
-
Wednesday, September 19, 2012 12:58 AM
Please post a working sample then please I honestly dont think adding WithEvents will fix it but if you think it will please post a working sample of what you suggest please.
Post your current scroll bar definitions. -
Wednesday, September 19, 2012 1:46 AM
Everything I have is based off this sample from MSDN
http://msdn.microsoft.com/en-us/library/system.windows.forms.scrollbar.scroll.aspx
Ive put everythign back as they suggested and i'm still clueless why its not working. So please feel free to post your working sample.
-
Wednesday, September 19, 2012 2:12 AM
Everything I have is based off this sample from MSDN
http://msdn.microsoft.com/en-us/library/system.windows.forms.scrollbar.scroll.aspx
Ive put everythign back as they suggested and i'm still clueless why its not working. So please feel free to post your working sample.
I was trying to help you correct the errors. I don't seem to be helping. Sorry. -
Wednesday, September 19, 2012 3:19 AM
Start over.
Your description is not clear - you can't force the screen to do anything. Do you mean that you want to force a picture box to always be in the centre of your form?
Or are you talking about moving an image behind the picture box to create an illusion of movement across a map? Because you then talk about tiles, moving the screen and an object.
I cannot figure out what you are trying to do. Your examples do not seem to match your task description. You have mouse events and scroll bars. If you are trying to centre a picture box within your form then mouse movements and scroll bars would not be relevant - you would be considering events such as the form loading, form resizing, or picture box resizing. If you were talkin about an object on a moving map then you would be dealing with whatever events create movement in the object, which I think would be keystrokes. But since your task description is completely unclear, perhaps the examples are relevant after all. It's not possible to tell.
-
Thursday, September 20, 2012 12:38 AM
Sorry, I'll re-explain. I want the scrollbar to follow a picture box as you move the picutre box using keystrokes or key up and keydown event.
I know how to make a game using tile map and move tiles to create a illussion your moving when the brackground/tiles our moving not the player. But I thought it would be easier/less code if the player moved and the scroll bar will auto move following the player acting like a camera. So I grabed the sample fromt he MSDN that I posted above but the MSDN sample appears to not work for some reason.
Anyway, just need to know how make the scrollbar move via keydown event or automaticaly follow a picture box the player controls by keydown event. Does that make sense now?
-
Thursday, September 20, 2012 1:44 AM
Anyway, just need to know how make the scrollbar move via keydown event or automaticaly follow a picture box the player controls by keydown event. Does that make sense now?
Set the scroll bar value to the value that represents the proportion that the player has moved across the map. So if the player is at a position that represents 60% of the distance from the left, the horizontal scroll bar value should be at 60% of its total range. Set this value whenever the player's position within the scene changes, which could be on a keystroke event or could be due to some other gameplay, such as encountering a wormhole.
You could set the scroll bar values (min, max) to match the map values, and use the player position directly or (my preference) you could convert the player position to a percentage and set the scroll bar maximum to 100 and use the percentage figure to control the scrollbar.
Note that the scroll bar is not the best option for this form of display as it will be approximate only, due to issues with the width of the thumb. But it may be adequate for your purpose.
-
Thursday, September 20, 2012 4:42 AM
I made a form with two Buttons and a PictureBox. Button1 is aligned along the top of the form and is 30000 wide and 10 high in size. Button2 is aligned along the right side of the form and is 10 wide and 30000 high in size. PictureBox1 is 100 wide and 88 high and is initially located at coordinates for its top left corner at X 15000 and Y 15000. Pressing T makes the PictureBox move up but stay centered on the screen, V moves it down, D left and H right. You can see the horizontal or vertical scroll bar move as you press left right or up down buttons while the picturebox stays centered in the screen.
I also placed a zoom image as the background of Form1 so you could see the PictureBox moving across the image but it's hard to tell unless you increment the PictureBox movements at higher than + 1 or - 1 which can cause odd behaviour.
Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Me.AutoScroll = True Me.WindowState = FormWindowState.Maximized Me.DoubleBuffered = True PictureBox1.Location = New Point(15000, 15000) KeyPreview = True End Sub Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown If e.KeyCode = Keys.T Then 'Up PictureBox1.Location = New Point(PictureBox1.Location.X(), PictureBox1.Location.Y() - 1) Me.AutoScrollPosition = New Point(PictureBox1.Location.X() - 683 + 50, PictureBox1.Location.Y() - 384 + 44) End If If e.KeyCode = Keys.D Then 'Left PictureBox1.Location = New Point(PictureBox1.Location.X() - 1, PictureBox1.Location.Y()) Me.AutoScrollPosition = New Point(PictureBox1.Location.X() - 683 + 50, PictureBox1.Location.Y() - 384 + 44) End If If e.KeyCode = Keys.H Then 'Right PictureBox1.Location = New Point(PictureBox1.Location.X() + 1, PictureBox1.Location.Y()) Me.AutoScrollPosition = New Point(PictureBox1.Location.X() - 683 + 50, PictureBox1.Location.Y() - 384 + 44) End If If e.KeyCode = Keys.V Then 'Down PictureBox1.Location = New Point(PictureBox1.Location.X(), PictureBox1.Location.Y() + 1) Me.AutoScrollPosition = New Point(PictureBox1.Location.X() - 683 + 50, PictureBox1.Location.Y() - 384 + 44) End If End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click End Sub Private Sub Button2_Click(sender As Object, e As EventArgs) End Sub Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click End Sub End Class
You've taught me everything I know but not everything you know.
-
Friday, September 21, 2012 11:09 AMThanks guys I'll test it to night, I have to go teach a class right now but this looks great. TY all for the help on this matter.
-
Friday, September 21, 2012 8:47 PM
See my previous post. This new code will let you have a picturebox of any size.
Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Me.AutoScroll = True Me.WindowState = FormWindowState.Maximized Me.DoubleBuffered = True PictureBox1.Location = New Point(15000, 15000) KeyPreview = True End Sub Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown If e.KeyCode = Keys.T Then 'Up PictureBox1.Location = New Point(PictureBox1.Location.X(), PictureBox1.Location.Y() - 1) Me.AutoScrollPosition = New Point(PictureBox1.Location.X() - 683 + (Math.Round(PictureBox1.Height / 2)), PictureBox1.Location.Y() - 384 + (Math.Round(PictureBox1.Width / 2))) End If If e.KeyCode = Keys.D Then 'Left PictureBox1.Location = New Point(PictureBox1.Location.X() - 1, PictureBox1.Location.Y()) Me.AutoScrollPosition = New Point(PictureBox1.Location.X() - 683 + (Math.Round(PictureBox1.Height / 2)), PictureBox1.Location.Y() - 384 + (Math.Round(PictureBox1.Width / 2))) End If If e.KeyCode = Keys.H Then 'Right PictureBox1.Location = New Point(PictureBox1.Location.X() + 1, PictureBox1.Location.Y()) Me.AutoScrollPosition = New Point(PictureBox1.Location.X() - 683 + (Math.Round(PictureBox1.Height / 2)), PictureBox1.Location.Y() - 384 + (Math.Round(PictureBox1.Width / 2))) End If If e.KeyCode = Keys.V Then 'Down PictureBox1.Location = New Point(PictureBox1.Location.X(), PictureBox1.Location.Y() + 1) Me.AutoScrollPosition = New Point(PictureBox1.Location.X() - 683 + (Math.Round(PictureBox1.Height / 2)), PictureBox1.Location.Y() - 384 + (Math.Round(PictureBox1.Width / 2))) End If End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click End Sub Private Sub Button2_Click(sender As Object, e As EventArgs) End Sub Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click End Sub End ClassYou've taught me everything I know but not everything you know.
-
Saturday, September 22, 2012 1:00 AM
This is like my previous post except it uses the mouse to move the PictureBox across the form by placing the mouse pointer at new coordinates and clicking one of the buttons. It's kind of ugly in that the PictureBox moves to the new location before the autoscroll catches up to the new coordinates.
Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Me.AutoScroll = True Me.WindowState = FormWindowState.Maximized Me.DoubleBuffered = True PictureBox1.Location = New Point(15000, 15000) End Sub Private Sub Form1_MouseDown(sender As Object, e As MouseEventArgs) Handles Me.MouseDown If MousePosition.X() Or MousePosition.Y() Then PictureBox1.Location = New Point(MousePosition.X(), MousePosition.Y()) Me.AutoScrollPosition = New Point(PictureBox1.Location.X() - 683 + (Math.Round(PictureBox1.Height / 2)), PictureBox1.Location.Y() - 384 + (Math.Round(PictureBox1.Width / 2))) End If End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click End Sub Private Sub Button2_Click(sender As Object, e As EventArgs) End Sub Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click End Sub End ClassYou've taught me everything I know but not everything you know.
-
Saturday, September 22, 2012 3:08 AMThis is really useful stuff Monkey. Do you have any books you can suggest or sites you used to learn? You have only been here a short time but you seem to have made huge progress quickly. I'm amazed by your brilliant programing in such a short time. Two thumbs up bro.
-
Saturday, September 22, 2012 5:50 AM
The change to this code is to detect the primary screen working area width and height automatically instead of having 1366 / 2 and 768 / 2 hard coded into the program.
Public Class Form1 Dim SPSWAW As Object = Screen.PrimaryScreen.WorkingArea.Width Dim SPSWAH As Object = Screen.PrimaryScreen.WorkingArea.Height Dim WAW As Integer = CInt(SPSWAW) Dim WAH As Integer = CInt(SPSWAH) Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Me.AutoScroll = True Me.WindowState = FormWindowState.Maximized Me.DoubleBuffered = True PictureBox1.Location = New Point(15000, 15000) End Sub Private Sub Form1_MouseDown(sender As Object, e As MouseEventArgs) Handles Me.MouseDown If MousePosition.X() Or MousePosition.Y() Then PictureBox1.Location = New Point(MousePosition.X(), MousePosition.Y()) Me.AutoScrollPosition = New Point(PictureBox1.Location.X() - ((WAW) / 2) + (Math.Round(PictureBox1.Height / 2)), PictureBox1.Location.Y() - ((WAH) / 2) + (Math.Round(PictureBox1.Width / 2))) End If End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click End Sub Private Sub Button2_Click(sender As Object, e As EventArgs) End Sub Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click End Sub End ClassYou've taught me everything I know but not everything you know.
-
Saturday, September 22, 2012 6:04 AM
Thanks for the feedback M Holmes. Basically I've learned from watching what all of the responders to OP's questions provide in their feedback in the form of code and explanations. I also resort to searching all MSDN forums and using the MSDN library a lot for help in resolving code issues. And I google for answers to questions which links to forums other than MSDN for the most part. Plus I post questions of my own sometimes and usually get replies PDQ from a variety of responders.
I can't cite any books for reference because I haven't read any so I am lacking in a lot of basic knowledge on programming anyhow. Sites I use to learn are mostly MSDN and Code Project though a variety of other sites I access from time to time if they contain relevant information to the task at hand.
Thanks again! :)
You've taught me everything I know but not everything you know.
-
Saturday, September 22, 2012 10:17 AM
Here's the updated code that uses the Keys to move the PictureBox with the auto screen size detect added.
Public Class Form1 Dim SPSWAW As Object = Screen.PrimaryScreen.WorkingArea.Width Dim SPSWAH As Object = Screen.PrimaryScreen.WorkingArea.Height Dim WAW As Integer = CInt(SPSWAW) Dim WAH As Integer = CInt(SPSWAH) Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Me.AutoScroll = True Me.WindowState = FormWindowState.Maximized Me.DoubleBuffered = True PictureBox1.Location = New Point(15000, 15000) KeyPreview = True End Sub Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown If e.KeyCode = Keys.T Then 'Up PictureBox1.Location = New Point(PictureBox1.Location.X(), PictureBox1.Location.Y() - 5) Me.AutoScrollPosition = New Point(PictureBox1.Location.X() - ((WAW) / 2) + (Math.Round(PictureBox1.Height / 2)), PictureBox1.Location.Y() - ((WAH) / 2) + (Math.Round(PictureBox1.Width / 2))) End If If e.KeyCode = Keys.D Then 'Left PictureBox1.Location = New Point(PictureBox1.Location.X() - 5, PictureBox1.Location.Y()) Me.AutoScrollPosition = New Point(PictureBox1.Location.X() - ((WAW) / 2) + (Math.Round(PictureBox1.Height / 2)), PictureBox1.Location.Y() - ((WAH) / 2) + (Math.Round(PictureBox1.Width / 2))) End If If e.KeyCode = Keys.H Then 'Right PictureBox1.Location = New Point(PictureBox1.Location.X() + 5, PictureBox1.Location.Y()) Me.AutoScrollPosition = New Point(PictureBox1.Location.X() - ((WAW) / 2) + (Math.Round(PictureBox1.Height / 2)), PictureBox1.Location.Y() - ((WAH) / 2) + (Math.Round(PictureBox1.Width / 2))) End If If e.KeyCode = Keys.V Then 'Down PictureBox1.Location = New Point(PictureBox1.Location.X(), PictureBox1.Location.Y() + 5) Me.AutoScrollPosition = New Point(PictureBox1.Location.X() - ((WAW) / 2) + (Math.Round(PictureBox1.Height / 2)), PictureBox1.Location.Y() - ((WAH) / 2) + (Math.Round(PictureBox1.Width / 2))) End If End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click End Sub Private Sub Button2_Click(sender As Object, e As EventArgs) End Sub Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click End Sub End ClassYou've taught me everything I know but not everything you know.
- Marked As Answer by mholmes_3038 Tuesday, September 25, 2012 4:41 AM

