Console.WriteLine(uint.TryParse("-10", out x));
結果はfalse
これを利用することで、モニタの表示画面からはみ出した座標が記録されていた場合
次に開く時はモニタ中央に開けるかやってみた
string str = null;
uint x = 0;
uint y = 0;
if (File.Exists(Path))
{
using (sr = new StreamReader(Path))
{
str = sr.ReadToEnd();
}
var rgxX = new Regex(@"(^x=)([0-9]+)", RegexOptions.Multiline);
var strX = rgxX.Match(str).Groups[2].Value;
var rgxY = new Regex(@"(^y=)([0-9]+)", RegexOptions.Multiline);
var strY = rgxY.Match(str).Groups[2].Value;
if (uint.TryParse(strX, out x) && uint.TryParse(strY, out y))
{
this.Left = (int)x;
this.Top = (int)y;
}
else { SetCenter(); }
}
else { SetCenter(); }
void SetCenter()
{
// this.StartPosition = FormStartPosition.CenterScreen;はForm_Loadイベントハンドラでは機能しない
this.Left = (Screen.PrimaryScreen.Bounds.Width - this.Width) / 2;
this.Top = (Screen.PrimaryScreen.Bounds.Height - this.Height) / 2;
}
一応いけるんだけどサブモニタ上ではうまく機能しないようだ
マイナスのy座標が記録されている場合でも、そのままの座標で開いてしまう
メインモニタ上では機能するので、とりあえずいいか