winforms c# windows窗体应用程序单击按钮时固定起始位置

puruo6ea  于 2022-12-14  发布在  C#
关注(0)|答案(1)|浏览(171)

我创建了一个新的windows窗体应用程序,我试图使用一个按钮,允许用户在单击该按钮时保存应用程序的启动位置。我试图查找该问题,但我找不到任何有关使用按钮来完成此操作的信息。
这里是我发现到目前为止,保存在屏幕上的位置时,窗体关闭,我只是不知道如何使它只保存位置时,一个按钮被点击:

public static void GeometryFromString(string thisWindowGeometry, Form formIn)
{
    if (string.IsNullOrEmpty(thisWindowGeometry) == true)
    {
        return;
    }
    string[] numbers = thisWindowGeometry.Split('|');
    string windowString = numbers[4];
    if (windowString == "Normal")
    {
        Point windowPoint = new Point(int.Parse(numbers[0]),
            int.Parse(numbers[1]));
        Size windowSize = new Size(int.Parse(numbers[2]),
            int.Parse(numbers[3]));

        bool locOkay = GeometryIsBizarreLocation(windowPoint, windowSize);
        bool sizeOkay = GeometryIsBizarreSize(windowSize);

        if (locOkay == true && sizeOkay == true)
        {
            formIn.Location = windowPoint;
            formIn.Size = windowSize;
            formIn.StartPosition = FormStartPosition.Manual;
            formIn.WindowState = FormWindowState.Normal;
        }
        else if (sizeOkay == true)
        {
            formIn.Size = windowSize;
        }
    }
    else if (windowString == "Maximized")
    {
        formIn.Location = new Point(100, 100);
        formIn.StartPosition = FormStartPosition.Manual;
        formIn.WindowState = FormWindowState.Maximized;
    }
}
private static bool GeometryIsBizarreLocation(Point loc, Size size)
{
    bool locOkay;
    if (loc.X < 0 || loc.Y < 0)
    {
        locOkay = false;
    }
    else if (loc.X + size.Width > Screen.PrimaryScreen.WorkingArea.Width)
    {
        locOkay = false;
    }
    else if (loc.Y + size.Height > Screen.PrimaryScreen.WorkingArea.Height)
    {
        locOkay = false;
    }
    else
    {
        locOkay = true;
    }
    return locOkay;
}
private static bool GeometryIsBizarreSize(Size size)
{
    return (size.Height <= Screen.PrimaryScreen.WorkingArea.Height &&
        size.Width <= Screen.PrimaryScreen.WorkingArea.Width);
}
public static string GeometryToString(Form mainForm)
{
    return mainForm.Location.X.ToString() + "|" +
        mainForm.Location.Y.ToString() + "|" +
        mainForm.Size.Width.ToString() + "|" +
        mainForm.Size.Height.ToString() + "|" +
        mainForm.WindowState.ToString();
}
3b6akqbq

3b6akqbq1#

下面是实现保存Location(可选地保存Size)按钮的多种方法之一。

首先,如果没有设置资源,请创建一个设置资源。右键单击项目并选择属性。

选择Settings标签,然后按一下链接以建立资源。

输入“大小”和“位置”:

Save按钮添加Click处理程序:

public MainForm()
{
    InitializeComponent();
    buttonSaveSizeAndPosition.Click += saveSizeAndPosition;
}

private async void saveSizeAndPosition(object sender, EventArgs e)
{
    Properties.Settings.Default.Location = Location;
    Properties.Settings.Default.Size = Size;
    Properties.Settings.Default.Save();
    var textB4 = Text;
    Text = $"Location = {Location} Size = {Size}";
    await Task.Delay(1000);
    Text = textB4;
}

然后,当您加载主窗体时,在重新加载这些属性之前,检查Size是否已偏离默认值:

protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    if (!Properties.Settings.Default.Size.Equals(new Size()))
    {
        Location = Properties.Settings.Default.Location;
        Size = Properties.Settings.Default.Size;
    }
}

相关问题