0

So, I'm trying to create a application to create a watermark on user's computer, to avoid information leakage by some picture taken using a phone or something like that.

At this moment, I have a windows form application that almost accoplish the objective, but I need (it was imposed, indeed) that the text written must be written diagonally. Until now, my application is a windows form with a translucide form, filled up with labels in horizontal orientation .

Using a windows form application, I can't create it, since I'm using labels to write the watermark text.

I was planning to dynamically create some .png images, write on each one the text and dispose them along the main form, or even create a single image and write the text all over it.

Some have any suggestions ? Stay in this way or changing the approach ?

Thanks anyway for your attention for read it untill got this point!

Bernard Vander Beken
  • 4,848
  • 5
  • 54
  • 76
  • 1
    welcome to SO, check this one out: https://stackoverflow.com/questions/1371943/c-sharp-vertical-label-in-a-windows-forms – Isparia Nov 16 '21 at 13:55
  • Use the form's `BackgroundImage` property to display the drawn image and use the `TransparencyKey` to make the rest of the form transparent. That said, if you're expecting your "watermark" to _never_ be covered by anything, you should probably read: https://devblogs.microsoft.com/oldnewthing/20050607-00/?p=35413 – 41686d6564 stands w. Palestine Nov 16 '21 at 14:00
  • The big problem is create a image with text that shoulb be user name. So, I need to create an image dinamaclly, write user name on it and set as background image. I've been searching through SO but without success. Any clues ? – Bruno Leonidio Nov 16 '21 at 14:16
  • 1
    In that case, does this answer your question? [How can drawString method be used for writing diagonal way](https://stackoverflow.com/questions/91521/how-can-drawstring-method-be-used-for-writing-diagonal-way) – 41686d6564 stands w. Palestine Nov 16 '21 at 14:22
  • Kinda, it's solve my problem about create the text diagonally. But I dunno how to use a dynamically image as a background image. But thanks for fastest answer I've seen lol. – Bruno Leonidio Nov 16 '21 at 14:27

1 Answers1

1

Here's a simple example using a borderless form that is "clickthrough". It doesn't interfere with the mouse:

public partial class Watermark : Form
{

    public String value = "Idle_Mind"; // set this somehow
    private const int WS_EX_TRANSPARENT = 0x20;

    public Watermark()
    {
        InitializeComponent();
        this.Opacity = .25;
        this.TopMost = true;
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
        this.WindowState = FormWindowState.Maximized;
        this.Paint += Watermark_Paint;
    }

    // this makes the form ignore all clicks, so it is "passthrough"
    protected override System.Windows.Forms.CreateParams CreateParams
    {
        get
        {
            CreateParams cp = base.CreateParams;
            cp.ExStyle = cp.ExStyle | WS_EX_TRANSPARENT;
            return cp;
        }
    }

    private void Watermark_Paint(object sender, PaintEventArgs e)
    {
        if (value != "")
        {
            // play with this drawing code to change your "watermark"
            SizeF szF = e.Graphics.MeasureString(value, this.Font);
            e.Graphics.RotateTransform(-45);
            int max = Math.Max(this.Width, this.Height);
            for(int y=0; y<=max; y=y+(2*(int)szF.Height))
            {
                e.Graphics.DrawString(value, this.Font, Brushes.Black, 0, y);
            }
        }
    }
}

You'll need to do more work to keep the overlay from appearing in the Alt-Tab list, and also prevent it from being minimized/restored using keyboard shortcuts.

Screenshot:

enter image description here

Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
  • You also may want to make sure the app works with multiple screens ;) Just saying. – TomTom Nov 16 '21 at 15:34
  • Hum... Looks like the magic happens on "Paint" event. That I was looking for (maybe I was asking in the wrong way). Thanks ! I gonna test it. I was stuck whit Graphic object without know how to use it with windows forms (I'm from backend team, just helping friends in other squad). Thank you sooooo much =) – Bruno Leonidio Nov 16 '21 at 17:05
  • @TomTom Definitely! This is some very basic STARTER code.... – Idle_Mind Nov 16 '21 at 20:20
  • I have done this validation about multiple screen in my very first vesion application, it's pretty easy. Actually, my first version do it, write in both screens (I have only 2 screens), but writing horizontally. Nevertheless It's a good hint! Thanks for it! We need more good complementations like that! – Bruno Leonidio Nov 16 '21 at 20:57