using Lskj.Business.Impl; using Lskj.Model; using Lskj.Util; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Windows.Forms; namespace Lskj.Main { public partial class FrmWatermark : Form { // 保存主窗体引用,用于同步位置和大小 private Form _mainForm; public FrmWatermark(Form mainForm) { InitializeComponent(); _mainForm = mainForm; // 接收主窗体引用 this.FormBorderStyle = FormBorderStyle.None; this.ControlBox = false; this.ShowInTaskbar = false; // 设置窗体背景为透明 Color color = Color.FromArgb(175, 175, 175); this.BackColor = color; this.TransparencyKey = color; this.Opacity = 0.2; //重绘界面,绘制水印 this.Paint += FrmWatermark_Paint; // 监听主窗体的移动和大小变化(关键修复) if (_mainForm != null) { _mainForm.LocationChanged += MainForm_LocationChanged; _mainForm.SizeChanged += MainForm_SizeChanged; _mainForm.VisibleChanged += MainForm_VisibleChanged; _mainForm.FormClosing += MainForm_FormClosing; } // 初始同步位置和大小 SyncPositionAndSize(); } // 主窗体位置变化时同步水印位置 private void MainForm_LocationChanged(object sender, EventArgs e) { SyncPositionAndSize(); } // 主窗体大小变化时同步水印大小和位置 private void MainForm_SizeChanged(object sender, EventArgs e) { SyncPositionAndSize(); } // 主窗体显示状态变化时同步 private void MainForm_VisibleChanged(object sender, EventArgs e) { this.Visible = _mainForm.Visible; if (_mainForm.Visible) { SyncPositionAndSize(); this.BringToFront(); } } // 主窗体关闭时同步关闭水印 private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { this.Close(); } // 同步水印窗体与主窗体的位置和大小 private void SyncPositionAndSize() { if (_mainForm == null || _mainForm.IsDisposed) return; // 关键修复:在多屏幕环境下正确计算位置 this.Size = _mainForm.ClientSize; this.Location = _mainForm.PointToScreen(new Point(0,30)); // 确保水印在主窗体上方 if (_mainForm.WindowState != FormWindowState.Minimized) { this.TopMost = true; this.BringToFront(); } // 强制重绘 this.Invalidate(); } private void FrmWatermark_Paint(object sender, PaintEventArgs e) { try { GraphicsText graphicsText = new GraphicsText(); graphicsText.Graphics = e.Graphics; StringFormat format = new StringFormat(); format.Alignment = StringAlignment.Center; format.LineAlignment = StringAlignment.Center; Pen pen = new Pen(Color.Red, 2f); int alpha = 128; // 设置透明度为128(半透明) Color color = Color.FromArgb(200, 200, 200); //color = Color.FromArgb(alpha, color); SolidBrush brush = new SolidBrush(color); if (!string.IsNullOrEmpty(ERPInfo.Instance.UserName)) { //居中 //int x = (this.Width - 90) / 2; //int y = (this.Height - 32) / 2; //graphicsText.DrawString(ERPInfo.Instance.UserName, new Font("宋体", 20, FontStyle.Bold), brush, new PointF(x, y), format, -20f); //graphicsText.DrawString(ERPInfo.Instance.UserName, new Font("宋体", 20, FontStyle.Bold), brush, new PointF(x-50, y), format, -20f); int x = 20; int y = 20; for (int i = 30; i < this.Height - 40; i=i+200) { for (int j = 40; j < this.Width - 40; j = j + 250) { graphicsText.DrawString(ERPInfo.Instanc