C#.NET與JAVA互通之MD5哈希V2024 配套視頻: 要點: 1.計算MD5時,SDK自帶的計算哈希(ComputeHash)方法,輸入輸出參數都是byte數組。就涉及到字元串轉byte數組轉換時,編碼選擇的問題。 2.輸入參數,字元串轉byte數組時,編碼雙方要統一,一般為:UTF-8。 ...
先上效果圖
參考
思路
使用透明無框窗體覆蓋需要添加水印的窗體,並設置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 [DllImport("user32", EntryPoint = "SetWindowLong")] 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 [DllImport("user32", EntryPoint = "GetWindowLong")] 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 }