Click here to Skip to main content
15,914,500 members
Home / Discussions / C#
   

C#

 
AnswerRe: Is it possible to put a watermark on a form? Pin
perlmunger6-Jan-05 9:14
perlmunger6-Jan-05 9:14 
GeneralRe: Is it possible to put a watermark on a form? Pin
Member 966-Jan-05 10:30
Member 966-Jan-05 10:30 
GeneralRe: Is it possible to put a watermark on a form? Pin
Heath Stewart6-Jan-05 14:29
protectorHeath Stewart6-Jan-05 14:29 
GeneralRe: Is it possible to put a watermark on a form? Pin
perlmunger6-Jan-05 15:12
perlmunger6-Jan-05 15:12 
GeneralRe: Is it possible to put a watermark on a form? Pin
Heath Stewart6-Jan-05 15:47
protectorHeath Stewart6-Jan-05 15:47 
GeneralRe: Is it possible to put a watermark on a form? Pin
Heath Stewart6-Jan-05 14:31
protectorHeath Stewart6-Jan-05 14:31 
GeneralRe: Is it possible to put a watermark on a form? Pin
perlmunger6-Jan-05 14:59
perlmunger6-Jan-05 14:59 
AnswerRe: Is it possible to put a watermark on a form? Pin
Heath Stewart6-Jan-05 15:44
protectorHeath Stewart6-Jan-05 15:44 
After re-reading this I see what you're asking, but I hope that my other post answered why you were seeing what you were seeing. Also, never use CreateGraphics to owner-draw. You need to override OnPaint and use the PaintEventArgs.Graphics otherwise whatever you draw will not be re-drawn when the region is covers (or any part of it) is invalidated.

If you want to draw over everything the easiest way is to use an invisible - except for the text - window. For this you used layered windows, which are only supported on Windows 2000 and newer. Layered windows are what allow Form.Opacity to work, which is why that property is only supported on Windows 2000 and newer. P/Invoke the SetLayeredWindowAttributes API, which you can read about in the Platform SDK.

A way that is supported on all platforms is to use a clipping region for a control that covers the entire form, like so:
//#!csc.exe /t:winexe /unsafe Test.cs
 
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
 
class Test : Form
{
  static void Main()
  {
    Application.Run(new Test());
  }
 
  Test()
  {
    Watermark w = new Watermark();
    Controls.Add(w);
    w.Dock = DockStyle.Fill;
    w.Font = new Font(Font.FontFamily, 48f, FontStyle.Bold);
    w.ForeColor = SystemColors.ControlDark;
    w.Text = "Example";
 
    TextBox tb = new TextBox();
    Controls.Add(tb);
    tb.Location = new Point(8, 8);
 
    Text = "Watermark Example";
  }
}
 
public class Watermark : Control
{
  StringFormat format;
  GraphicsPath path;
  Brush brush;
 
  public Watermark()
  {
    SetStyle(ControlStyles.AllPaintingInWmPaint |
        ControlStyles.DoubleBuffer | ControlStyles.ResizeRedraw |
        ControlStyles.UserPaint, true);
 
    format = StringFormat.GenericDefault;
    format.Alignment = StringAlignment.Center;
    format.LineAlignment = StringAlignment.Center;
 
    brush = new SolidBrush(ForeColor);
 
    OnResize(EventArgs.Empty);
  }
 
  public override Color ForeColor
  {
    get { return base.ForeColor; }
    set
    {
      brush = new SolidBrush(value);
      base.ForeColor = value;
    }
  }
 
  [DefaultValue(StringAlignment.Center)]
  public StringAlignment HorizontalAlignment
  {
    get { return format.Alignment; }
    set { format.Alignment = value; }
  }
 
  [DefaultValue(StringAlignment.Center)]
  public StringAlignment VerticalAlignment
  {
    get { return format.LineAlignment; }
    set { format.LineAlignment = value; }
  }
 
  protected override void OnHandleCreated(EventArgs e)
  {
    base.OnHandleCreated(e);
    Refresh();
  }
 
  protected override void OnResize(EventArgs e)
  {
    base.OnResize(e);
 
    path = new GraphicsPath();
    path.AddString(Text, Font.FontFamily, (int)Font.Style,
      Font.Size, new PointF(Width / 2f, Height / 2f), format);
 
    Region = new Region(path);
  }
 
  protected override void OnPaint(PaintEventArgs e)
  {
    base.OnPaint(e);
 
    e.Graphics.FillPath(brush, path);
  }
}


This posting is provided "AS IS" with no warranties, and confers no rights.

Software Design Engineer
Developer Division Sustained Engineering
Microsoft

[My Articles] [My Blog]
GeneralRe: Is it possible to put a watermark on a form? Pin
perlmunger6-Jan-05 16:04
perlmunger6-Jan-05 16:04 
GeneralRe: Is it possible to put a watermark on a form? Pin
Heath Stewart7-Jan-05 5:40
protectorHeath Stewart7-Jan-05 5:40 
GeneralRe: Is it possible to put a watermark on a form? Pin
Member 967-Jan-05 5:22
Member 967-Jan-05 5:22 
GeneralRe: Is it possible to put a watermark on a form? Pin
Heath Stewart7-Jan-05 5:31
protectorHeath Stewart7-Jan-05 5:31 
GeneralRe: Is it possible to put a watermark on a form? Pin
Member 967-Jan-05 8:39
Member 967-Jan-05 8:39 
GeneralRe: Is it possible to put a watermark on a form? Pin
Heath Stewart7-Jan-05 9:09
protectorHeath Stewart7-Jan-05 9:09 
GeneralRe: Is it possible to put a watermark on a form? Pin
Member 967-Jan-05 9:29
Member 967-Jan-05 9:29 
GeneralRe: Is it possible to put a watermark on a form? Pin
Heath Stewart7-Jan-05 11:38
protectorHeath Stewart7-Jan-05 11:38 
GeneralApplication.Exit Pin
pcJuice6-Jan-05 7:42
pcJuice6-Jan-05 7:42 
GeneralRe: Application.Exit Pin
Nick Parker6-Jan-05 7:56
protectorNick Parker6-Jan-05 7:56 
GeneralRe: Application.Exit Pin
pcJuice6-Jan-05 8:01
pcJuice6-Jan-05 8:01 
GeneralRe: Application.Exit Pin
Adam Goossens7-Jan-05 0:55
Adam Goossens7-Jan-05 0:55 
GeneralRe: Application.Exit Pin
Adam Goossens7-Jan-05 1:21
Adam Goossens7-Jan-05 1:21 
GeneralRe: Application.Exit Pin
pcJuice7-Jan-05 13:51
pcJuice7-Jan-05 13:51 
QuestionHow much exception handling is to much? Pin
KevinMac6-Jan-05 6:26
KevinMac6-Jan-05 6:26 
AnswerRe: How much exception handling is to much? Pin
Andy Brummer6-Jan-05 7:57
sitebuilderAndy Brummer6-Jan-05 7:57 
GeneralRe: How much exception handling is to much? Pin
Heath Stewart6-Jan-05 13:52
protectorHeath Stewart6-Jan-05 13:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.