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