gpt4 book ai didi

c# - 为什么不能将 Windows 窗体的大小绑定(bind)到 ApplicationSettings?

转载 作者:行者123 更新时间:2023-11-30 18:52:19 24 4
gpt4 key购买 nike

更新:已解决,有代码

I got it working, see my answer below for the code...

原帖

正如 Tundey 在 his answer to my last question 中指出的那样,您几乎可以毫不费力地将 Windows 窗体控件的所有内容绑定(bind)到 ApplicationSettings。那么真的没有办法用表单大小来做到这一点吗? This tutorial 表示您需要显式处理 Size,以便在窗口最大化或最小化时可以保存 RestoreBounds 而不是 size。但是,我希望我可以使用如下属性:

public Size RestoreSize
{
get
{
if (this.WindowState == FormWindowState.Normal)
{
return this.Size;
}
else
{
return this.RestoreBounds.Size;
}
}
set
{
...
}
}

但我看不到在设计器中绑定(bind)它的方法(PropertyBinding 列表中明显缺少 Size)。

最佳答案

我终于想出了一个 Form 子类来一劳永逸地解决这个问题。要使用它:

  1. 继承自 RestorableForm 而不是 Form。
  2. 将 (ApplicationSettings) -> (PropertyBinding) 中的绑定(bind)添加到 WindowRestoreState。
  3. 在窗口即将关闭时调用 Properties.Settings.Default.Save()。

现在窗口位置和状态将在 session 之间被记住。根据下面其他发帖人的建议,我加入了一个 ConstrainToScreen 函数,以确保窗口在恢复时能够很好地适应可用的显示。

代码

// Consider this code public domain. If you want, you can even tell
// your boss, attractive women, or the other guy in your cube that
// you wrote it. Enjoy!

using System;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;

namespace Utilities
{
public class RestorableForm : Form, INotifyPropertyChanged
{
// We invoke this event when the binding needs to be updated.
public event PropertyChangedEventHandler PropertyChanged;

// This stores the last window position and state
private WindowRestoreStateInfo windowRestoreState;

// Now we define the property that we will bind to our settings.
[Browsable(false)] // Don't show it in the Properties list
[SettingsBindable(true)] // But do enable binding to settings
public WindowRestoreStateInfo WindowRestoreState
{
get { return windowRestoreState; }
set
{
windowRestoreState = value;
if (PropertyChanged != null)
{
// If anybody's listening, let them know the
// binding needs to be updated:
PropertyChanged(this,
new PropertyChangedEventArgs("WindowRestoreState"));
}
}
}

protected override void OnClosing(CancelEventArgs e)
{
WindowRestoreState = new WindowRestoreStateInfo();
WindowRestoreState.Bounds
= WindowState == FormWindowState.Normal ?
Bounds : RestoreBounds;
WindowRestoreState.WindowState = WindowState;

base.OnClosing(e);
}

protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);

if (WindowRestoreState != null)
{
Bounds = ConstrainToScreen(WindowRestoreState.Bounds);
WindowState = WindowRestoreState.WindowState;
}
}

// This helper class stores both position and state.
// That way, we only have to set one binding.
public class WindowRestoreStateInfo
{
Rectangle bounds;
public Rectangle Bounds
{
get { return bounds; }
set { bounds = value; }
}

FormWindowState windowState;
public FormWindowState WindowState
{
get { return windowState; }
set { windowState = value; }
}
}

private Rectangle ConstrainToScreen(Rectangle bounds)
{
Screen screen = Screen.FromRectangle(WindowRestoreState.Bounds);
Rectangle workingArea = screen.WorkingArea;

int width = Math.Min(bounds.Width, workingArea.Width);
int height = Math.Min(bounds.Height, workingArea.Height);

// mmm....minimax
int left = Math.Min(workingArea.Right - width,
Math.Max(bounds.Left, workingArea.Left));
int top = Math.Min(workingArea.Bottom - height,
Math.Max(bounds.Top, workingArea.Top));

return new Rectangle(left, top, width, height);
}
}
}

设置绑定(bind)引用

关于c# - 为什么不能将 Windows 窗体的大小绑定(bind)到 ApplicationSettings?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18585/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com