SVN r1220
SVN-Revision: r1220
This commit is contained in:
@@ -18,6 +18,7 @@ namespace Lskj.Main
|
||||
{
|
||||
// 保存主窗体引用,用于同步位置和大小
|
||||
private Form _mainForm;
|
||||
private bool _eventsDetached;
|
||||
|
||||
public FrmWatermark(Form mainForm)
|
||||
{
|
||||
@@ -35,6 +36,7 @@ namespace Lskj.Main
|
||||
this.Opacity = 0.3;
|
||||
//重绘界面,绘制水印
|
||||
this.Paint += FrmWatermark_Paint;
|
||||
this.Disposed += FrmWatermark_Disposed;
|
||||
|
||||
|
||||
// 监听主窗体的移动和大小变化(关键修复)
|
||||
@@ -54,18 +56,21 @@ namespace Lskj.Main
|
||||
// 主窗体位置变化时同步水印位置
|
||||
private void MainForm_LocationChanged(object sender, EventArgs e)
|
||||
{
|
||||
if (IsUnavailable()) return;
|
||||
SyncPositionAndSize();
|
||||
}
|
||||
|
||||
// 主窗体大小变化时同步水印大小和位置
|
||||
private void MainForm_SizeChanged(object sender, EventArgs e)
|
||||
{
|
||||
if (IsUnavailable()) return;
|
||||
SyncPositionAndSize();
|
||||
}
|
||||
|
||||
// 主窗体显示状态变化时同步
|
||||
private void MainForm_VisibleChanged(object sender, EventArgs e)
|
||||
{
|
||||
if (IsUnavailable()) return;
|
||||
this.Visible = _mainForm.Visible;
|
||||
if (_mainForm.Visible)
|
||||
{
|
||||
@@ -77,13 +82,13 @@ namespace Lskj.Main
|
||||
// 主窗体关闭时同步关闭水印
|
||||
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
|
||||
{
|
||||
this.Close();
|
||||
SafeClose();
|
||||
}
|
||||
|
||||
// 同步水印窗体与主窗体的位置和大小
|
||||
private void SyncPositionAndSize()
|
||||
{
|
||||
if (_mainForm == null || _mainForm.IsDisposed) return;
|
||||
if (IsUnavailable()) return;
|
||||
|
||||
// 关键修复:在多屏幕环境下正确计算位置
|
||||
this.Size = _mainForm.ClientSize;
|
||||
@@ -100,6 +105,45 @@ namespace Lskj.Main
|
||||
this.Invalidate();
|
||||
}
|
||||
|
||||
public void SafeClose()
|
||||
{
|
||||
if (this.IsDisposed || this.Disposing)
|
||||
return;
|
||||
|
||||
DetachMainFormEvents();
|
||||
this.Close();
|
||||
}
|
||||
|
||||
private bool IsUnavailable()
|
||||
{
|
||||
return this.IsDisposed || this.Disposing || _mainForm == null || _mainForm.IsDisposed || _mainForm.Disposing;
|
||||
}
|
||||
|
||||
private void DetachMainFormEvents()
|
||||
{
|
||||
if (_eventsDetached || _mainForm == null)
|
||||
return;
|
||||
|
||||
_mainForm.LocationChanged -= MainForm_LocationChanged;
|
||||
_mainForm.SizeChanged -= MainForm_SizeChanged;
|
||||
_mainForm.VisibleChanged -= MainForm_VisibleChanged;
|
||||
_mainForm.FormClosing -= MainForm_FormClosing;
|
||||
_eventsDetached = true;
|
||||
}
|
||||
|
||||
protected override void OnFormClosed(FormClosedEventArgs e)
|
||||
{
|
||||
DetachMainFormEvents();
|
||||
_mainForm = null;
|
||||
base.OnFormClosed(e);
|
||||
}
|
||||
|
||||
private void FrmWatermark_Disposed(object sender, EventArgs e)
|
||||
{
|
||||
DetachMainFormEvents();
|
||||
_mainForm = null;
|
||||
}
|
||||
|
||||
private void FrmWatermark_Paint(object sender, PaintEventArgs e)
|
||||
{
|
||||
try
|
||||
|
||||
Reference in New Issue
Block a user