翼度科技»论坛 编程开发 .net 查看内容

WinForm窗口水印

7

主题

7

帖子

21

积分

新手上路

Rank: 1

积分
21
先上效果图


 
参考

思路

使用透明无框窗体覆盖需要添加水印的窗体,并设置owner为主窗体。然后在透明窗体绘制水印文本即可。
代码
  1.   1 public class Watermark
  2.   2 {
  3.   3     private string text;
  4.   4     private int gap;
  5.   5     private Font font = new Font("微软雅黑", 16, FontStyle.Regular);
  6.   6     private Color color = Color.FromArgb(255, 0, 0, 0);
  7.   7
  8.   8     private Form form = new Form();
  9.   9
  10. 10     public Watermark(Form ownerForm, string text = "Watermark", int gap = 75, double opacity = 0.1, Font font = null, Color? color = null)
  11. 11     {
  12. 12         this.text = text;
  13. 13         this.gap = gap;
  14. 14         if (font != null)
  15. 15         {
  16. 16             this.font = font;
  17. 17         }
  18. 18         if (color.HasValue)
  19. 19         {
  20. 20             this.color = color.Value;
  21. 21         }
  22. 22         form.Size = ownerForm.Size;
  23. 23         ownerForm.SizeChanged += OwnerForm_SizeChanged;
  24. 24         ownerForm.Move += OwnerForm_Move;
  25. 25
  26. 26         form.Owner = ownerForm;
  27. 27         form.FormBorderStyle = FormBorderStyle.None;
  28. 28         form.Opacity = opacity;
  29. 29         form.ShowInTaskbar = false;
  30. 30         form.TransparencyKey = Color.White;
  31. 31         form.BackColor = Color.White;
  32. 32
  33. 33         form.Paint += Form_Paint;
  34. 34         form.Show();
  35. 35
  36. 36
  37. 37         GetWindowLong(form.Handle, GWL_EXSTYLE);
  38. 38         SetWindowLong(form.Handle, GWL_EXSTYLE, WS_EX_TRANSPARENT | WS_EX_LAYERED);
  39. 39     }
  40. 40
  41. 41     private void OwnerForm_Move(object sender, EventArgs e)
  42. 42     {
  43. 43         form.Location = ((Form)sender).Location;
  44. 44     }
  45. 45
  46. 46     private void OwnerForm_SizeChanged(object sender, EventArgs e)
  47. 47     {
  48. 48         form.Size = ((Form)sender).Size;
  49. 49     }
  50. 50     private void Form_Paint(object sender, PaintEventArgs e)
  51. 51     {
  52. 52         const float cos30 = 0.866f;
  53. 53         const float sin30 = 0.5f;
  54. 54         var g = e.Graphics;
  55. 55         g.SmoothingMode = SmoothingMode.AntiAlias;
  56. 56         g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
  57. 57         //平移画布到需要画印章的位置
  58. 58         g.TranslateTransform(0, gap);
  59. 59         //逆时针旋转30度
  60. 60         g.RotateTransform(-30);
  61. 61         // 绘制画布区域
  62. 62         //g.FillRectangle(new SolidBrush(Color.FromArgb(50, 100, 100, 100)), 0, 0, frm.Width, frm.Height);
  63. 63         for (int x = 5; x < e.ClipRectangle.Right + gap; x += gap * 2)
  64. 64         {
  65. 65             for (int y = 5; y < e.ClipRectangle.Bottom + gap; y += gap * 2)
  66. 66             {
  67. 67                 // 计算文字起点位置
  68. 68                 float x1 = cos30 * x - sin30 * y;
  69. 69                 float y1 = sin30 * x + cos30 * y;
  70. 70                 //画上文字
  71. 71                 g.DrawString(text, font, new SolidBrush(color), x1, y1);
  72. 72             }
  73. 73         }
  74. 74     }
  75. 75
  76. 76     #region 在窗口结构中为指定的窗口设置信息
  77. 77     /// <summary>
  78. 78     /// 在窗口结构中为指定的窗口设置信息
  79. 79     /// </summary>
  80. 80     /// <param name="hwnd">欲为其取得信息的窗口的句柄</param>
  81. 81     /// <param name="nIndex">欲取回的信息</param>
  82. 82     /// <param name="dwNewLong">由nIndex指定的窗口信息的新值</param>
  83. 83     /// <returns></returns>
  84. 84     [DllImport("user32", EntryPoint = "SetWindowLong")]
  85. 85     private static extern uint SetWindowLong(IntPtr hwnd, int nIndex, uint dwNewLong);
  86. 86     #endregion
  87. 87
  88. 88     #region 从指定窗口的结构中取得信息
  89. 89     /// <summary>
  90. 90     /// 从指定窗口的结构中取得信息
  91. 91     /// </summary>
  92. 92     /// <param name="hwnd">欲为其获取信息的窗口的句柄</param>
  93. 93     /// <param name="nIndex">欲取回的信息</param>
  94. 94     /// <returns></returns>
  95. 95     [DllImport("user32", EntryPoint = "GetWindowLong")]
  96. 96     private static extern uint GetWindowLong(IntPtr hwnd, int nIndex);
  97. 97     #endregion
  98. 98
  99. 99     private const uint WS_EX_LAYERED = 0x80000;
  100. 100     private const int WS_EX_TRANSPARENT = 0x20;
  101. 101     private const int GWL_EXSTYLE = (-20);
  102. 102 }
复制代码
 
使用
  1. 1 public Form1()
  2. 2 {
  3. 3     InitializeComponent();
  4. 4     new Watermark(this);
  5. 5 }
复制代码
 

来源:https://www.cnblogs.com/penghanghang/p/18247146
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具