Code Share: Add a watermark to the form

Sometimes you need to add a watermark to the form, it may not be registered, or has reviewed other documentation.

Screenshot below

image


:( There is a code that did not solve the problem is to switch other programs is the watermark will not go away, the other will cover the program, interested students can try to control what events Active)

Watermark class public
     {
         // the bottom cover to form a watermark
         // Called Watermark wm = new Watermark (watermark to be covered form);
         public Watermark (Form1 Ctrl)
         {
             Form1 FRM = new new Form1 ();
             ctrl.SizeChanged + = the delegate
             {
                 frm.Size = ctrl.Size;
             };

            ctrl.Move += delegate
             {
                 frm.Location = ctrl.Location;
             };

            frm.TopMost = true;
             frm.FormBorderStyle = FormBorderStyle.None;
             frm.Opacity = 0.3;
             frm.ShowInTaskbar = false;

            string status = "approval";

            Bitmap myBitmap = new Bitmap(128, 128);
             myBitmap.MakeTransparent(Color.Transparent);

            Graphics g = Graphics.FromImage(myBitmap);

            Graphics G = this.CreateGraphics // ();
             //g.Clear(Color.White);
             // antialiasing
             g.SmoothingMode = SmoothingMode.AntiAlias;
             g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
             // translating the desired position canvas painting seal
             g.TranslateTransform (0, 64);
             rotating 30 degrees counterclockwise //
             g.RotateTransform (-30);
             // draw an oval box
             // SizeF sf = g.MeasureString (status, new font ( "Microsoft elegant black", 16, FontStyle.Bold));
             //g.DrawEllipse(new Pen (Color.Red,. 3), 0, 0, sf.Width + 10, + 10 sf.Height);
             / / draw text
             g.DrawString (status, new Font ( "Microsoft elegant black", 16, FontStyle.Bold), Brushes.Red , 5, 5);

            myBitmap.Save("png.png", ImageFormat.Png);
             frm.BackgroundImage = myBitmap;
             frm.Show();


             GetWindowLong(frm.Handle, GWL_EXSTYLE);
             SetWindowLong(frm.Handle, GWL_EXSTYLE, WS_EX_TRANSPARENT | WS_EX_LAYERED);

        }
         #Region setting information specified window to the window structure
         /// <Summary>
         /// setting information specified window in the window structure
         /// </ Summary>
         /// <param name = "HWND"> to obtain a handle for the window information </ param>
         /// <param name = "nIndex"> information to be retrieved </ param>
         /// <param name = "dwNewLong"> specified by the window information nIndex the new value of </ param>
         /// <Returns> </ Returns>
         [the DllImport ( "user32", the EntryPoint = "the SetWindowLong")]
         Private static extern uint the SetWindowLong (IntPtr HWND, int nIndex, uint dwNewLong);
         #endregion

        #region acquires information from the specified structure of the window
         /// <Summary>
         /// acquires information from the specified structure of the window
         /// </ Summary>
         /// <param name = "HWND"> For obtaining information for window handle </ param>
         /// <param name = "nIndex"> information to be retrieved </ param>
         /// <Returns> </ Returns>
         [the DllImport ( "user32", the EntryPoint = "the GetWindowLong" )]
         Private static extern uint the GetWindowLong (IntPtr HWND, int nIndex);
         #endregion

        private const uint WS_EX_LAYERED = 0x80000;
         private const int WS_EX_TRANSPARENT = 0x20;
         private const int GWL_EXSTYLE = (-20);
     }

Guess you like

Origin www.cnblogs.com/myrapid/p/11790617.html