Finding the center of application - VB.net - vb.net

I am wanting to find the center of my application. Please read before saying Me.Width / 2..
I am loading a web page that has a Java based app in it and am wanting to automatically click the, 'login', button once the page has loaded. The VB app is a set size, but will not be maximized allowing the user to move the app around the screen. What I was thinking was doing something like..
SetCursorPos({CenterOfApp.Width} + 20, {CenterOfApp.Height})
..and then simulating a mouse click.
Help would be appreciated, thanks!
Edit 1 - I believe the answer could be below, I am just not sure of the formula required. Again I say, this is a fixed sized VB.net application loading a fixed sized Java application threw the VB.net browser control. The button will always be in the same location of the VB.net app, but the VB.net app will not always be in the same location of the computer screen. Meaning, I will need to find the center of the VB.net app depending on where it is on the screen of the computer.
Dim ScreenWidth As Integer = Screen.PrimaryScreen.Bounds.Width
Dim ScreenHeight As Integer = Screen.PrimaryScreen.Bounds.Height
Dim ClientLeft As Integer = Me.Location.X
Dim LeftOfScreenToApp As Integer ' This is to be the distance from the left side of the screen to the left side of the VB app.
LeftOfScreenToApp = ...
SetCursorPos(LeftOfScreenToApp + (Me.Width / 2) + 20, Me.Height / 2)

So happens that I was just overlooking the fact that Me.Location.X is the offset from left of screen to the left of the VB.net app, so..
Dim LeftOfScreenToLeftOfApp As Integer = Me.Location.X
Dim MiddleOfApp As Integer = LeftOfScreenToLeftOfApp + Me.Width / 2
..gets the center of the app no matter where it is located on the screen.

Related

How to make VB.NET MDI parent full screen (without maximizing)?

I thought this would take 5 minutes. Spent 3 hours and haven't figured it out...
I have an MDI application and would like an easy way for the user to make it full screen without maximizing it, so I added a Windows/Full Screen menu option and put in this code to handle it:
Me.Left = 0
Me.Top = 0
Me.Width = Screen.PrimaryScreen.WorkingArea.Width
Me.Height = Screen.PrimaryScreen.WorkingArea.Height
The problem is this leaves gaps on the left, bottom, and right side. The top is the only screen edge that works as expected - snugged against the top. The others leave about a 1/8" gap to the screen edges, and 1/8" from the task bar at the bottom.
Sure, I could fudge it and hardcode +10 (or whatever) to the height and width, and -5 to the left. But that seems like a kludge (a fudge kludge), and maybe won't be the same for every monitor.
I tried the DPI awareness thing in the manifest, but it made no difference. I also tried Screen.PrimaryScreen.Bounds and Screen.PrimaryScreen.WorkingArea.X / Y, also with no joy.
It really shouldn't be this difficult. Wth?
Try this:
Dim diffW As Integer = Me.Width - Me.ClientSize.Width
Dim diffH As Integer = Me.Height - Me.ClientSize.Height
Me.ClientSize = New Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height - diffH)
Me.Location = New Point(-diffW / 2, 0)
I´m not quite sure if I understand you correct: You don´t like to have the user to press the maximize button? Instead you try to provide your own "maximize" function?
Ok, sounds strange, but anyway:
Did you already try this:
Screen.PrimaryScreen.WindowState = FormWindowState.Maximized
(sorry, copied from C# code if there is an syntax mayhem)
I understand it is late but this hack can solve:
for c#
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
for vb:
Me.FormBorderStyle = FormBorderStyle.None
Me.WindowState = FormWindowState.Maximized

Location properties on winforms acting strange

I have a form where I allow the user to generate multiple panels with some various contents in the panels by pressing an "add" button. Depending on what the user does in the panel, the panel grows and shrinks to fit the contents. Because of this change is size, I have created a sub that formats the panels on the form.
Private Sub formatPanels(frm As Form)
Dim count As Integer = 0
Dim startPoint As Point = New Point(12, 80)
Dim endPoint As Point = New Point(0, 0)
Dim maxY As Integer = 0
For Each pnl As Control In frm.Controls
If TypeOf pnl Is Panel Then
ReDim Preserve _arr_Panels(count)
_arr_Panels(count) = pnl
count += 1
pnl.Location = startPoint
startPoint.Y += pnl.Size.Height + 30
End If
Next
End Sub
So as you can see, we loop through every panel and the first always begins at the location (12,80) and then increments with the size of the panel and some spacing.
HERE IS THE ISSUE. This ONLY happens when i am SCROLLED DOWN the form. The panels spacing all of a sudden screws up and decides to put the first panel hundreds of pixels down the form. Is the location property based on what you're looking at? So if I were scrolled down the form location(0,0) would be the top left of the current view? There must be some weird property to location that I am not aware of.
Thanks
This behavior is not related to a panel, but to any control on a form with AutoScroll = True and Anchor including Top. (Note: if Anchor didn't also include Left I had some strange positioning on the first call of the function.
The solution is described here which is to use AutoScrollPosition. If you change your startPoint to this it will adjust for the scroll position.
Dim startPoint As Point = New Point(12, Me.AutoScrollPosition.Y + 80)
And the documentation for AutoScrollPosition states this:
When adding controls programmatically to a form, use the AutoScrollPosition property to position the control either inside or outside of the current viewable scroll area.

VB.NET - Change form size by screen resolution:

I'm presuming the answers very simple, but here's the issue, my option menu has two parts, normal settings and advanced settings, the advanced settings are accessed by pressing the advance button, fairly basic. This will make the form bigger unveiling the further settings, but the only problem I have is making a form bigger on different resolution does not make it bigger by the same factor - to put what i saying into context, if i increase the size of my form on a standard 1080p monitor, then run my program on a laptop resolution of 1366 by 768, the form will be much smaller than what i got it to draw on the 1080p resolution screen.
Heres the code I'm using to increase the form size:
Private Sub Advance_btn_Click(sender As Object, e As EventArgs) Handles Advance_btn.Click
If advanced = 0 Then
Me.Size = New Drawing.Size(262, 198) ' will increase form size
Advance_btn.Text = "<" ' text for button changes to know i have pressed the button - (to '<')
advanced = 1
ElseIf advanced = 1 Then
Me.Size = New Size(262, 85) ' will decrease form size
Advance_btn.Text = ">" ' text for button changes to know i have pressed the button - (to '>')
advanced = 0
End If
End Sub
The form is also without borders, if that makes somewhat a difference, thanks.
Me.Height = Screen.PrimaryScreen.Bounds.Height
Me.Width = Screen.PrimaryScreen.Bounds.Width * 0.6
In this example I use for years, the width is set to 60% of the screen width. This is in an IF statement, where I check if the user has a 4K monitor, if not, a full-screen mode is set at application start-up. You can, perpahs, do a similar thing with your drawing area.

Scaling form controls according to screen resolution

I am trying to write program in vb 2010 that is independent of screen resolution.
I am designing the program in 1920*1080 and when I change the resolution to e.g. 800*600 everything blows up and the program won't fit the screen. How can I fix this?
I have tried three different approaches:
loop through all controls and scale their position and dimensions
Friend Sub ResizeControl(ByRef ctl As Control)
'---------------------------- GET SCALES -------------------------
Dim DesignScreenWidth As Integer = 1920
Dim DesignScreenHeight As Integer = 1080
Dim CurrentScreenWidth As Integer = Screen.PrimaryScreen.Bounds.Width
Dim CurrentScreenHeight As Integer = Screen.PrimaryScreen.Bounds.Height
'Ratios
Dim ratioX As Double = CurrentScreenWidth / DesignScreenWidth ' e.g. 800/1920
Dim ratioY As Double = CurrentScreenHeight / DesignScreenHeight
With ctl
Dim height As Integer = Math.Min(.Height, CurrentScreenHeight)
Dim width As Integer = Math.Min(.Width, CurrentScreenWidth)
'Position
If (.GetType.GetProperty("Top").CanRead) Then .Top = CInt(.Top * ratioY)
If (.GetType.GetProperty("Left").CanRead) Then .Left = CInt(.Left * ratioX)
'Size
If (.GetType.GetProperty("Width").CanRead) Then .Width = CInt(width * ratioX)
If (.GetType.GetProperty("Height").CanRead) Then .Height = CInt(height * ratioY)
End With
'---------------------- RESIZE SUB CONTROLS -------------------------------
For Each subCtl As Control In ctl.Controls
ResizeControl(subCtl)
Next subCtl
End Sub
Anchor each control to the main Form and only resize the main form
tried to AutoScale Mode
Dim factorX As Double = ratioX * 96.0F
Dim factorY As Double = ratioY * 96.0F
Dim newSize As SizeF = New SizeF(factorX, factorY)
AutoScaleDimensions = newSize
AutoScaleMode = AutoScaleMode.Dpi
Scale(newSize)
Font = New Font(Font.FontFamily, Font.Size * factorX)
None of these methods has worked for me. What am I doing wrong?
One thing I figured out was that my main form is larger than 800*600 pixels so when I run the designer in 800*600 resolution VS cut down the with to 812px so my calculations of with and thus scaling ratio becomes wrong. This error goes applies for all three methods.
Please advise on the best method and if I am doing something wrong.
As an expansion to my first comment, take a look at some best practices from the UX (user experience) world. There is a lot of science and and deliberation put into UIs that work.
There's a great SE site - ux.stackexchange.com that you may find can answer your questsions better than SO.
Here's some questions you may find helpful:
https://ux.stackexchange.com/questions/3414/desktop-software-design-patterns (see MS & Apple have their own guidelines incl. things like button widths)
https://ux.stackexchange.com/questions/11361/responsive-web-design-technique-in-desktop-application
Responsive web design seems to parallel what you're doing. The idea behind it is to have your website be able to handle any client device - which is becoming very important because of the mobile explosion.
I suggest doing some studying in these areas to come up with a good solution.

WinForms: Locating the relative position of two controls

I'm writing a VB.Net WinForms application that has multiple data girds on any given form. On one such form, the data grids are loaded into split containers which in turn are located on a tab control. The load method for each data grid is threaded so that an animated "Loading" form can be shown. I would like to position the new loading form (which is smaller than the grid) on top of, and preferably in the center of, the grid that is loading.
Whats the easiest way to find the grids location within the main form so that I can adjust the loading forms location?
Solved. Comments? Other solutions?
Iterate through the parent controls until you find the main form. Add the point of each location to the previous.
Private Function Get_Control_Location(ByVal control As Control) As Point
If control.Name = "MainForm" Then
Return control.Location
End If
Return control.Location + Get_Control_Location(control.Parent)
End Function
Then calculate in the size of the new loading so that its centered on the grid.
Dim x As Integer = (GridControl.Width / 2) - (PleaseWait.Width / 2)
Dim y As Integer = (GridControl.Height / 2) - (PleaseWait.Height / 2)
PleaseWait.Location = Get_Control_Location(GridControl) + New Point(x, y)
I hope this helps someone else!