簡體   English   中英

如何使用showdialog(.NET Compact Framework)使表單居中

[英]How to center a form using showdialog (.NET Compact Framework)

我想集中在.NET Compact Framework中使用Form.ShowDialog()啟動的彈出窗體。 我沒有在.NET CF Form對象中看到任何像StartPosition這樣的屬性。

有人可以建議我如何在.NET CF 3.5中居中彈出窗口?

您可以創建一個為您工作的擴展方法:

public static class FormExtensions
{
    public static void CenterForm(this Form theForm)
    {
        theForm.Location = new Point(
            Screen.PrimaryScreen.WorkingArea.Width / 2 - theForm.Width / 2,
            Screen.PrimaryScreen.WorkingArea.Height / 2 - theForm.Height / 2);
    }
}

你這樣稱呼它:

TheDialogForm f = new TheDialogForm();
f.CenterForm();            
f.ShowDialog();

如果沒有為Dialog定義Parent,則使用

login.StartPosition = FormStartPosition.CenterScreen;
login.ShowDialog(); 

其中login是Form Object

或者,如果您在現有的父Form之上致電,也可以使用

login.StartPosition = FormStartPosition.CenterParent;
login.ShowDialog();

如果您認為該屬性始終與您相同,也可以在Form的“屬性”對話框中設置此屬性。 默認情況下,它應該設置為CenterParent,如果您在某些情況下在父Form之前調用您的Form ,如第一次登錄屏幕等,則無法使用它。

如果您希望彈出窗體默認顯示在屏幕的中心,您只需在窗體屬性中設置它的起始位置,它應該聽起來像'中心父級'。

像這樣的東西:

form1.StartPosition = FormStartPosition.CenterScreen;

我知道這是舊帖子,但我有同樣的問題,我用這種方式解決了:

我創建了一個接口:

public interface FormExtensions
    {
        void CenterForm(Form forma);
    }

我在我的類上實現了接口之后:

    public partial class frmFirma : Form, FormExtensions
    {
        public frmFirma()
        {
            InitializeComponent();
        }
        public void CenterForm(Form forma)
        {
            forma.Location = new Point(
            Screen.PrimaryScreen.WorkingArea.Width / 2 - forma.Width / 2,
            Screen.PrimaryScreen.WorkingArea.Height / 2 - forma.Height / 2);
        }
    }

然后我可以創建一個實例:“frmFirma”調用方法“CenterForm”:

private void pictureBox1_DoubleClick(object sender, EventArgs e)
        {
            Formas.frmFirma firma = new Formas.frmFirma();
            firma.CenterForm(firma);
            firma.ShowDialog();     
        }

我希望這適用於某人。

這是最簡單的方法

Form f= new AmrDealForm();
f.CenterToScreen();
f.ShowDialog();

在'frmDialog_Activated事件中設置表單的左側和頂部屬性

Private Sub frmDialog_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
        Me.Left = (frmMain.Width - Me.Width) / 2 ' AS Your Wish
        Me.Top = (frmMain.Height - Me.Height) / 2 + 165 '' AS Your Wish
    End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM