How to resize form while keeping it at the centre of the screen? - vb.net

I have a vb.net form.
I used the following tutorial to add shadow and moving/resizing feature to my form.
Metro-UI-Zune-like-Interface-form
It is working fine but my problem is that i want that whenever my form is resized,
it should remain at the centre of the screen (or, if i resize it from bottom, it resizes from the top & bottom both half-half).
I have used this :
Private Sub Form1_SizeChanged(sender As Object, e As EventArgs) Handles Me.SizeChanged
Top = (Screen.PrimaryScreen.WorkingArea.Height - Height) / 2
Left = (Screen.PrimaryScreen.WorkingArea.Width - Width) / 2
End Sub
But whenever i resize it, after the resizing, the form moves to the position of the mouse pointer.
The mouse pointer is outside the form window since it resizes half half in both opposite directions.
It causes flickering and it changes the position of the form.
Any ideas how i can implement this smoothly ? Thanks in advance.

Declare 2 variables to keep the desktop screen resolutions
Dim dx, dy As Integer
on form load get the screen resolutions and them in dx & dy
dx = My.Computer.Screen.Bounds.Width
dy = My.Computer.Screen.Bounds.Height
and finally, on a resize event do the following
Private Sub Form1_ResizeEnd(sender As Object, e As EventArgs) Handles MyBase.ResizeEnd
Dim x, y As Integer
x = Me.Width / 2
y = Me.Height / 2
Me.Location = New Point(dx / 2 - x, dy / 2 - y)
End Sub

You could use the Form.CenterToScreen() method:
Private Sub Form1_SizeChanged(sender As Object, e As EventArgs) Handles Me.SizeChanged
Me.CenterToScreen()
End Sub
Read more: https://msdn.microsoft.com/en-us/library/system.windows.forms.form.centertoscreen(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1
Yes, I know the article says that it's not intended to be used from your code, but I've seen other professional programmers use it without a problem. :)

Related

How to make a Control move left/right only using the mouse pointer?

I'm currently making a game, it contains a paddle (called base) that must move from left to right only.
I found a piece of code that allowed the platform to move, however it moves in all directions and isn't synced with my mouse pointer properly:
Private Sub Form1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
base.Location = MousePosition
End Sub
What do I need to change or add in order for the paddle to only move horizontally?
Assume your paddle (I'm naming the Control paddle here, base is not a good name) is placed near the bottom of the Form, its Height ~25 pixels and its bottom distance from the Form's bottom side ~10-20 pixels.
You can clip the Cursor to a narrow band right above it when the Mouse enters the Form.
You can then move the Cursor without intersecting other Controls in the Form, which could interfere with the generation of MouseMove events.
You can also hide the Cursor, so the arrow pointer doesn't become visually obnoxious (unless it's required for something else, of course).
When the Cursor is moved, the movement is translated to the middle of the paddle Control, which is moved only to the left or right, in relation to the current Cursor offset:
(PointToClient(Cursor.Position).X - (paddle.Width \ 2))
When the Form closes, restore the Cursor and the clipping region.
Paste this code inside the Form that contains the paddle (and rename base to paddle):
Protected Overrides Sub OnMouseEnter(e As EventArgs)
MyBase.OnMouseEnter(e)
ClipCursor()
End Sub
Protected Overrides Sub OnMouseMove(e As MouseEventArgs)
MyBase.OnMouseMove(e)
paddle.Left = PointToClient(Cursor.Position).X - (paddle.Width \ 2)
End Sub
Protected Overrides Sub OnFormClosing(e As FormClosingEventArgs)
ShowCursor()
MyBase.OnFormClosing(e)
End Sub
Private Sub ClipCursor()
Dim bandLocation = New Point(
Left + 8 + (paddle.Width \ 2),
Bottom - paddle.Height * 2 - Cursor.Size.Height)
Dim bandSize = New Size(ClientSize.Width - paddle.Width, 20)
Cursor.Clip = New Rectangle(bandLocation, bandSize)
Cursor.Hide()
End Sub
Private Sub ShowCursor()
Cursor.Clip = Rectangle.Empty
Cursor.Show()
End Sub
You can accomplish this by only assigning the X coordinate to the location property:
base.Location = New Point(Cursor.Position.X, Button1.Location.Y)
This will ignore the Y coordinate, resulting only in horizontal movement. Also, be aware that depending on your situation, you may have to translate the mouse pointer coordinates relative to the window. So, in case the result is distorted, do it like this:
base.Location = New Point(PointToClient(Cursor.Position).X, Button1.Location.Y)
This will translate the mouse coordinates (from e.g. Cursor.Position property) into window-relative coordinates.

draw coordinate Two dimensions graph of (x-y) visual basic

hey every one I want to make a program that able to draw coordinate graph of Two dimensions of (x-y).when I enter a value in (x) text box and (y) text box and hit draw button it well draw the graph in the blue picture box . I searched in web sit but I found only one method that draw using the mouse and this not what I want .This is the image of the program and and it supposed to draw in white line like this image
Private Sub PictureBox2_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox2.MouseMove
Static last As Point
If e.Button = Windows.Forms.MouseButtons.Left Then
PictureBox2.CreateGraphics.DrawLine(Pens.White, last.X, last.Y, e.X, e.Y)
End If
last = e.Location
End Sub
this is the code that I found that draw using the mouse
You should use the Graphic class inside the panel's Paint event
Private Sub Panel1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs)
' Create pen.
Using blackPen As New Pen(Color.Black, 3)
' Create points that define line.
Dim point1 As New Point(100, 100)
Dim point2 As New Point(500, 100)
' Draw line to screen.
e.Graphics.DrawLine(blackPen, point1, point2)
End Using
End Sub
Then call Panle1.Invalidate() to fire the Paint event
It would be good to have a bit more details of your code, especially how and in what class you store the X and Y coordinate. By the way you draw a line between 2 points so you would need two sets of X and Y coordinate boxes on your form.
After that it is as easy as what you found on the internet using the DrawLine method (https://msdn.microsoft.com/en-us/library/system.drawing.graphics.drawline(v=vs.110).aspx), you just needs to fire it from the Click event of your "draw" button.
Hi again Ahmed
Here is a simple sample of a form that would draw a line when button is clicked. Of course it would need more bootstrapping to make sure user only enter integer value for the number of pixels and in its basic form, (0,0) is the top left of the panel but it can easily be converted for a bottom left approach...
and
Class Form1
Private Sub cmdDraw_Click(sender As Object, e As EventArgs) Handles cmdDraw.Click
Dim x1 As Integer = Integer.Parse(txtX1.Text)
Dim y1 As Integer = Integer.Parse(txtY1.Text)
Dim x2 As Integer = Integer.Parse(txtX2.Text)
Dim y2 As Integer = Integer.Parse(txtY2.Text)
pnlMap.CreateGraphics.DrawLine(New Pen(Color.Black), x1, y1, x2, y2)
End Sub
End Class

Find position of mouse relative to control, rather than screen

I have a Picture Box called BGImage. I hope that when the user clicks on this I can capture the position of the mouse relative to BGImage.
I've tried using MousePosition, only to find it gives the mouse location on the screen, not on the PictureBox.
So I also tried using PointToClient:
Dim MousePos As Point = Me.PointToClient(MousePosition)
But this gives me the location {X=1866,Y=55} whereas I actually clicked on the PictureBox at around {X=516,Y=284}.
I think the problem arises because I have full-screened my program and set the position of the PictureBox to be at the centre of the screen (BGImage.Location = New Point((My.Computer.Screen.WorkingArea.Width / 2) - (1008 / 2), ((My.Computer.Screen.WorkingArea.Height / 2) - (567 / 2))))
I should also mention that the size of the PictureBox is 1008 By 567 pixels and my screen resolution is 1366 by 768.
Is there any way I can get the mouse position relative to BGImage's position?
Add a mouse click event to your picture box
Then use the MouseEventArgs to get the mouse position inside the picture box.
This will give you the X and the Y location inside the picture box.
Dim PPoint As Point
Private Sub PictureBox1_MouseClick(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseClick
PPoint = New Point(e.X, e.Y)
MsgBox(Convert.ToString(PPoint))
End Sub
I have before the same problem and just solved with the help of some friends.
Give a look Here mouse position is not correct
Here its the code that give you the correct position of the Mouse Based On A Picture.
Tanks to #Aaron he have give a final solution to this problem.
This will put a red dot on the exact point you click. I wonder how useful setting the cursor position will be though, as they will almost certainly move the mouse after clicking the button (inadvertently or not).
Setting the Cursor position needs to be in Screen coordinates - this converts back to client coordinates for drawing. I don't believe the PointToClient is necessary for the cursor position. In the below code, it is an unnecessary conversion, as you just go back to client coordinates. I left it in to show an example of each conversion, so that you can experiment with them.
Public Class Form1
Private PPoint As Point
Public Sub New()
' This call is required by the designer.
InitializeComponent()
PictureBox1.BackColor = Color.White
PictureBox1.BorderStyle = BorderStyle.Fixed3D
AddHandler PictureBox1.MouseClick, AddressOf PictureBox1_MouseClick
AddHandler Button8.Click, AddressOf Button8_Click
' Add any initialization after the InitializeComponent() call.
End Sub
Private Sub Button8_Click(sender As Object, e As EventArgs)
Dim g As Graphics = PictureBox1.CreateGraphics()
Dim rect As New Rectangle(PictureBox1.PointToClient(PPoint), New Size(1, 1))
g.DrawRectangle(Pens.Red, rect)
End Sub
Private Sub PictureBox1_MouseClick(sender As Object, e As MouseEventArgs)
PPoint = PictureBox1.PointToScreen(New Point(e.X, e.Y))
Label8.Text = PPoint.X.ToString()
Label9.Text = PPoint.Y.ToString()
End Sub
End Class
Instead of using:
Dim MousePos As Point = Me.PointToClient(MousePosition)
You should be using:
Dim MousePos As Point = BGImage.PointToClient(MousePosition)
It will give you mouse position in BGImage coordinates, whereas the first code gives you the mouse position in the Form's coordinates.

VB.net -- Getting mouse coordinates outside the form

I'm writing a simple program in Vb.net that collects information. such things as mouse x y coordinates, Pixel color, and keystroke numbers. I would like to be able to view the x and y coordinates of the cursor anywhere on the screen instead of just over the form, and i would like to do this in the simplest way possible. One way i have achieved the desired effect is with the following settings:
Picturebox2:
BackColor = Red
image = 3 x 2 pixel image (Hardly visible, but is required all the same)
Form1:
Transparency key = Red
This results in the appearance of the mouse coordinates being displayed while the cursor is outside the form boundaries. however it's still over the form. The code i'm using for this particular problem is:
Dim mouseloc As Point
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
mouseloc = Cursor.Position
lblc.Text = PointToClient(mouseloc).ToString
lbls.Text = PointToScreen(mouseloc).ToString
End Sub
Private Sub PictureBox2_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox2.MouseMove
mouseloc = Cursor.Position
lblc.Text = PointToClient(mouseloc).ToString
lbls.Text = PointToScreen(mouseloc).ToString
End Sub
Im running Visual Studio 2010 on a Windows 7 x64 Sony VAIO
A really simple way would be to Capture the mouse in the Form via Me.Capture = True. See here for details:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.capture.aspx
That being said, if you need to track the mouse even when you are not the active application you are going to have to use some type of hooking. Not clear exactly what you are looking to do though.

Why can I not set right property in vb

I want my form to appear at the right edge of my screen
but I not use since Right is readonly .Is there a solution for this?
Right= My.Computer.Screen.WorkingArea.Right
Use this code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim x As Integer = My.Computer.Screen.WorkingArea.Right - Me.Width
Dim y As Integer = My.Computer.Screen.WorkingArea.Bottom / 2 - Me.Height / 2
Me.Location = New Point(x, y)
End Sub
The Y-Coordinate is not much specified in your question, so I took it as the center..!
Basically you need to subtract the width of the form from the right edge, only then it will appear as you wanted.
Cheers..
I used following and it worked
Left= My.Computer.Screen.WorkingArea.Right -Width
We won't know why until you give us more information such as an error message.
Dim nRight As Integer = My.Computer.Screen.WorkingArea.Right
works just fine.
Set the Location property instead.
If you want to move the form to the right edge of the screen by stretching the form, you can use something like this:
Me.Width = Me.Width + My.Computer.Screen.WorkingArea.Right - (Me.Left + Me.Width)
If you want to move the form to the right edge of the screen without changing its width, you can use this:
Me.Left = Me.Left + My.Computer.Screen.WorkingArea.Right - (Me.Left + Me.Width)