C winform set start location at top right năm 2024

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 sizeWindowsDefaultBoundsForm is positioned at the windows default location and has the bounds determined by windows defaultCenterParentForm is centered within the bounds of its parent form best for modal / ShowDialogCenterScreenForm is centered on the current display with dimensions specified in the forms sizeManualPosition is determined by the Location property Me.Location = Drawing.Point

Possible Scenarios

  1. Modal, on top of all windows .TopMost = True
  1. Modal, on top of that application .StartPosition = CenterParent .ShowDialog
  1. 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 PrevNext

How to set the start position of a form in C#?

The form's position can be specified manually by setting the Location property or use the default location specified by Windows. You can also position the form to display in the center of the screen or in the center of its parent form for forms such as multiple-document interface (MDI) child forms.nullForm.StartPosition Property (System.Windows.Forms)learn.microsoft.com › en-us › dotnet › api › system.windows.forms.form.st...null

How can you set size and position a startup form on the Windows desktop?

First, you can set it with the size grips in the designer. By dragging either the right edge, bottom edge, or the corner, you can resize the form. The second way you can resize the form while the designer is open, is through the properties pane. Select the form, then find the Properties pane in Visual Studio.nullPosition and resize a form - Windows Forms .NET - Microsoft Learnlearn.microsoft.com › en-us › dotnet › desktop › how-to-position-and-resizenull

How to set startup page in C# Windows forms?

Set MainForm to be the startup form for the project:.

For C#, go to Program. cs, in the Main() method, initialize MainForm in the call to Application. Run(): C# ... .

For VB.NET, open the properties window of the project, in the Application select MainForm from the Startup form dropdown list..

How do you change the location of a control in Windows forms?

Position a control using the Properties window.

In Visual Studio, select the control you want to position..

In the Properties window, enter values for the Location property, separated by a comma, to position the control within its container..

Chủ đề