using Lskj.Business; 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; private bool _eventsDetached; 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.3; //重绘界面,绘制水印 this.Paint += FrmWatermark_Paint; this.Disposed += FrmWatermark_Disposed; // 监听主窗体的移动和大小变化(关键修复) 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) { 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) { SyncPositionAndSize(); this.BringToFront(); } } // 主窗体关闭时同步关闭水印 private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { SafeClose(); } // 同步水印窗体与主窗体的位置和大小 private void SyncPositionAndSize() { if (IsUnavailable()) 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(); } 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 { 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); Font watermarkFont = new Font("宋体", 15, FontStyle.Bold); Color color = Color.FromArgb(200, 200, 200); //int alpha = 128; // 设置透明度为128(半透明) //color = Color.FromArgb(alpha, color); SolidBrush brush = new SolidBrush(color); string content = ERPInfo.Instance.UserName; if (!string.IsNullOrWhiteSpace(SystemInfo.Instance.WatermarkContent)) { content = ReplaceHelper.ReplaceUserInfo(SystemInfo.Instance.WatermarkContent); content=content.Replace("{loginMac}", ERPInfo.Instance.MacAddress + "", true); } if (!string.IsNullOrEmpty(content)) { // 1. 测量文本实际尺寸 SizeF textSize = e.Graphics.MeasureString(content, watermarkFont); // 2. 计算旋转角度和左侧预留空间 float rotationAngle = -30f; // 旋转角度 double radians = rotationAngle * Math.PI / 180; // 转换为弧度 // 计算旋转后文本左侧可能超出的最大距离 float leftOverlap = (float)(Math.Abs(textSize.Width * Math.Cos(radians)) / 2 + Math.Abs(textSize.Height * Math.Sin(radians)) / 2); // 3. 计算动态间距 int horizontalSpacing = (int)(textSize.Width * 1.5); int verticalSpacing = (int)(textSize.Height * 2); // 设置最小间距 horizontalSpacing = Math.Max(horizontalSpacing, 250); verticalSpacing = Math.Max(verticalSpacing, 150); // 4. 计算绘制区域(只限制左侧,不限制右侧) int marginLeft = (int)leftOverlap; // 左侧预留空间,防止超出 int marginTop = 30; // 顶部基础边距 int marginBottom = 40; // 底部基础边距 int startX = marginLeft; // 起始X,确保左侧不超出 int startY = marginTop; // 起始Y int maxY = this.Height - marginBottom; // 最大Y,避免底部超出 // 5. 绘制水印网格 for (int i = startY; i < maxY; i += verticalSpacing) { // 只限制左侧,右侧允许超出(不设置maxX) for (int j = startX; j < this.Width + horizontalSpacing; j += horizontalSpacing) { var state = e.Graphics.Save(); e.Graphics.TranslateTransform(j, i); e.Graphics.RotateTransform(rotationAngle); graphicsText.DrawString( content, watermarkFont, brush, new PointF(0, 0), format, 0f); e.Graphics.Restore(state); } } } //if (!string.IsNullOrEmpty(content)) //{ // 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(content, new Font("宋体", 15, FontStyle.Bold), brush, new PointF(j,i), format, -30f); // } // } // e.Graphics.TranslateTransform(x, y); // e.Graphics.RotateTransform(-20f); //} e.Graphics.ResetTransform(); } catch (Exception ex) { string Message = ErrorMessage.PromptErrorMessage(ex); //MessageUtil.Show(Message, ex.Message); } } /// /// 鼠标穿透水印操作下方 /// protected override CreateParams CreateParams { get { CreateParams cp = base.CreateParams; cp.ExStyle |= 0x00000020; // 设置 WS_EX_TRANSPARENT 样式 return cp; } } } }