Positioning
StartPosition Enumeration
Enables you to set the start position of a form at run-time
This property should be set before the form is shown.
This property works with both .Show and .ShowDialog
WindowsDefaultLocation (default) | Form is positioned at the windows default location and has the dimensions specified in the forms size |
WindowsDefaultBounds | Form is positioned at the windows default location and has the bounds determined by windows default |
CenterParent | Form is centered within the bounds of its parent form |
best for modal / ShowDialog | |
CenterScreen | Form is centered on the current display with dimensions specified in the forms size |
Manual | Position is determined by the Location property |
Me.Location = Drawing.Point |
Possible Scenarios
1) Modal, on top of all windows
.TopMost = True
2) Modal, on top of that application
.StartPosition = CenterParent
.ShowDialog
2) Modeless, on top of that application
.StartPosition = Manual
.Show(gFormNativeWindow)
System.Windows.Forms.Screen.PrimaryScreen
this gets the primary screen
WorkingArea.Height - gets the height of the display excluding the taskbar.
Approach 1 - Manual / Location Property
This approach uses one Windows API.
You need to pass the size of the application screen to the windows form so the form can be positioned in the center of the application.
Add the following structure definition to the namespace
public struct Structure_RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
Add the following code inside a new class called Class1
[System.Runtime.InteropServices.DllImport("user32.dll")]
public extern bool GetWindowRect(System.IntPtr hWnd, ref Structure_RECT rect);
public System.IntPtr Method_HandleGet(Microsoft.Office.Interop.Excel.Application oApplication)
{
return new System.IntPtr(oApplication.Hwnd);
}
public static void Method_DisplayForm()
{
System.IntPtr ohandle;
Structure_RECT oRect = new Structure_RECT();
System.Windows.Forms.NativeWindow nativeWindow;
ohandle = Method_HandleGet(Globals.ThisAddIn.Application);
nativeWindow = new System.Windows.Forms.NativeWindow();
nativeWindow.AssignHandle(ohandle);
GetWindowRect(ohandle, ref oRect);
nativeWindow.ReleaseHandle();
int applicationCenter_x;
int applicationCenter_y;
applicationCenter_x = oRect.Left + (oRect.Right - oRect.Left) / 2;
applicationCenter_y = oRect.Top + (oRect.Bottom - oRect.Top) / 2;
Form1 oMyForm;
oMyForm = new Form1(applicationCenter_x, applicationCenter_y);
oMyForm.ShowDialog();
}
Add the following code to the Form1 class.
public partial class Form1 : Form
{
public Form1(
int Center_x,
int Center_y)
{
int windowCenter_x;
int windowCenter_y;
InitializeComponent();
windowCenter_x = Center_x - (int)this.Width / 2;
windowCenter_y = Center_y - (int)this.Height / 2;
this.StartLocation = System.Windows.Forms.FormStartPosition.Manual;
this.Location = new System.Drawing.Point(windowCenter_x, windowCenter_y);
}
Approach 2 - Saving the Position
This event will be generated for you by double clicking anywhere on the form.
Private Sub frmMain_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
If App_RegSaveFormPositionRead() = True Then
Me.Location = New System.Drawing.Point(clsRegistry.App_RegFormPositionLeftRead(gsREGISTRY_APPNAME), _
clsRegistry.App_RegFormPositionTopRead(gsREGISTRY_APPNAME))
Me.Size = New System.Drawing.Size(clsRegistry.App_RegFormPositionWidthRead(gsREGISTRY_APPNAME, _
giDEFAULT_FORMWIDTH), _
clsRegistry.App_RegFormPositionHeightRead(gsREGISTRY_APPNAME, _
giDEFAULT_FORMHEIGHT))
End If
End Sub
#Region " Windows Form Designer generated code "
Inside this region is a Dispose generated subroutine.
Add the call to the Form_Load subroutine at the top of this subroutine.
Protected Overloads Overrides Sub Dispose(ByVal bdisposing As Boolean)
Call frmMain_Close()
If bdisposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(bdisposing)
End Sub
Create the following private subroutine below the Form_Load event
Private Sub frmMain_Close()
If App_RegSaveFormPositionRead() = True Then
With gfrmMAINFORM.DesktopBounds
Call clsRegistry.App_RegFormPositionLeftSave(gsREGISTRY_APPNAME, .Left)
Call clsRegistry.App_RegFormPositionTopSave(gsREGISTRY_APPNAME, .Top)
Call clsRegistry.App_RegFormPositionWidthSave(gsREGISTRY_APPNAME, .Width)
Call clsRegistry.App_RegFormPositionHeightSave(gsREGISTRY_APPNAME, .Height)
End With
End If
End Sub
These are the two helper methods
Public Function App_RegSaveFormPositionRead() As Boolean
App_RegSaveFormPositionRead = _
CBool(App_RegistryRead("Options", "Save Form Position", CStr(False)))
End Function
Public Sub App_RegSaveFormPositionSave(ByVal bValue As Boolean)
Call App_RegistrySave("Options", "Save Form Position", CStr(bValue))
End Sub
© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext